-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathccall_test.go
50 lines (42 loc) · 1.06 KB
/
ccall_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
package ccall
import (
"context"
"errors"
"sync/atomic"
"testing"
)
// TestCallConcurrently_Success tests calling multiple functions concurrently successfully.
func TestCallConcurrently_Success(t *testing.T) {
var accum atomic.Int32
var fns []CallConcurrentlyFunc
for i := int32(0); i < 10; i++ {
x := i // copy value
fns = append(fns, func(ctx context.Context) error {
accum.Add(x)
return nil
})
}
if err := CallConcurrently(context.Background(), fns...); err != nil {
t.Fatal(err.Error())
}
if val := accum.Load(); val != 45 {
t.Fatalf("expected 45 but got %d", val)
}
}
// TestCallConcurrently_Err tests calling multiple functions with an error.
func TestCallConcurrently_Err(t *testing.T) {
errRet := errors.New("test error")
var fns []CallConcurrentlyFunc
for i := 0; i < 10; i++ {
i := i
fns = append(fns, func(ctx context.Context) error {
if i == 5 || i == 8 {
return errRet
}
return nil
})
}
if err := CallConcurrently(context.Background(), fns...); err != errRet {
t.Fatalf("expected error but got %v", err)
}
}