-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacc_test.go
63 lines (54 loc) · 1.24 KB
/
acc_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
package acc
import (
"context"
"testing"
"time"
)
func TestAccumulatorWithStartTime(t *testing.T) {
ctx := context.Background()
acc := New(Config[int]{
Processor: func(ctx context.Context, data []int) {
for _, d := range data {
t.Logf("Processed Data: %d\n", d)
}
},
StartTime: time.Now().Add(2 * time.Minute),
})
go func() {
if err := acc.Start(ctx); err != nil {
t.Errorf("Error starting accumulator: %v", err)
}
}()
id, err := acc.Add(ctx, 42)
if err != nil {
t.Errorf("Error adding data: %v", err)
}
if err := acc.Cancel(ctx, id); err != nil {
t.Errorf("Error canceling data: %v", err)
}
time.Sleep(1 * time.Second)
}
func TestAccumulatorWithInterval(t *testing.T) {
ctx := context.Background()
acc := New[int](Config[int]{
Processor: func(ctx context.Context, data []int) {
for _, d := range data {
t.Logf("Processed Data: %d\n", d)
}
},
Interval: 1 * time.Second,
})
go func() {
if err := acc.Start(ctx); err != nil {
t.Errorf("Error starting accumulator: %v", err)
}
}()
id, err := acc.Add(ctx, 42)
if err != nil {
t.Errorf("Error adding data: %v", err)
}
if err := acc.Cancel(ctx, id); err != nil {
t.Errorf("Error canceling data: %v", err)
}
time.Sleep(1 * time.Second)
}