-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver_test.go
111 lines (96 loc) · 2.61 KB
/
server_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
package nrelay
import (
"context"
"log"
"sync/atomic"
"testing"
"github.com/octu0/chanque"
"github.com/pkg/errors"
)
type testServerRunRelays_RelayWithError struct {
msg string
}
func (r *testServerRunRelays_RelayWithError) Run(context.Context) error {
return errors.New(r.msg)
}
type testServerRunRelays_RelayWithNoError struct {
c *int32
}
func (r *testServerRunRelays_RelayWithNoError) Run(context.Context) error {
atomic.AddInt32(r.c, 1)
return nil
}
type testServerLogWriter struct {
t *testing.T
}
func (w *testServerLogWriter) Write(p []byte) (int, error) {
if testing.Verbose() {
w.t.Logf("%s", p)
}
return len(p), nil
}
func TestServerRunRelays(t *testing.T) {
t.Run("runErr/noCtxDone", func(tt *testing.T) {
e := chanque.NewExecutor(10, 10)
tt.Cleanup(func() { e.Release() })
lg := log.New(&testServerLogWriter{tt}, tt.Name()+"@", log.LstdFlags)
relays := []Relay{
&testServerRunRelays_RelayWithError{"err1"},
&testServerRunRelays_RelayWithError{"err2"},
&testServerRunRelays_RelayWithError{"err3"},
}
err := runRelays(context.TODO(), e, lg, relays)
if err != nil {
switch err.Error() {
case "err1", "err2", "err3":
// ok
default:
tt.Errorf("expect:['err1' or 'err2' or 'err3'] actual:%s", err.Error())
}
} else {
tt.Errorf("must error")
}
})
t.Run("runErrCombine/noCtxDone", func(tt *testing.T) {
e := chanque.NewExecutor(10, 10)
tt.Cleanup(func() { e.Release() })
counter := int32(0)
lg := log.New(&testServerLogWriter{tt}, tt.Name()+"@", log.LstdFlags)
relays := []Relay{
&testServerRunRelays_RelayWithNoError{&counter},
&testServerRunRelays_RelayWithError{"err2"},
&testServerRunRelays_RelayWithNoError{&counter},
}
err := runRelays(context.TODO(), e, lg, relays)
if err != nil {
if err.Error() != "err2" {
tt.Errorf("expect:err2 actual:%s", err.Error())
}
} else {
tt.Errorf("must error")
}
if atomic.LoadInt32(&counter) != 2 {
tt.Errorf("Run() called twice: %d", counter)
}
})
t.Run("runNoErr/ctxDone", func(tt *testing.T) {
e := chanque.NewExecutor(10, 10)
tt.Cleanup(func() { e.Release() })
lg := log.New(&testServerLogWriter{tt}, tt.Name()+"@", log.LstdFlags)
counter := int32(0)
relays := []Relay{
&testServerRunRelays_RelayWithNoError{&counter},
&testServerRunRelays_RelayWithNoError{&counter},
&testServerRunRelays_RelayWithNoError{&counter},
}
ctx, cancel := context.WithCancel(context.Background())
cancel() // already done
err := runRelays(ctx, e, lg, relays)
if err != nil {
tt.Errorf("must no error %v", err)
}
if counter != 3 {
tt.Errorf("Run() called 3 times")
}
})
}