[chore] - Add tests for errors (#1071)

This commit is contained in:
ahrav 2023-02-08 04:15:44 -08:00 committed by GitHub
parent 7dd2b74f1f
commit 0d73dbe638
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

149
pkg/sources/errors_test.go Normal file
View file

@ -0,0 +1,149 @@
package sources
import (
"reflect"
"sync"
"testing"
)
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) {
got := NewScanErrors(tc.projects)
if !reflect.DeepEqual(got, tc.want) {
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) {
se := NewScanErrors(tc.wantErr)
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++ {
se.Add(nil)
}
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) {
se := NewScanErrors(tc.wantErrCnt)
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++ {
se.Add(nil)
}
wg.Done()
}()
}
wg.Wait()
if se.Count() != uint64(tc.wantErrCnt) {
t.Errorf("got %d, want %d", se.Count(), tc.wantErrCnt)
}
})
}
}