-
Notifications
You must be signed in to change notification settings - Fork 4
/
worker_test.go
75 lines (73 loc) · 2.3 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
package parallel
import (
"sync"
"testing"
)
func TestWorker(t *testing.T) {
t.Run("Initialization", func(t *testing.T) {
p := New()
t.Run("NewWorkerIncorrectConfig", func(t *testing.T) {
_, err := p.NewWorker("TestWorker", &WorkerConfig{Parallelism: 0})
testNotNil(t, err)
})
t.Run("NewWorker", func(t *testing.T) {
w, err := p.NewWorker("TestWorker", &WorkerConfig{Parallelism: 1})
testNil(t, err)
testNotNil(t, w)
})
t.Run("DuplicateNewWorker", func(t *testing.T) {
_, err := p.NewWorker("TestWorker", &WorkerConfig{Parallelism: 1})
testNotNil(t, err)
})
t.Run("GetWorker", func(t *testing.T) {
w := p.Worker("TestWorker")
testAssert(t, w.Name == "TestWorker", "GetWorker failed: incorrect name")
testNotNil(t, w.Config)
testAssert(t, w.Config.Parallelism == 1, "GetWorker failed: incorrect parallelism")
})
t.Run("GetWorkerDoesntExist", func(t *testing.T) {
w := p.Worker("DoesntExist")
testAssert(t, w == nil, "worker is not nil")
})
t.Run("SetParallelism", func(t *testing.T) {
p.Worker("TestWorker").SetParallelism(12)
testAssert(t, p.Worker("TestWorker").Config.Parallelism == 12, "GetWorker failed: incorrect parallelism")
})
})
t.Run("Execution", func(t *testing.T) {
p := New()
w, err := p.NewWorker("TestWorker", &WorkerConfig{Parallelism: 1})
if err != nil {
t.Fatalf("ExecutionSetup: Failed to create new worker: %v", err)
}
t.Run("ExecutionSingle", func(t *testing.T) {
ts := &testStruct{Counter: 0, Mutex: sync.Mutex{}}
ef := func(wh *WorkerHelper, args interface{}) {
ts := args.(*testStruct)
ts.Mutex.Lock()
ts.Counter = ts.Counter + 1
ts.Mutex.Unlock()
wh.Done()
}
w.SetExecution(ef)
w.Start(interface{}(ts))
w.Wait()
testAssert(t, ts.Counter == 1, "Execution failed: counter does not equal 1 (equals %v)", ts.Counter)
})
t.Run("ExecutionMultiple", func(t *testing.T) {
ts := &testStruct{Counter: 0, Mutex: sync.Mutex{}}
ef := func(wh *WorkerHelper, args interface{}) {
ts := args.(*testStruct)
ts.Mutex.Lock()
ts.Counter = ts.Counter + 1
ts.Mutex.Unlock()
wh.Done()
}
w.SetExecution(ef)
w.SetParallelism(8)
w.Start(interface{}(ts))
w.Wait()
testAssert(t, ts.Counter == 8, "Execution failed: counter does not equal 8 (equals %v)", ts.Counter)
})
})
}