2023-02-08 12:15:44 +00:00
|
|
|
package sources
|
|
|
|
|
|
|
|
import (
|
2023-10-30 14:28:25 +00:00
|
|
|
"errors"
|
2023-02-28 03:02:59 +00:00
|
|
|
"fmt"
|
2023-02-08 12:15:44 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-03-26 13:34:12 +00:00
|
|
|
var testError = fmt.Errorf("simulated failure")
|
|
|
|
|
2023-02-08 12:15:44 +00:00
|
|
|
func TestNewScanErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
projects int
|
|
|
|
want *ScanErrors
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no projects",
|
|
|
|
projects: 0,
|
|
|
|
want: &ScanErrors{
|
|
|
|
errors: make([]error, 0),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one project",
|
|
|
|
projects: 1,
|
|
|
|
want: &ScanErrors{
|
|
|
|
errors: make([]error, 0, 1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "fifty projects",
|
|
|
|
projects: 50,
|
|
|
|
want: &ScanErrors{
|
|
|
|
errors: make([]error, 0, 50),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-02-09 01:33:30 +00:00
|
|
|
got := NewScanErrors()
|
2023-02-08 12:15:44 +00:00
|
|
|
|
2023-10-30 14:28:25 +00:00
|
|
|
if !errors.Is(got.Errors(), tc.want.Errors()) {
|
2023-02-08 12:15:44 +00:00
|
|
|
t.Errorf("got %+v, want %+v", got, tc.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScanErrorsAdd(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
concurrency int
|
|
|
|
wantErr int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no concurrency, no errors",
|
|
|
|
concurrency: 1,
|
|
|
|
wantErr: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no concurrency, one error",
|
|
|
|
concurrency: 1,
|
|
|
|
wantErr: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "concurrency, 100 errors",
|
|
|
|
concurrency: 10,
|
|
|
|
wantErr: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "concurrency, 1000 errors",
|
|
|
|
concurrency: 10,
|
|
|
|
wantErr: 1000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-02-09 01:33:30 +00:00
|
|
|
se := NewScanErrors()
|
2023-02-08 12:15:44 +00:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < tc.concurrency; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
for j := 0; j < tc.wantErr/tc.concurrency; j++ {
|
2024-03-26 13:34:12 +00:00
|
|
|
se.Add(testError)
|
2023-02-08 12:15:44 +00:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if se.Count() != uint64(tc.wantErr) {
|
|
|
|
t.Errorf("got %d, want %d", se.Count(), tc.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScanErrorsCount(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
concurrency int
|
|
|
|
wantErrCnt int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no concurrency, no errors",
|
|
|
|
concurrency: 1,
|
|
|
|
wantErrCnt: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no concurrency, one error",
|
|
|
|
concurrency: 1,
|
|
|
|
wantErrCnt: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "concurrency, 100 errors",
|
|
|
|
concurrency: 10,
|
|
|
|
wantErrCnt: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "concurrency, 2048 errors",
|
|
|
|
concurrency: 8,
|
|
|
|
wantErrCnt: 2048,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-02-09 01:33:30 +00:00
|
|
|
se := NewScanErrors()
|
2023-02-08 12:15:44 +00:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < tc.concurrency; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
for j := 0; j < tc.wantErrCnt/tc.concurrency; j++ {
|
2024-03-26 13:34:12 +00:00
|
|
|
se.Add(testError)
|
2023-02-08 12:15:44 +00:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if se.Count() != uint64(tc.wantErrCnt) {
|
|
|
|
t.Errorf("got %d, want %d", se.Count(), tc.wantErrCnt)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-02-28 03:02:59 +00:00
|
|
|
|
|
|
|
func TestScanErrorsString(t *testing.T) {
|
|
|
|
se := NewScanErrors()
|
2024-03-26 13:34:12 +00:00
|
|
|
se.Add(testError)
|
|
|
|
want := `["` + testError.Error() + `"]`
|
2023-02-28 03:02:59 +00:00
|
|
|
if got := fmt.Sprintf("%v", se); got != want {
|
|
|
|
t.Errorf("got %q, want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|