aboutsummaryrefslogtreecommitdiff
path: root/pkg/worker/list_processor_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/worker/list_processor_test.go')
-rw-r--r--pkg/worker/list_processor_test.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/pkg/worker/list_processor_test.go b/pkg/worker/list_processor_test.go
new file mode 100644
index 0000000..b7373d1
--- /dev/null
+++ b/pkg/worker/list_processor_test.go
@@ -0,0 +1,90 @@
+// go:build unit
+
+package worker
+
+import (
+ "context"
+ "errors"
+ "math/rand"
+ "sync"
+ "testing"
+
+ "git.sr.ht/~gabrielgio/img/pkg/testkit"
+)
+
+type (
+ mockCounterListProcessor struct {
+ done bool
+ countTo int
+ counter int
+ }
+
+ mockContextListProcessor struct {
+ }
+)
+
+func TestListProcessorLimit(t *testing.T) {
+ mock := &mockCounterListProcessor{
+ countTo: 10000,
+ }
+ worker := NewWorkerFromListProcessor[int](mock, nil)
+
+ err := worker.Start(context.Background())
+ testkit.TestFatalError(t, "Start", err)
+
+ testkit.TestValue(t, "Start", mock.countTo, mock.counter)
+}
+
+func TestListProcessorContextCancelQuery(t *testing.T) {
+ mock := &mockContextListProcessor{}
+ worker := NewWorkerFromListProcessor[int](mock, nil)
+
+ ctx, cancel := context.WithCancel(context.Background())
+ var wg sync.WaitGroup
+
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ err := worker.Start(ctx)
+ if errors.Is(err, context.Canceled) {
+ return
+ }
+ testkit.TestFatalError(t, "Start", err)
+ }()
+
+ cancel()
+ // this rely on timeout to test
+ wg.Wait()
+}
+
+func (m *mockCounterListProcessor) Query(_ context.Context) ([]int, error) {
+ if m.done {
+ return make([]int, 0), nil
+ }
+ values := make([]int, 0, m.countTo)
+ for i := 0; i < m.countTo; i++ {
+ values = append(values, rand.Int())
+ }
+
+ m.done = true
+ return values, nil
+}
+
+func (m *mockCounterListProcessor) Process(_ context.Context, _ int) error {
+ m.counter++
+ return nil
+}
+
+func (m *mockContextListProcessor) Query(_ context.Context) ([]int, error) {
+ // keeps returning the query so it can run in infinity loop
+ values := make([]int, 0, 10)
+ for i := 0; i < 10; i++ {
+ values = append(values, rand.Int())
+ }
+ return values, nil
+}
+
+func (m *mockContextListProcessor) Process(_ context.Context, _ int) error {
+ // do nothing
+ return nil
+}