-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions_test.go
90 lines (76 loc) · 2.07 KB
/
actions_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
package workering_test
import (
"context"
"strings"
"sync"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/bonsai-oss/workering/v2"
)
func testWorkerBuilder(input, output chan string) workering.WorkerFunction {
return func(ctx context.Context, done chan<- any) {
defer (func() { done <- "" })()
for {
select {
case <-ctx.Done():
return
case inputValue := <-input:
output <- strings.ToUpper(inputValue)
}
}
}
}
func TestWorker_Livecycle(t *testing.T) {
inputChannel := make(chan string)
outputChannel := make(chan string)
t.Run("without waiters", func(t *testing.T) {
workerName := uuid.New().String()
workering.Register(workering.RegisterSet{
Name: workerName,
Worker: testWorkerBuilder(inputChannel, outputChannel),
})
worker := workering.Get(workerName)
assert.Nil(t, worker.Start())
inputChannel <- "hello"
assert.Equal(t, "HELLO", <-outputChannel)
assert.Nil(t, worker.Stop())
})
t.Run("with waiters", func(t *testing.T) {
workerName := uuid.New().String()
workering.Register(workering.RegisterSet{
Name: workerName,
Worker: testWorkerBuilder(inputChannel, outputChannel),
})
worker := workering.Get(workerName)
assert.Nil(t, worker.Start())
inputChannel <- "hello"
assert.Equal(t, "HELLO", <-outputChannel)
wg := sync.WaitGroup{}
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
assert.NotNil(t, <-worker.WaitStopped())
}(&wg)
time.Sleep(10 * time.Millisecond)
assert.Nil(t, worker.Stop())
wg.Wait()
})
t.Run("explicit workerFunction reusing", func(t *testing.T) {
workerName := uuid.New().String()
workering.Register(workering.RegisterSet{
Name: workerName,
Worker: testWorkerBuilder(inputChannel, outputChannel),
})
worker := workering.Get(workerName)
assert.Nil(t, worker.Start())
inputChannel <- "hello"
assert.Equal(t, "HELLO", <-outputChannel)
assert.Nil(t, worker.Stop())
assert.Nil(t, worker.Start())
inputChannel <- "world"
assert.Equal(t, "WORLD", <-outputChannel)
assert.Nil(t, worker.Stop())
})
}