-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer_test.go
126 lines (101 loc) · 3.4 KB
/
buffer_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// -----------------------------------------------------------------------------
// worker for running tasks enqueued in background (buffer.go)
//
// Copyright (C) 2024 Frank Mueller / Oldenburg / Germany / World
// -----------------------------------------------------------------------------
package worker
import (
"context"
"fmt"
"testing"
"time"
"tideland.dev/go/audit/asserts"
)
// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------
// TestRatedBufferSetup tests the simple setup of a rated buffer.
func TestRatedBufferSetup(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in, out := setupRatedBuffer(ctx, 1, 1, time.Second)
assert.NotNil(in)
assert.NotNil(out)
}
// TestRatedBufferIn tests the input of tasks in order to process them.
func TestRatedBufferIn(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in, out := setupRatedBuffer(ctx, 100, 10, time.Second)
assert.NotNil(in)
assert.NotNil(out)
assert.NoError(in(makeProcessATask("A")))
assert.NoError(in(makeProcessATask("B")))
assert.NoError(in(makeProcessATask("C")))
}
// TestRatedBufferInOut tests the input and output of tasks.
func TestRatedBufferInOut(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
ctx, cancel := context.WithCancel(context.Background())
in, out := setupRatedBuffer(ctx, 2, 10, time.Second)
assert.NotNil(in)
assert.NotNil(out)
assert.NoError(in(makeProcessATask("A")))
assert.NoError(in(makeProcessATask("B")))
assert.NoError(in(makeProcessATask("C")))
assert.NoError(in(makeShutdownATask()))
for {
atask := <-out()
action, task := atask()
if action == actionShutdown {
break
}
assert.Logf("task error %q", task().Error())
}
cancel()
}
func TestRatedBufferRate(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
ctx, cancel := context.WithCancel(context.Background())
in, out := setupRatedBuffer(ctx, 2, 10, time.Second)
expected := 500 * time.Millisecond
assert.NotNil(in)
assert.NotNil(out)
assert.NoError(in(makeProcessATask("A")))
assert.NoError(in(makeProcessATask("B")))
assert.NoError(in(makeProcessATask("C")))
assert.NoError(in(makeProcessATask("D")))
assert.NoError(in(makeProcessATask("E")))
assert.NoError(in(makeShutdownATask()))
for {
now := time.Now()
atask := <-out()
action, task := atask()
duration := time.Since(now)
assert.Logf("duration %v", duration)
assert.About(float64(duration), float64(expected), float64(100*time.Millisecond))
if action == actionShutdown {
break
}
assert.Logf("task error %q", task().Error())
}
cancel()
}
// -----------------------------------------------------------------------------
// internal helper
// -----------------------------------------------------------------------------
func makeProcessATask(msg string) actionTask {
return func() (action, Task) {
return actionProcess, func() error { return fmt.Errorf(msg) }
}
}
func makeShutdownATask() actionTask {
return func() (action, Task) {
return actionShutdown, nil
}
}
// -----------------------------------------------------------------------------
// end of file
// -----------------------------------------------------------------------------