-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlrsc_test.go
191 lines (158 loc) · 4.52 KB
/
lrsc_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"errors"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"hub.jazz.net/git/bluemixgarage/lrsc-bridge/bridge"
"hub.jazz.net/git/bluemixgarage/lrsc-bridge/reporter"
"io"
)
var _ = Describe("LRSC Bridge", func() {
It("validates handshake", func() {
RegisterTestingT(GinkgoT())
response := "some_handshake_response\n"
Expect(validateHandshake(response)).To(Equal(true))
})
It("reconnects", func() {
count := 0
connectionAttempts := 0
mockConn := &mockConnection{
readFunc: func() (string, error) {
if count == 0 {
count += 1
return "", errors.New("EOF")
} else {
return "{}\n", nil
}
},
writeFunc: func(message string) error {
if message == "JSON_000" {
connectionAttempts += 1
}
return nil
}}
testDialer := &testDialer{conn: mockConn}
lrscClient := &lrscConnection{dialer: testDialer}
lrscClient.StatusReporter = reporter.New()
lrscClient.inbound = make(chan lrscMessage)
go runConnectionLoop("LRSC Client", lrscClient)
messages := lrscClient.inbound
<-messages
Expect(connectionAttempts).To(Equal(2))
})
It("can receive a message", func() {
mockConn := &mockConnection{
readFunc: func() (string, error) {
return `{"deveui": "id", "pdu": "data"}` + "\n", nil
},
writeFunc: func(string) error {
return nil
},
}
testDialer := &testDialer{conn: mockConn}
lrscClient := &lrscConnection{dialer: testDialer}
lrscClient.StatusReporter = reporter.New()
lrscClient.inbound = make(chan lrscMessage)
go runConnectionLoop("LRSC Client", lrscClient)
messages := lrscClient.inbound
Expect(<-messages).To(Equal(lrscMessage{DeviceGuid: "id", Payload: "data"}))
})
It("reports an error if connection fails", func() {
failingDialer := &failingDialer{}
lrscClient := &lrscConnection{dialer: failingDialer}
lrscClient.StatusReporter = reporter.New()
lrscClient.establish()
Expect(lrscClient.Summary()).To(Equal(`{"CONNECTION":"FAILED"}`))
})
It("writes command message", func() {
written := ""
mockConn := &mockConnection{
readFunc: func() (string, error) {
return "", nil
},
writeFunc: func(s string) error {
written = s
return nil
},
}
lrscClient := lrscConnection{conn: mockConn}
lrscClient.sendCommand(bridge.Command{Device: "device", Payload: "payload"})
message, err := parseLrscMessage(written)
if err != nil {
panic(err)
}
Expect(message.DeviceGuid).To(Equal("device"))
Expect(message.Payload).To(Equal("payload"))
Expect(message.Type).To(Equal(messageTypeDownstream))
Expect(message.UniqueSequenceNo).To(BeEquivalentTo(1))
Expect(message.Port).To(BeEquivalentTo(10))
Expect(message.Mode).To(Equal(messageModeUnconfirmed))
})
It("increases sequence number", func() {
written := ""
mockConn := &mockConnection{
readFunc: func() (string, error) {
return "", nil
},
writeFunc: func(s string) error {
written = s
return nil
},
}
lrscClient := lrscConnection{conn: mockConn}
for i := 1; i <= 5; i++ {
lrscClient.sendCommand(bridge.Command{})
message, err := parseLrscMessage(written)
if err != nil {
panic(err)
}
Expect(message.UniqueSequenceNo).To(BeEquivalentTo(i))
}
})
Describe("converting commands to LRSC downstream messages", func() {
lrscMessage := convertCommandToLrscDownstreamMessage(bridge.Command{Device: "AA-AA", Payload: "payload"})
It("message type is downstream", func() {
Expect(lrscMessage.Type).To(Equal(messageTypeDownstream))
})
It("uses device id from IoTF command", func() {
Expect(lrscMessage.DeviceGuid).To(Equal("AA-AA"))
})
It("uses payload from IoTF command", func() {
Expect(lrscMessage.Payload).To(Equal("payload"))
})
})
})
type testDialer struct {
conn io.ReadWriteCloser
}
func (self testDialer) dial() (io.ReadWriteCloser, error) {
return self.conn, nil
}
func (self testDialer) endpoint() string {
return "/dev/null"
}
type failingDialer struct {
}
func (self failingDialer) dial() (io.ReadWriteCloser, error) {
return nil, errors.New("FAILED")
}
func (self failingDialer) endpoint() string {
return "/dev/null"
}
type mockConnection struct {
readFunc func() (string, error)
writeFunc func(string) error
reporter.StatusReporter
}
func (self *mockConnection) Read(b []byte) (n int, err error) {
response, err := self.readFunc()
copy(b, response)
return len(response), err
}
func (self *mockConnection) Write(b []byte) (n int, err error) {
err = self.writeFunc(string(b))
return len(b), err
}
func (self *mockConnection) Close() error {
return nil
}