-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker_test.go
101 lines (82 loc) · 2.28 KB
/
worker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// -----------------------------------------------------------------------------
// worker for running tasks enqueued in background.
//
// Copyright (C) 2024 Frank Mueller / Oldenburg / Germany / World
// -----------------------------------------------------------------------------
package worker_test
import (
"context"
"testing"
"time"
"tideland.dev/go/audit/asserts"
"tideland.dev/go/worker"
)
// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------
// TestNewOK tests the simple creation of a worker.
func TestNewOK(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
w, err := worker.New(context.TODO())
assert.OK(err)
assert.NotNil(w)
}
// TestStoppingOK tests the stopping of running worker.
func TestStoppingOK(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
w, err := worker.New(context.TODO())
assert.OK(err)
assert.NotNil(w)
err = worker.Shutdown(w)
assert.OK(err)
}
// TestEnqueueOK tests the enqueuing of tasks in order to process them.
func TestEnqueueOK(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
w, err := worker.New(context.TODO())
assert.OK(err)
assert.NotNil(w)
// Enqueue some counter tasks.
count := 0
task := func() error {
count++
return nil
}
err = worker.Enqueue(w, task)
assert.OK(err)
err = worker.Enqueue(w, task)
assert.OK(err)
err = worker.Enqueue(w, task)
assert.OK(err)
// Stop the worker and check the count.
err = worker.Shutdown(w)
assert.OK(err)
assert.Equal(count, 3)
}
func TestAsyncAwaitOK(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
w, err := worker.New(context.TODO())
assert.OK(err)
assert.NotNil(w)
count := 0
task := func() error {
count++
return nil
}
await, err := worker.AsyncAwait(w, task, 1*time.Second)
assert.OK(err)
// Simulate some work.
simulator := 0
for i := 0; i < 50; i++ {
simulator++
}
// Wait for the worker to finish the task.
err = await()
assert.OK(err)
err = worker.Shutdown(w)
assert.OK(err)
assert.Equal(count, 1)
}
// -----------------------------------------------------------------------------
// end of file
// -----------------------------------------------------------------------------