diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-02-26 19:54:48 +0100 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-18 16:30:36 +0200 |
commit | c8e1328164e9ffbd681c3c0e449f1e6b9856b896 (patch) | |
tree | faee639a4c55c5dc3bfc59a5400026822c40221d /pkg/worker/list_processor_test.go | |
download | lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.tar.gz lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.tar.bz2 lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.zip |
feat: Inicial commit
It contains rough template for the server and runners.
It contains rough template for the server and runners.
Diffstat (limited to 'pkg/worker/list_processor_test.go')
-rw-r--r-- | pkg/worker/list_processor_test.go | 90 |
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 +} |