-
Notifications
You must be signed in to change notification settings - Fork 1
/
peer_test.go
100 lines (83 loc) · 1.88 KB
/
peer_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
// Copyright 2020 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zyre
import (
"context"
"testing"
zmq "github.com/go-zeromq/zmq4"
)
type mockPeerMailbox struct {
sequence uint16
}
func (p *mockPeerMailbox) connect(identity []byte, endpoint string) error {
return nil
}
func (p *mockPeerMailbox) close() {
}
func (p *mockPeerMailbox) send(m *ZreMsg) error {
p.sequence = m.sequence
return nil
}
func TestPeer(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
p := NewPeer(ctx, "peer1")
mailbox := &mockPeerMailbox{}
p.mailbox = mailbox
p.Connect([]byte("from"), "endpoint")
if !p.connected {
t.Error("peer not connected")
}
if p.ready {
t.Error("peer should not be ready")
}
_, ok := p.Header("no header")
if ok {
t.Error("want header not ok")
}
m := NewZreMsg(HelloID)
p.Send(m)
if mailbox.sequence != 1 {
t.Errorf("want sequence %v, got %v", 1, mailbox.sequence)
}
p.Disconnect()
if p.connected {
t.Error("peer still connected")
}
p.Destroy()
}
func TestPeerNetwork(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
mailbox := zmq.NewDealer(ctx)
defer mailbox.Close()
err := mailbox.Listen("tcp://127.0.0.1:5551")
if err != nil {
t.Fatal(err)
}
peer := NewPeer(ctx, "you")
err = peer.Connect([]byte("me"), "tcp://127.0.0.1:5551")
if err != nil {
t.Fatal(err)
}
if !peer.connected {
t.Fatal("Peer should be connected")
}
m := NewZreMsg(HelloID)
m.Endpoint = "tcp://127.0.0.1:5551"
peer.Send(m)
// FIXME
// exp, err := m.Marshal()
// if err != nil {
// t.Fatal(err)
// }
// msg, err := mailbox.Recv()
// if err != nil {
// t.Fatal(err)
// }
// if !bytes.Equal(msg.Bytes(), exp) {
// t.Error("Hello message was corrupted")
// }
peer.Destroy()
}