-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
140 lines (124 loc) · 2.81 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package tt
import (
"context"
"errors"
"io/ioutil"
"log"
"net"
"testing"
"time"
"github.com/gregoryv/tt/spec"
"github.com/gregoryv/tt/ttx"
)
func TestServer_SetConnectTimeout(t *testing.T) {
srv := NewServer()
srv.SetConnectTimeout(0)
}
func TestServer_SetDebugIncreasesLogging(t *testing.T) {
srv := NewServer()
l := log.New(ioutil.Discard, "", 0)
srv.SetLogger(l)
srv.SetDebug(true)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
t.Cleanup(cancel)
cancel()
srv.Run(ctx)
if l.Flags() == 0 {
t.Error("SetDebug did not change logger flags")
}
}
func TestServer_cancelContext(t *testing.T) {
srv := NewServer()
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
cancel()
srv.Run(ctx)
// should not block
}
func Test_connFeed(t *testing.T) {
{ // accepts connections
ctx, cancel := context.WithCancel(context.Background())
ln, _ := net.Listen("tcp", ":")
time.AfterFunc(3*time.Millisecond, func() {
conn, err := net.Dial("tcp", ln.Addr().String())
if err != nil {
t.Fatal(err)
}
conn.Close()
cancel()
})
f := connFeed{
Listener: ln,
AcceptTimeout: time.Millisecond,
feed: make(chan Connection, 1),
}
f.Run(ctx)
}
{ // ends on listener close
ln, _ := net.Listen("tcp", ":")
time.AfterFunc(time.Millisecond, func() { ln.Close() })
f := connFeed{
Listener: ln,
AcceptTimeout: time.Millisecond,
feed: make(chan Connection, 1),
}
err := f.Run(context.Background())
if !errors.Is(err, net.ErrClosed) {
t.Error(err)
}
}
}
func TestSubscription_String(t *testing.T) {
sub := mustNewSubscription("all/gophers/#", ttx.NoopPub)
if v := sub.String(); v != "sub 0: all/gophers/#" {
t.Errorf("unexpected subscription string %q", sub)
}
}
func TestMustNewSubscription(t *testing.T) {
defer func() {
if e := recover(); e == nil {
t.Fatal("expect panic")
}
}()
mustNewSubscription("")
}
func ExampleTopicFilter() {
_ = mustParseTopicFilter("/a/+/b/+/+")
}
func TestParseTopicFilter(t *testing.T) {
impl := func(filter string) bool {
err := parseTopicFilter(filter)
return err == nil
}
err := spec.VerifyFilterFormat(impl)
if err != nil {
t.Error(err)
}
}
func Test_parseTopicName(t *testing.T) {
bad := func(name string) {
t.Helper()
if err := parseTopicName(name); err == nil {
t.Errorf("topic name %q accepted, expected error", name)
}
}
bad("/+")
bad("+")
bad("#")
}
// ----------------------------------------
// mustNewSubscription panics on bad filter
func mustNewSubscription(filter string, handlers ...pubHandler) *subscription {
mustParseTopicFilter(filter)
sub := newSubscription(handlers...)
sub.addTopicFilter(filter)
return sub
}
func mustParseTopicFilter(v string) string {
err := parseTopicFilter(v)
if err != nil {
panic(err.Error())
}
return v
}