This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
865 lines (695 loc) · 21.4 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
package main
import (
"bufio"
"encoding/json"
"fmt"
"net"
"net/http/httptest"
"reflect"
"strings"
"sync"
"testing"
"time"
"github.com/gorilla/websocket"
)
type dummyRFID struct {
mu sync.RWMutex
ln net.Listener
c net.Conn
incoming chan []byte
}
func (d *dummyRFID) reader() {
r := bufio.NewReader(d.c)
for {
msg, err := r.ReadBytes('\r')
if err != nil {
close(d.incoming)
break
}
d.incoming <- msg
}
}
func (d *dummyRFID) write(msg []byte) {
d.mu.Lock()
defer d.mu.Unlock()
w := bufio.NewWriter(d.c)
_, err := w.Write(msg)
if err != nil {
println(err.Error())
return
}
err = w.Flush()
if err != nil {
println(err.Error())
return
}
}
func (d *dummyRFID) run() {
defer d.ln.Close()
c, err := d.ln.Accept()
if err != nil {
println(err.Error())
return
}
d.c = c
d.reader()
}
func (d *dummyRFID) addr() string {
d.mu.RLock()
defer d.mu.RUnlock()
addr := "http://" + d.ln.Addr().String()
return addr
}
func (d *dummyRFID) Close() {
d.mu.Lock()
defer d.mu.Unlock()
if d.c != nil {
d.c.Close()
}
if d.ln != nil {
d.ln.Close()
}
}
func newDummyRFIDReader() *dummyRFID {
d := dummyRFID{
incoming: make(chan []byte),
}
var err error
d.mu.Lock()
d.ln, err = net.Listen("tcp", ":0")
d.mu.Unlock()
if err != nil {
println(err.Error())
panic("Cannot start dummy RFID TCP-server")
}
go d.run()
return &d
}
type dummyUIAgent struct {
c *websocket.Conn
msg chan Message
}
func newDummyUIAgent(msg chan Message, port string) *dummyUIAgent {
d := dummyUIAgent{
msg: msg,
}
ws, _, err := websocket.DefaultDialer.Dial(fmt.Sprintf("ws://localhost:%s/ws", port), nil)
if err != nil {
panic(fmt.Sprintf("Cannot connect to ws://localhost:%s/ws: %v", port, err.Error()))
}
d.c = ws
go d.run()
return &d
}
func (a *dummyUIAgent) run() {
for {
_, msg, err := a.c.ReadMessage()
if err != nil {
break
}
var m Message
err = json.Unmarshal(msg, &m)
if err != nil {
break
}
a.msg <- m
}
}
func port(s string) string {
return s[strings.LastIndex(s, ":")+1:]
}
func TestMissingRFIDUnit(t *testing.T) {
// Setup: ->
uiChan := make(chan Message)
sipSrv := newSIPTestServer()
defer sipSrv.Close()
srv := httptest.NewServer(nil)
defer srv.Close()
hub = newHub(Config{
HTTPPort: port(srv.URL),
SIPServer: sipSrv.Addr(),
RFIDPort: "12346", // not listening
RFIDTimeout: 1 * time.Second,
})
defer hub.Close()
a := newDummyUIAgent(uiChan, port(srv.URL))
defer a.c.Close()
// <- end setup
msg := <-uiChan
want := Message{Action: "CONNECT", RFIDError: true,
ErrorMessage: fmt.Sprintf("dial tcp 127.0.0.1:%s: connect: connection refused", hub.config.RFIDPort)}
if !reflect.DeepEqual(msg, want) {
t.Errorf("Got %+v; want %+v", msg, want)
t.Fatal("UI didn't get notified of failed RFID connect")
}
}
func TestRFIDUnitInitVersionFailure(t *testing.T) {
// Setup: ->
uiChan := make(chan Message)
sipSrv := newSIPTestServer()
defer sipSrv.Close()
srv := httptest.NewServer(nil)
defer srv.Close()
d := newDummyRFIDReader()
defer d.Close()
hub = newHub(Config{
HTTPPort: port(srv.URL),
SIPServer: sipSrv.Addr(),
RFIDPort: port(d.addr()),
RFIDTimeout: 1 * time.Second,
})
defer hub.Close()
a := newDummyUIAgent(uiChan, port(srv.URL))
defer a.c.Close()
// <- end setup
if msg := <-d.incoming; string(msg) != "VER2.00\r" {
t.Fatal("RFID-unit didn't get version init command")
}
d.write([]byte("NOK\r"))
got := <-uiChan
want := Message{Action: "CONNECT", RFIDError: true, ErrorMessage: "RFID-unit responded with NOK"}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
t.Fatal("UI didn't get notified of failed RFID connect")
}
}
func TestUnavailableSIPServer(t *testing.T) {
// Setup: ->
uiChan := make(chan Message)
sipSrv := newSIPTestServer().Failing()
defer sipSrv.Close()
srv := httptest.NewServer(nil)
defer srv.Close()
d := newDummyRFIDReader()
defer d.Close()
hub = newHub(Config{
HTTPPort: port(srv.URL),
SIPServer: sipSrv.Addr(),
RFIDPort: port(d.addr()),
RFIDTimeout: 1 * time.Second,
})
defer hub.Close()
a := newDummyUIAgent(uiChan, port(srv.URL))
defer a.c.Close()
// <- end setup
if msg := <-d.incoming; string(msg) != "VER2.00\r" {
t.Fatal("RFID-unit didn't get version init command")
}
d.write([]byte("OK\r"))
<-uiChan
if err := a.c.WriteMessage(websocket.TextMessage, []byte(`{"Action":"CHECKIN"}`)); err != nil {
t.Fatal("UI failed to send message over websokcet conn")
}
if msg := <-d.incoming; string(msg) != "BEG\r" {
t.Fatal("UI -> CHECKIN: RFID-unit didn't get instructed to start scanning")
}
d.write([]byte("OK\r"))
d.write([]byte("RDT1003010824124004:NO:02030000|1\r"))
got := <-uiChan
got.ErrorMessage = "" // No way to know the os-assigned port number in error message
want := Message{Action: "CONNECT", SIPError: true}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
t.Fatal("UI didn't get notified of SIP error")
}
}
func TestCheckins(t *testing.T) {
// Setup: ->
uiChan := make(chan Message)
sipSrv := newSIPTestServer()
defer sipSrv.Close()
srv := httptest.NewServer(nil)
defer srv.Close()
d := newDummyRFIDReader()
defer d.Close()
hub = newHub(Config{
HTTPPort: port(srv.URL),
SIPServer: sipSrv.Addr(),
RFIDPort: port(d.addr()),
RFIDTimeout: 1 * time.Second,
})
defer hub.Close()
a := newDummyUIAgent(uiChan, port(srv.URL))
defer a.c.Close()
// <- end setup
if msg := <-d.incoming; string(msg) != "VER2.00\r" {
t.Fatal("RFID-unit didn't get version init command")
}
d.write([]byte("OK\r"))
// Verify that UI get's CONNECT message
got := <-uiChan
want := Message{Action: "CONNECT"}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
t.Fatal("UI didn't get notified of succesfull rfid connect")
}
// Send "CHECKIN" message from UI and verify that the UI gets notified of
// succesfull connect & RFID-unit that gets instructed to starts scanning for tags.
err := a.c.WriteMessage(websocket.TextMessage, []byte(`{"Action":"CHECKIN","Branch":"fmaj"}`))
if err != nil {
t.Fatal("UI failed to send message over websokcet conn")
}
if msg := <-d.incoming; string(msg) != "BEG\r" {
t.Fatal("UI -> CHECKIN: RFID-unit didn't get instructed to start scanning")
}
// acknowledge BEG command
d.write([]byte("OK\r"))
// Simulate found book on RFID-unit. Verify that it get's checked in through
// SIP, the Alarm turned on, and that UI get's notified of the transaction
sipSrv.Respond("101YNN20140226 161239AO|AB03010824124004|AQfhol|AJHeavy metal in Baghdad|CTfbol|AA2|CS927.8|\r")
d.write([]byte("RDT1003010824124004:NO:02030000|0\r"))
if msg := <-d.incoming; string(msg) != "OK1\r" {
t.Errorf("Checkin: RFID reader didn't get instructed to turn on alarm")
}
// simulate failed alarm command
d.write([]byte("NOK\r"))
got = <-uiChan
want = Message{Action: "CHECKIN",
Item: Item{
Label: "Heavy metal in Baghdad",
Barcode: "03010824124004",
Date: "26/02/2014",
AlarmOnFailed: true,
Transfer: "fbol",
Status: "Feil: fikk ikke skrudd på alarm.",
}}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
t.Fatal("UI didn't get the correct message after checkin")
}
// retry alarm on
err = a.c.WriteMessage(websocket.TextMessage, []byte(`{"Action":"RETRY-ALARM-ON"}`))
if err != nil {
t.Fatal("UI failed to send message over websokcet conn")
}
if msg := <-d.incoming; string(msg) != "ACT1003010824124004:NO:02030000\r" {
t.Fatal("UI -> RETRY-ALARM-ON didn't trigger the right RFID command")
}
d.write([]byte("OK\r"))
got = <-uiChan
want = Message{Action: "CHECKIN",
Item: Item{
Label: "Heavy metal in Baghdad",
Barcode: "03010824124004",
Date: "26/02/2014",
}}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
t.Fatal("UI didn't get the correct message after checkin retry alarm on")
}
// Simulate barcode not in our db
sipSrv.Respond("100NUY20140128 114702AO|AB1234|CV99|AFItem not checked out|\r")
d.write([]byte("RDT1234:NO:02030000|0\r"))
if msg := <-d.incoming; string(msg) != "OK \r" {
t.Errorf("Alarm was changed after unsuccessful checkin")
}
d.write([]byte("OK\r"))
got = <-uiChan
want = Message{Action: "CHECKIN",
Item: Item{
Barcode: "1234",
TransactionFailed: true,
Unknown: true,
Status: "eksemplaret finnes ikke i basen",
}}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
}
// Simulate book on RFID-unit, but with missing tags. Verify that UI gets
// notified with the books title, along with an error message
sipSrv.Respond("1803020120140226 203140AB03010824124004|AO|AJHeavy metal in Baghdad|AQfhol|BGfhol|\r")
d.write([]byte("RDT1003010824124004:NO:02030000|1\r"))
if msg := <-d.incoming; string(msg) != "OK \r" {
t.Error("Alarm was changed after unsuccessful checkin")
}
d.write([]byte("OK\r"))
got = <-uiChan
want = Message{Action: "CHECKIN",
Item: Item{
Label: "Heavy metal in Baghdad",
Barcode: "03010824124004",
TransactionFailed: true,
}}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
t.Fatal("UI didn't get the correct message when item is missing tags")
}
// Verify that the RFID-unit gets END message when the corresponding
// websocket connection is closed.
// TODO I'm closing connection before, is this necessary?
// msg = <-d.incoming
// if string(msg) != "END\r" {
// t.Fatal("RFID-unit didn't get END message when UI connection was lost")
// }
}
func TestCheckouts(t *testing.T) {
// setup ->
uiChan := make(chan Message)
sipSrv := newSIPTestServer()
defer sipSrv.Close()
srv := httptest.NewServer(nil)
defer srv.Close()
d := newDummyRFIDReader()
defer d.Close()
hub = newHub(Config{
HTTPPort: port(srv.URL),
SIPServer: sipSrv.Addr(),
RFIDPort: port(d.addr()),
LogSIPMessages: true,
RFIDTimeout: 1 * time.Second,
})
defer hub.Close()
a := newDummyUIAgent(uiChan, port(srv.URL))
defer a.c.Close()
// <- end setup
// TESTS /////////////////////////////////////////////////////////////////
if msg := <-d.incoming; string(msg) != "VER2.00\r" {
t.Fatal("RFID-unit didn't get version init command")
}
d.write([]byte("OK\r"))
got := <-uiChan
want := Message{Action: "CONNECT"}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
t.Fatal("UI didn't get notified of succesfull rfid connect")
}
// Send "CHECKOUT" message from UI and verify that the UI gets notified of
// succesfull connect & RFID-unit that gets instructed to starts scanning for tags.
if err := a.c.WriteMessage(
websocket.TextMessage,
[]byte(`{"Action":"CHECKOUT", "Patron": "95", "Branch":"hutl"}`)); err != nil {
t.Fatal("UI failed to send message over websokcet conn")
}
if msg := <-d.incoming; string(msg) != "BEG\r" {
t.Fatal("UI -> CHECKOUT: RFID-unit didn't get instructed to start scanning")
}
// acknowledge BEG command
d.write([]byte("OK\r"))
// Simulate book on RFID-unit, but SIP show that book is already checked out.
// Verify that UI gets notified with the books title, along with an error message
sipSrv.Respond("120NUN20140303 102741AOHUTL|AA95|AB03011174511003|AJKrutt-Kim|AH|AFItem checked out to another patron|BLY|\r")
d.write([]byte("RDT1003011174511003:NO:02030000|0\r"))
if msg := <-d.incoming; string(msg) != "OK \r" {
t.Errorf("Alarm was changed after unsuccessful checkout")
}
d.write([]byte("OK\r"))
got = <-uiChan
want = Message{Action: "CHECKOUT",
Item: Item{
Label: "Krutt-Kim",
Barcode: "03011174511003",
TransactionFailed: true,
Status: "Item checked out to another patron",
}}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
t.Fatal("UI didn't get the correct message when item is already checked out to another patron")
}
// Test successful checkout
sipSrv.Respond("121NNY20140303 110236AOHUTL|AA95|AB03011063175001|AJCat's cradle|AH20140331 235900|\r")
d.write([]byte("RDT1003011063175001:NO:02030000|0\r"))
if msg := <-d.incoming; string(msg) != "OK0\r" {
t.Errorf("Alarm was not turned off after successful checkout")
}
// simulate failed alarm
d.write([]byte("NOK\r"))
got = <-uiChan
want = Message{Action: "CHECKOUT",
Item: Item{
Label: "Cat's cradle",
Barcode: "03011063175001",
Date: "31/03/2014",
AlarmOffFailed: true,
Status: "Feil: fikk ikke skrudd av alarm.",
}}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
t.Fatal("UI didn't get the correct message after succesfull checkout")
}
// retry alarm off
if err := a.c.WriteMessage(
websocket.TextMessage, []byte(`{"Action":"RETRY-ALARM-OFF"}`)); err != nil {
t.Fatal("UI failed to send message over websokcet conn")
}
if msg := <-d.incoming; string(msg) != "DAC1003011063175001:NO:02030000\r" {
t.Errorf("Got %q, Want DAC1003011063175001:NO:02030000", msg)
t.Fatal("UI -> RETRY-ALARM-ON didn't trigger the right RFID command")
}
d.write([]byte("OK\r"))
got = <-uiChan
want = Message{Action: "CHECKOUT",
Item: Item{
Label: "Cat's cradle",
Barcode: "03011063175001",
Date: "31/03/2014",
}}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
t.Fatal("UI didn't get the correct message after succesfull checkout")
}
}
// Test that rereading of items with missing tags doesn't trigger multiple SIP-calls
func TestBarcodesSession(t *testing.T) {
// setup ->
uiChan := make(chan Message)
sipSrv := newSIPTestServer()
defer sipSrv.Close()
srv := httptest.NewServer(nil)
defer srv.Close()
d := newDummyRFIDReader()
defer d.Close()
hub = newHub(Config{
HTTPPort: port(srv.URL),
SIPServer: sipSrv.Addr(),
RFIDPort: port(d.addr()),
RFIDTimeout: 1 * time.Second,
})
defer hub.Close()
a := newDummyUIAgent(uiChan, port(srv.URL))
defer a.c.Close()
// <- end setup
if msg := <-d.incoming; string(msg) != "VER2.00\r" {
t.Fatal("RFID-unit didn't get version init command")
}
d.write([]byte("OK\r"))
<-uiChan // CONNECT OK
if err := a.c.WriteMessage(websocket.TextMessage, []byte(`{"Action":"CHECKIN"}`)); err != nil {
t.Fatal("UI failed to send message over websokcet conn")
}
<-d.incoming
d.write([]byte("OK\r"))
sipSrv.Respond("1803020120140226 203140AB03010824124004|AJHeavy metal in Baghdad|AQfhol|BGfhol|\r")
d.write([]byte("RDT1003010824124004:NO:02030000|1\r"))
<-d.incoming
d.write([]byte("OK\r"))
<-uiChan
d.write([]byte("RDT1003010824124004:NO:02030000|1\r"))
<-d.incoming
d.write([]byte("OK\r"))
if got := <-uiChan; got.SIPError {
t.Fatalf("Rereading of failed tags triggered multiple SIP-calls")
}
}
func TestWriteLogic(t *testing.T) {
// setup ->
uiChan := make(chan Message)
sipSrv := newSIPTestServer()
defer sipSrv.Close()
srv := httptest.NewServer(nil)
defer srv.Close()
d := newDummyRFIDReader()
defer d.Close()
hub = newHub(Config{
HTTPPort: port(srv.URL),
SIPServer: sipSrv.Addr(),
RFIDPort: port(d.addr()),
RFIDTimeout: 1 * time.Second,
})
defer hub.Close()
a := newDummyUIAgent(uiChan, port(srv.URL))
defer a.c.Close()
// <- end setup
sipSrv.Respond("1803020120140226 203140AB03010824124004|AJHeavy metal in Baghdad|AQfhol|BGfhol|\r")
if msg := <-d.incoming; string(msg) != "VER2.00\r" {
t.Fatal("RFID-unit didn't get version init command")
}
d.write([]byte("OK\r"))
<-uiChan // CONNECT OK
if err := a.c.WriteMessage(websocket.TextMessage,
[]byte(`{"Action":"ITEM-INFO", "Item": {"Barcode": "03010824124004"}}`)); err != nil {
t.Fatal("UI failed to send message over websokcet conn")
}
if msg := <-d.incoming; string(msg) != "TGC\r" {
t.Fatal("item-info didn't trigger RFID TagCount")
}
d.write([]byte("OK|2\r"))
got := <-uiChan
want := Message{Action: "ITEM-INFO",
Item: Item{
Label: "Heavy metal in Baghdad",
Barcode: "03010824124004",
NumTags: 2,
}}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
t.Fatal("UI didn't get the correct item info ")
}
// 1. failed write
if err := a.c.WriteMessage(websocket.TextMessage,
[]byte(`{"Action":"WRITE", "Item": {"Barcode": "03010824124004", "NumTags": 2}}`)); err != nil {
t.Fatal("UI failed to send message over websokcet conn")
}
if msg := <-d.incoming; string(msg) != "SLPLBN|02030000\r" {
t.Fatal("WRITE command didn't initialize the reader properly")
}
d.write([]byte("NOK\r"))
got = <-uiChan
want = Message{Action: "WRITE",
Item: Item{
Label: "Heavy metal in Baghdad",
Barcode: "03010824124004",
WriteFailed: true,
NumTags: 2,
}}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
t.Fatal("UI didn't get the correct message of failed write ")
}
// 2. succesfull write
if err := a.c.WriteMessage(websocket.TextMessage,
[]byte(`{"Action":"WRITE", "Item": {"Barcode": "03010824124004", "NumTags": 2}}`)); err != nil {
t.Fatal("UI failed to send message over websokcet conn")
}
if msg := <-d.incoming; string(msg) != "SLPLBN|02030000\r" {
t.Fatal("WRITE command didn't initialize the reader properly")
}
d.write([]byte("OK\r"))
if msg := <-d.incoming; string(msg) != "SLPLBC|NO\r" {
t.Fatal("WRITE command didn't initialize the reader properly")
}
d.write([]byte("OK\r"))
if msg := <-d.incoming; string(msg) != "SLPDTM|DS24\r" {
t.Fatal("WRITE command didn't initialize the reader properly")
}
d.write([]byte("OK\r"))
if msg := <-d.incoming; string(msg) != "SLPSSB|0\r" {
t.Fatal("WRITE command didn't initialize the reader properly")
}
d.write([]byte("OK\r"))
if msg := <-d.incoming; string(msg) != "SLPCRD|1\r" {
t.Fatal("WRITE command didn't initialize the reader properly")
}
d.write([]byte("OK\r"))
if msg := <-d.incoming; string(msg) != "SLPWTM|5000\r" {
t.Fatal("WRITE command didn't initialize the reader properly")
}
d.write([]byte("OK\r"))
if msg := <-d.incoming; string(msg) != "SLPRSS|1\r" {
t.Fatal("WRITE command didn't initialize the reader properly")
}
d.write([]byte("OK\r"))
if msg := <-d.incoming; string(msg) != "TGC\r" {
t.Fatal("WRITE command didn't initialize the reader properly")
}
d.write([]byte("OK|2\r"))
if msg := <-d.incoming; string(msg) != "WRT03010824124004|2|0\r" {
t.Fatal("Reader didn't get the right Write command")
}
d.write([]byte("OK|E004010046A847AD|E004010046A847AD\r"))
got = <-uiChan
want = Message{Action: "WRITE",
Item: Item{
Label: "Heavy metal in Baghdad",
Barcode: "03010824124004",
NumTags: 2,
Status: "OK, preget",
}}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
t.Fatal("UI didn't get the correct item info after WRITE command ")
}
}
func TestUserErrors(t *testing.T) {
// setup ->
uiChan := make(chan Message)
sipSrv := newSIPTestServer().Failing()
defer sipSrv.Close()
srv := httptest.NewServer(nil)
defer srv.Close()
d := newDummyRFIDReader()
defer d.Close()
hub = newHub(Config{
HTTPPort: port(srv.URL),
SIPServer: sipSrv.Addr(),
RFIDPort: port(d.addr()),
RFIDTimeout: 1 * time.Second,
})
defer hub.Close()
a := newDummyUIAgent(uiChan, port(srv.URL))
defer a.c.Close()
// <- end setup
if msg := <-d.incoming; string(msg) != "VER2.00\r" {
t.Fatal("RFID-unit didn't get version init command")
}
d.write([]byte("OK\r"))
<-uiChan // CONNECT OK
if err := a.c.WriteMessage(websocket.TextMessage,
[]byte(`{"Action":"BLA", "this is not well formed json }`)); err != nil {
t.Fatal("UI failed to send message over websokcet conn")
}
got := <-uiChan
want := Message{Action: "CONNECT", UserError: true,
ErrorMessage: "unexpected end of JSON input"}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
}
// Attempt CHECKOUT without sending the patron barcode
if err := a.c.WriteMessage(websocket.TextMessage,
[]byte(`{"Action":"CHECKOUT"}`)); err != nil {
t.Fatal("UI failed to send message over websokcet conn")
}
got = <-uiChan
want = Message{Action: "CHECKOUT", UserError: true,
ErrorMessage: "Patron not supplied"}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got %+v; want %+v", got, want)
}
}
func TestBarcodeFromTag(t *testing.T) {
var tests = []struct {
in string
out string
}{
{"1003011596802008:NO:02030000", "03011596802008"}, // only deichman tags should strip 10
{"1003011596802008:NO:02030001", "1003011596802008"},
{"10123456789012", "10123456789012"},
}
for _, tt := range tests {
r := barcodeFromTag(tt.in)
if !reflect.DeepEqual(r, tt.out) {
t.Errorf("BarcodeFromTag(%s) got: %s\t want %s\n", tt.in, r, tt.out)
}
}
}
/*
// Verify that if a second websocket connection is opened from the same IP,
// the first connection is closed.
func TestDuplicateClientConnections(t *testing.T) {
sipPool, _ = pool.NewChannelPool(1, 1, FailingSIPResponse())
ws, _, err := websocket.DefaultDialer.Dial("ws://127.0.0.1:8888/ws", nil)
if err != nil {
t.Fatal("Cannot get ws connection to 127.0.0.1:8888/ws")
}
_, _, err = websocket.DefaultDialer.Dial("ws://127.0.0.1:8888/ws", nil)
if err != nil {
t.Fatal("Cannot get ws connection to 127.0.0.1:8888/ws")
}
time.Sleep(100 * time.Millisecond)
// Try writing to the first connection; it should be closed:
err = ws.WriteMessage(websocket.TextMessage, []byte(`{"Action":"CONNECT"}`))
if err == nil {
t.Error("Two ws connections from the same IP should not be allowed")
}
time.Sleep(100 * time.Millisecond)
}
*/