-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathclient_test.go
131 lines (115 loc) · 3.39 KB
/
client_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
// Copyright 2018 Comcast Cable Communications Management, LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pulsar
import (
"context"
"io"
"testing"
"time"
"github.com/Comcast/pulsar-client-go/frame"
)
func TestClient_ServerInitiatedClose(t *testing.T) {
ctx, srvClose := context.WithCancel(context.Background())
defer srvClose()
srv, err := newMockPulsarServer(ctx)
if err != nil {
t.Fatal(err)
}
asyncErrs := make(chan error, 8)
c, err := NewClient(ClientConfig{
Addr: srv.addr,
Errs: asyncErrs,
})
if err != nil {
t.Fatal(err)
}
// wait for the Pulsar server to accept the connection
var srvConn *conn
select {
case srvConn = <-srv.conns:
t.Log("server received connection")
case <-time.After(time.Second):
t.Fatal("timeout waiting for server to receive connection")
}
// terminate the connection from the server's end
if err := srvConn.close(); err != nil {
t.Fatal(err)
}
select {
case err := <-asyncErrs:
if expected := io.EOF; err != expected {
t.Fatalf("Client.Errors() received unexpected err = %v; expected %v", err, expected)
}
t.Logf("Client.Errors() received (expected) err = %v", err)
case <-time.After(time.Second):
t.Fatal("timeout waiting for Client.Errors() to receive an error")
}
select {
case <-c.Closed():
t.Log("Client.OnClose() unblocked")
case <-time.After(time.Second):
t.Fatal("timeout waiting for Client.OnClose() to unblock")
}
}
func TestClient_ClientInitiatedClose(t *testing.T) {
ctx, srvClose := context.WithCancel(context.Background())
defer srvClose()
srv, err := newMockPulsarServer(ctx)
if err != nil {
t.Fatal(err)
}
asyncErrs := make(chan error, 8)
c, err := NewClient(ClientConfig{
Addr: srv.addr,
Errs: asyncErrs,
})
if err != nil {
t.Fatal(err)
}
// wait for the Pulsar server to accept the connection
var srvConn *conn
select {
case srvConn = <-srv.conns:
t.Log("server received connection")
case <-time.After(time.Second):
t.Fatal("timeout waiting for server to receive connection")
}
// start reading from the remote conn.
// send read errors to srvConnReadErr chan
srvConnReadErr := make(chan error, 1)
go func() {
srvConnReadErr <- srvConn.read(func(f frame.Frame) {})
}()
// terminate the connection from the client's end
if err := c.Close(); err != nil {
t.Fatalf("Client.Close() err = %v; expected nil", err)
}
select {
case err := <-asyncErrs:
t.Logf("Client.Errors() received (expected) err = %v", err)
case <-time.After(time.Second):
t.Fatal("timeout waiting for Client.Errors() to receive an error")
}
select {
case <-c.Closed():
t.Log("Client.OnClose() unblocked")
case <-time.After(time.Second):
t.Fatal("timeout waiting for Client.OnClose() to unblock")
}
select {
case err := <-srvConnReadErr:
t.Logf("remote conn read() unblocked with (expected) err = %v", err)
case <-time.After(time.Second):
t.Fatal("timeout waiting for remote conn read() to unblock")
}
}