-
Notifications
You must be signed in to change notification settings - Fork 0
/
existing_conn_test.go
64 lines (54 loc) · 1.65 KB
/
existing_conn_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
package channel
import (
"fmt"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/identity"
"github.com/stretchr/testify/require"
"net"
"testing"
"time"
)
func TestExistingConnWriteAndReply(t *testing.T) {
testAddr := "127.0.0.1:28433"
req := require.New(t)
l, err := net.Listen("tcp", testAddr)
req.NoError(err)
defer func() { req.NoError(l.Close()) }()
go func() {
for {
c, err := l.Accept()
if err != nil {
return
}
bindHandler := BindHandlerF(func(binding Binding) error {
binding.AddReceiveHandlerF(ContentTypePingType, func(m *Message, ch Channel) {
reply := NewResult(true, string(m.Body))
reply.ReplyTo(m)
if err := reply.WithTimeout(time.Second).SendAndWaitForWire(ch); err != nil {
pfxlog.Logger().WithError(err).Error("unable to send reply")
}
})
return nil
})
chListener := NewExistingConnListener(&identity.TokenId{Token: "listener"}, c, nil)
_, err = NewChannel("existing.server", chListener, bindHandler, nil)
req.NoError(err)
}
}()
options := DefaultOptions()
options.ConnectTimeout = time.Second
options.WriteTimeout = 100 * time.Millisecond
conn, err := net.Dial("tcp", testAddr)
req.NoError(err)
dialer := NewExistingConnDialer(&identity.TokenId{Token: "dialer"}, conn, nil)
ch, err := NewChannel("existing.client", dialer, nil, options)
req.NoError(err)
defer func() { req.NoError(ch.Close()) }()
for i := 0; i < 10; i++ {
msg := NewMessage(ContentTypePingType, []byte(fmt.Sprintf("hello-%v", i)))
reply, err := msg.WithTimeout(time.Second).SendForReply(ch)
req.NoError(err)
req.NotNil(reply)
req.Equal(string(msg.Body), string(reply.Body))
}
}