Repository navigation
Expand file tree
/
Copy pathconnection_test.go
More file actions
116 lines (104 loc) · 3.47 KB
/
Copy pathconnection_test.go
File metadata and controls
116 lines (104 loc) · 3.47 KB
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
package chancon
import (
"bytes"
"encoding/json"
"errors"
"net"
"testing"
"time"
"github.com/google/uuid"
)
func TestSendAndWaitRegistersObserverBeforeSending(t *testing.T) {
networkConnection := &immediateReplyConnection{}
connection := NewConnection(networkConnection, newObserverManager())
networkConnection.connection = connection
request := &Message{
Id: uuid.New(),
Channel: Channel{
Name: "test",
},
Date: time.Now(),
}
reply, err := connection.SendAndWaitForReplyWithTimeout(request, 100*time.Millisecond)
if err != nil {
t.Fatalf("SendAndWaitForReplyWithTimeout() error = %v", err)
}
if reply.ReplyTo != request.Id {
t.Fatalf("reply_to = %s, want %s", reply.ReplyTo, request.Id)
}
}
func TestSendReturnsAfterWriteFailure(t *testing.T) {
expectedError := errors.New("write failed")
networkConnection := &failingWriteConnection{
writeError: expectedError,
closed: make(chan struct{}),
}
connection := NewConnection(networkConnection, newObserverManager())
result := make(chan error, 1)
go func() {
result <- connection.Send(&Message{
Id: uuid.New(),
Channel: Channel{
Name: "test",
},
Date: time.Now(),
})
}()
select {
case err := <-result:
if !errors.Is(err, expectedError) {
t.Fatalf("Send() error = %v, want %v", err, expectedError)
}
case <-time.After(time.Second):
t.Fatal("Send() deadlocked after the connection returned a write error")
}
select {
case <-networkConnection.closed:
case <-time.After(time.Second):
t.Fatal("Send() did not close the connection after a write error")
}
}
type failingWriteConnection struct {
writeError error
closed chan struct{}
}
func (*failingWriteConnection) Read([]byte) (int, error) { return 0, errors.New("not implemented") }
func (connection *failingWriteConnection) Write([]byte) (int, error) {
return 0, connection.writeError
}
func (connection *failingWriteConnection) Close() error {
select {
case <-connection.closed:
default:
close(connection.closed)
}
return nil
}
func (*failingWriteConnection) LocalAddr() net.Addr { return testAddress("local") }
func (*failingWriteConnection) RemoteAddr() net.Addr { return testAddress("remote") }
func (*failingWriteConnection) SetDeadline(time.Time) error { return nil }
func (*failingWriteConnection) SetReadDeadline(time.Time) error { return nil }
func (*failingWriteConnection) SetWriteDeadline(time.Time) error { return nil }
type immediateReplyConnection struct {
connection *connection
}
func (*immediateReplyConnection) Read([]byte) (int, error) { return 0, errors.New("not implemented") }
func (networkConnection *immediateReplyConnection) Write(payload []byte) (int, error) {
request := new(Message)
if err := json.Unmarshal(bytes.TrimSpace(payload), request); err != nil {
return 0, err
}
networkConnection.connection.handleMessage(&Message{
Id: uuid.New(),
ReplyTo: request.Id,
Channel: request.Channel,
Date: time.Now(),
})
return len(payload), nil
}
func (*immediateReplyConnection) Close() error { return nil }
func (*immediateReplyConnection) LocalAddr() net.Addr { return testAddress("local") }
func (*immediateReplyConnection) RemoteAddr() net.Addr { return testAddress("remote") }
func (*immediateReplyConnection) SetDeadline(time.Time) error { return nil }
func (*immediateReplyConnection) SetReadDeadline(time.Time) error { return nil }
func (*immediateReplyConnection) SetWriteDeadline(time.Time) error { return nil }