-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathwebsocket_client_test.go
868 lines (753 loc) · 22.5 KB
/
websocket_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
866
867
868
package onet
import (
"bytes"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net/http"
"os"
"sort"
"sync"
"testing"
"time"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/require"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/network"
"go.dedis.ch/protobuf"
"golang.org/x/xerrors"
)
func TestClient_Send(t *testing.T) {
local := NewTCPTest(tSuite)
defer local.CloseAll()
// register service
RegisterNewService(backForthServiceName, func(c *Context) (Service, error) {
return &simpleService{
ctx: c,
}, nil
})
defer ServiceFactory.Unregister(backForthServiceName)
// create servers
servers, el, _ := local.GenTree(4, false)
client := local.NewClient(backForthServiceName)
r := &SimpleRequest{
ServerIdentities: el,
Val: 10,
}
sr := &SimpleResponse{}
require.Equal(t, uint64(0), client.Rx())
require.Equal(t, uint64(0), client.Tx())
require.Nil(t, client.SendProtobuf(servers[0].ServerIdentity, r, sr))
require.Equal(t, sr.Val, int64(10))
require.NotEqual(t, uint64(0), client.Rx())
require.NotEqual(t, uint64(0), client.Tx())
require.True(t, client.Tx() > client.Rx())
}
func TestClientTLS_Send(t *testing.T) {
cert, key, err := getSelfSignedCertificateAndKey()
require.Nil(t, err)
CAPool := x509.NewCertPool()
CAPool.AppendCertsFromPEM(cert)
local := NewTCPTest(tSuite)
local.webSocketTLSCertificate = cert
local.webSocketTLSCertificateKey = key
defer local.CloseAll()
// register service
RegisterNewService(backForthServiceName, func(c *Context) (Service, error) {
return &simpleService{
ctx: c,
}, nil
})
defer ServiceFactory.Unregister(backForthServiceName)
// create servers
servers, el, _ := local.GenTree(4, false)
client := local.NewClient(backForthServiceName)
client.TLSClientConfig = &tls.Config{RootCAs: CAPool}
r := &SimpleRequest{
ServerIdentities: el,
Val: 10,
}
sr := &SimpleResponse{}
require.Equal(t, uint64(0), client.Rx())
require.Equal(t, uint64(0), client.Tx())
require.Nil(t, client.SendProtobuf(servers[0].ServerIdentity, r, sr))
require.Equal(t, sr.Val, int64(10))
require.NotEqual(t, uint64(0), client.Rx())
require.NotEqual(t, uint64(0), client.Tx())
require.True(t, client.Tx() > client.Rx())
}
func TestClientTLS_certfile_Send(t *testing.T) {
// like TestClientTLSfile_Send, but uses cert and key from a file
// to solve issue 583.
cert, key, err := getSelfSignedCertificateAndKey()
require.Nil(t, err)
CAPool := x509.NewCertPool()
CAPool.AppendCertsFromPEM(cert)
f1, err := ioutil.TempFile("", "cert")
require.NoError(t, err)
defer os.Remove(f1.Name())
f1.Write(cert)
f1.Close()
f2, err := ioutil.TempFile("", "key")
require.NoError(t, err)
defer os.Remove(f2.Name())
f2.Write(key)
f2.Close()
local := NewTCPTest(tSuite)
local.webSocketTLSCertificate = []byte(f1.Name())
local.webSocketTLSCertificateKey = []byte(f2.Name())
local.webSocketTLSReadFiles = true
defer local.CloseAll()
// register service
RegisterNewService(backForthServiceName, func(c *Context) (Service, error) {
return &simpleService{
ctx: c,
}, nil
})
defer ServiceFactory.Unregister(backForthServiceName)
// create servers
servers, el, _ := local.GenTree(4, false)
client := local.NewClient(backForthServiceName)
client.TLSClientConfig = &tls.Config{RootCAs: CAPool}
r := &SimpleRequest{
ServerIdentities: el,
Val: 10,
}
sr := &SimpleResponse{}
require.Equal(t, uint64(0), client.Rx())
require.Equal(t, uint64(0), client.Tx())
require.Nil(t, client.SendProtobuf(servers[0].ServerIdentity, r, sr))
require.Equal(t, sr.Val, int64(10))
require.NotEqual(t, uint64(0), client.Rx())
require.NotEqual(t, uint64(0), client.Tx())
require.True(t, client.Tx() > client.Rx())
}
func TestClient_Parallel(t *testing.T) {
nbrNodes := 4
nbrParallel := 20
local := NewTCPTest(tSuite)
defer local.CloseAll()
// register service
RegisterNewService(backForthServiceName, func(c *Context) (Service, error) {
return &simpleService{
ctx: c,
}, nil
})
defer ServiceFactory.Unregister(backForthServiceName)
// create servers
servers, el, _ := local.GenTree(nbrNodes, true)
wg := sync.WaitGroup{}
wg.Add(nbrParallel)
for i := 0; i < nbrParallel; i++ {
go func(i int) {
defer wg.Done()
log.Lvl1("Starting message", i)
r := &SimpleRequest{
ServerIdentities: el,
Val: int64(10 * i),
}
client := local.NewClient(backForthServiceName)
sr := &SimpleResponse{}
err := client.SendProtobuf(servers[0].ServerIdentity, r, sr)
require.Nil(t, err)
require.Equal(t, int64(10*i), sr.Val)
log.Lvl1("Done with message", i)
}(i)
}
wg.Wait()
}
func TestClientTLS_Parallel(t *testing.T) {
cert, key, err := getSelfSignedCertificateAndKey()
require.Nil(t, err)
CAPool := x509.NewCertPool()
CAPool.AppendCertsFromPEM(cert)
nbrNodes := 4
nbrParallel := 20
local := NewTCPTest(tSuite)
local.webSocketTLSCertificate = cert
local.webSocketTLSCertificateKey = key
defer local.CloseAll()
// register service
RegisterNewService(backForthServiceName, func(c *Context) (Service, error) {
return &simpleService{
ctx: c,
}, nil
})
defer ServiceFactory.Unregister(backForthServiceName)
// create servers
servers, el, _ := local.GenTree(nbrNodes, true)
wg := sync.WaitGroup{}
wg.Add(nbrParallel)
for i := 0; i < nbrParallel; i++ {
go func(i int) {
defer wg.Done()
log.Lvl1("Starting message", i)
r := &SimpleRequest{
ServerIdentities: el,
Val: int64(10 * i),
}
client := local.NewClient(backForthServiceName)
client.TLSClientConfig = &tls.Config{RootCAs: CAPool}
sr := &SimpleResponse{}
require.Nil(t, client.SendProtobuf(servers[0].ServerIdentity, r, sr))
require.Equal(t, int64(10*i), sr.Val)
log.Lvl1("Done with message", i)
}(i)
}
wg.Wait()
}
func TestNewClientKeep(t *testing.T) {
c := NewClientKeep(tSuite, serviceWebSocket)
require.True(t, c.keep)
}
func TestMultiplePath(t *testing.T) {
_, err := RegisterNewService(dummyService3Name, func(c *Context) (Service, error) {
ds := &DummyService3{}
return ds, nil
})
require.Nil(t, err)
defer UnregisterService(dummyService3Name)
local := NewTCPTest(tSuite)
hs := local.GenServers(2)
server := hs[0]
defer local.CloseAll()
client := NewClientKeep(tSuite, dummyService3Name)
msg, err := protobuf.Encode(&DummyMsg{})
require.Nil(t, err)
path1, path2 := "path1", "path2"
resp, err := client.Send(server.ServerIdentity, path1, msg)
require.Nil(t, err)
require.Equal(t, path1, string(resp))
resp, err = client.Send(server.ServerIdentity, path2, msg)
require.Nil(t, err)
require.Equal(t, path2, string(resp))
}
func TestMultiplePathTLS(t *testing.T) {
cert, key, err := getSelfSignedCertificateAndKey()
require.Nil(t, err)
CAPool := x509.NewCertPool()
CAPool.AppendCertsFromPEM(cert)
_, err = RegisterNewService(dummyService3Name, func(c *Context) (Service, error) {
ds := &DummyService3{}
return ds, nil
})
require.Nil(t, err)
defer UnregisterService(dummyService3Name)
local := NewTCPTest(tSuite)
local.webSocketTLSCertificate = cert
local.webSocketTLSCertificateKey = key
hs := local.GenServers(2)
server := hs[0]
defer local.CloseAll()
client := NewClientKeep(tSuite, dummyService3Name)
client.TLSClientConfig = &tls.Config{RootCAs: CAPool}
msg, err := protobuf.Encode(&DummyMsg{})
require.Nil(t, err)
path1, path2 := "path1", "path2"
resp, err := client.Send(server.ServerIdentity, path1, msg)
require.Nil(t, err)
require.Equal(t, path1, string(resp))
resp, err = client.Send(server.ServerIdentity, path2, msg)
require.Nil(t, err)
require.Equal(t, path2, string(resp))
}
func TestWebSocket_Error(t *testing.T) {
client := NewClientKeep(tSuite, dummyService3Name)
local := NewTCPTest(tSuite)
hs := local.GenServers(2)
server := hs[0]
defer local.CloseAll()
lvl := log.DebugVisible()
log.SetDebugVisible(0)
log.OutputToBuf()
_, err := client.Send(server.ServerIdentity, "test", nil)
log.OutputToOs()
log.SetDebugVisible(lvl)
require.NotEqual(t, websocket.ErrBadHandshake, err)
require.NotEqual(t, "", log.GetStdErr())
}
func TestWebSocketTLS_Error(t *testing.T) {
cert, key, err := getSelfSignedCertificateAndKey()
require.Nil(t, err)
CAPool := x509.NewCertPool()
CAPool.AppendCertsFromPEM(cert)
client := NewClientKeep(tSuite, dummyService3Name)
client.TLSClientConfig = &tls.Config{RootCAs: CAPool}
local := NewTCPTest(tSuite)
local.webSocketTLSCertificate = cert
local.webSocketTLSCertificateKey = key
hs := local.GenServers(2)
server := hs[0]
defer local.CloseAll()
lvl := log.DebugVisible()
log.SetDebugVisible(0)
log.OutputToBuf()
_, err = client.Send(server.ServerIdentity, "test", nil)
log.OutputToOs()
log.SetDebugVisible(lvl)
require.NotEqual(t, websocket.ErrBadHandshake, err)
require.NotEqual(t, "", log.GetStdErr())
}
// TestWebSocket_Streaming_normal reads all messages from the service
func TestWebSocket_Streaming_normal(t *testing.T) {
local := NewTCPTest(tSuite)
defer local.CloseAll()
serName := "streamingService"
_, err := RegisterNewService(serName, newStreamingService)
require.NoError(t, err)
defer UnregisterService(serName)
servers, el, _ := local.GenTree(4, false)
client := local.NewClientKeep(serName)
n := 5
r := &SimpleRequest{
ServerIdentities: el,
Val: int64(n),
}
log.Lvl1("Happy-path testing")
conn, err := client.Stream(servers[0].ServerIdentity, r)
require.NoError(t, err)
for i := 0; i < n; i++ {
sr := &SimpleResponse{}
require.NoError(t, conn.ReadMessage(sr))
require.Equal(t, sr.Val, int64(n))
}
// Using the same client (connection) to repeat the same request should
// fail because the connection should be closed by the service when
// there are no more messages.
log.Lvl1("Fail on re-use")
sr := &SimpleResponse{}
require.Error(t, conn.ReadMessage(sr))
require.NoError(t, client.Close())
}
// TestWebSocket_Streaming_Parallel_normal
func TestWebSocket_Streaming_Parallel_normal(t *testing.T) {
local := NewTCPTest(tSuite)
defer local.CloseAll()
serName := "streamingService"
_, err := RegisterNewService(serName, newStreamingService)
require.NoError(t, err)
defer UnregisterService(serName)
servers, el, _ := local.GenTree(4, false)
n := 10
// Do streaming with 10 clients in parallel. Happy-path where clients read
// everything.
clients := make([]*Client, 100)
for i := range clients {
clients[i] = local.NewClientKeep(serName)
}
var wg sync.WaitGroup
for _, client := range clients {
wg.Add(1)
go func(c *Client) {
defer wg.Done()
r := &SimpleRequest{
ServerIdentities: el,
Val: int64(n),
}
conn, err := c.Stream(servers[0].ServerIdentity, r)
require.NoError(t, err)
for i := 0; i < n; i++ {
sr := &SimpleResponse{}
require.NoError(t, conn.ReadMessage(sr))
require.Equal(t, sr.Val, int64(n))
}
}(client)
}
wg.Wait()
for i := range clients {
require.NoError(t, clients[i].Close())
}
}
// TestWebSocket_Streaming_bi_normal sends multiple messages from the clients
// and reads all the messages
func TestWebSocket_Streaming_bi_normal(t *testing.T) {
local := NewTCPTest(tSuite)
defer local.CloseAll()
serviceStruct := struct {
once sync.Once
outChan chan *SimpleResponse
closeChan chan bool
}{
outChan: make(chan *SimpleResponse, 10),
closeChan: make(chan bool),
}
h := func(m *SimpleRequest) (chan *SimpleResponse, chan bool, error) {
go func() {
for i := 0; i < int(m.Val); i++ {
time.Sleep(100 * time.Millisecond)
serviceStruct.outChan <- &SimpleResponse{int64(i)}
}
<-serviceStruct.closeChan
serviceStruct.once.Do(func() {
close(serviceStruct.outChan)
})
}()
return serviceStruct.outChan, serviceStruct.closeChan, nil
}
newCustomStreamingService := func(c *Context) (Service, error) {
s := &StreamingService{
ServiceProcessor: NewServiceProcessor(c),
stopAt: -1,
}
if err := s.RegisterStreamingHandler(h); err != nil {
panic(err.Error())
}
return s, nil
}
serName := "biStreamingService"
_, err := RegisterNewService(serName, newCustomStreamingService)
require.NoError(t, err)
defer UnregisterService(serName)
servers, el, _ := local.GenTree(4, false)
client := local.NewClientKeep(serName)
// A first request to the service
n := 5
r := &SimpleRequest{
ServerIdentities: el,
Val: int64(n),
}
conn, err := client.Stream(servers[0].ServerIdentity, r)
require.NoError(t, err)
for i := 0; i < n; i++ {
sr := &SimpleResponse{}
require.NoError(t, conn.ReadMessage(sr))
require.Equal(t, sr.Val, int64(i))
}
// Lets perform a second request
n = 5
r = &SimpleRequest{
ServerIdentities: el,
Val: int64(n),
}
conn, err = client.Stream(servers[0].ServerIdentity, r)
require.NoError(t, err)
for i := 0; i < n; i++ {
sr := &SimpleResponse{}
require.NoError(t, conn.ReadMessage(sr))
require.Equal(t, sr.Val, int64(i))
}
client.Close()
time.Sleep(time.Second)
}
// TestWebSocket_Streaming_early_client makes the client close early.
func TestWebSocket_Streaming_early_client(t *testing.T) {
local := NewTCPTest(tSuite)
defer local.CloseAll()
serName := "streamingService"
serID, err := RegisterNewService(serName, newStreamingService)
require.NoError(t, err)
defer UnregisterService(serName)
servers, el, _ := local.GenTree(4, false)
client := local.NewClientKeep(serName)
n := 5
r := &SimpleRequest{
ServerIdentities: el,
Val: int64(n),
}
// go-routine should also terminate.
client = local.NewClientKeep(serName)
services := local.GetServices(servers, serID)
serviceRoot := services[0].(*StreamingService)
serviceRoot.gotStopChan = make(chan bool, 1)
_, err = client.Stream(servers[0].ServerIdentity, r)
require.NoError(t, err)
require.NoError(t, client.Close())
select {
case <-serviceRoot.gotStopChan:
case <-time.After(time.Second):
require.Fail(t, "should have got an early finish signal")
}
}
// TestWebSocket_Streaming_Parallel_early_client
func TestWebSocket_Streaming_Parallel_early_client2(t *testing.T) {
local := NewTCPTest(tSuite)
defer local.CloseAll()
serName := "streamingService"
serID, err := RegisterNewService(serName, newStreamingService)
require.NoError(t, err)
defer UnregisterService(serName)
servers, el, _ := local.GenTree(4, false)
services := local.GetServices(servers, serID)
serviceRoot := services[0].(*StreamingService)
n := 10
// Unhappy-path where clients stop early.
clients := make([]*Client, 100)
for i := range clients {
clients[i] = local.NewClientKeep(serName)
}
serviceRoot.gotStopChan = make(chan bool, len(clients))
wg := sync.WaitGroup{}
for _, client := range clients {
wg.Add(1)
go func(c *Client) {
defer wg.Done()
r := &SimpleRequest{
ServerIdentities: el,
Val: int64(n),
}
conn, err := c.Stream(servers[0].ServerIdentity, r)
require.NoError(t, err)
// read one message instead of n then close
sr := &SimpleResponse{}
require.NoError(t, conn.ReadMessage(sr))
require.Equal(t, sr.Val, int64(n))
require.NoError(t, c.Close())
}(client)
}
// we should get close messages
for i := 0; i < len(clients); i++ {
select {
case <-serviceRoot.gotStopChan:
case <-time.After(time.Second):
require.Fail(t, "should have got an early finish signal")
}
}
wg.Wait()
}
// TestWebSocket_Streaming_early_service closes the service early
func TestWebSocket_Streaming_early_service(t *testing.T) {
local := NewTCPTest(tSuite)
defer local.CloseAll()
serName := "streamingService"
serID, err := RegisterNewService(serName, newStreamingService)
require.NoError(t, err)
defer UnregisterService(serName)
servers, el, _ := local.GenTree(4, false)
client := local.NewClientKeep(serName)
n := 5
r := &SimpleRequest{
ServerIdentities: el,
Val: int64(n),
}
// Have the service terminate early. The client should stop receiving
// messages.
log.Lvl1("Service terminate early")
stopAt := 1
client = local.NewClientKeep(serName)
services := local.GetServices(servers, serID)
serviceRoot := services[0].(*StreamingService)
serviceRoot.stopAt = stopAt
conn, err := client.Stream(servers[0].ServerIdentity, r)
require.NoError(t, err)
for i := 0; i < n; i++ {
if i > stopAt {
sr := &SimpleResponse{}
require.Error(t, conn.ReadMessage(sr))
} else {
sr := &SimpleResponse{}
require.NoError(t, conn.ReadMessage(sr))
require.Equal(t, sr.Val, int64(n))
}
}
require.NoError(t, client.Close())
}
// TestWebSocket_Streaming_Parallel_early_service
func TestWebSocket_Streaming_Parallel_ealry_service(t *testing.T) {
local := NewTCPTest(tSuite)
defer local.CloseAll()
serName := "streamingService"
serID, err := RegisterNewService(serName, newStreamingService)
require.NoError(t, err)
defer UnregisterService(serName)
servers, el, _ := local.GenTree(4, false)
services := local.GetServices(servers, serID)
serviceRoot := services[0].(*StreamingService)
n := 10
// The other unhappy-path where the service stops early.
clients := make([]*Client, 100)
for i := range clients {
clients[i] = local.NewClientKeep(serName)
}
stopAt := 1
serviceRoot.stopAt = stopAt
wg := sync.WaitGroup{}
for _, client := range clients {
wg.Add(1)
go func(c *Client) {
defer wg.Done()
r := &SimpleRequest{
ServerIdentities: el,
Val: int64(n),
}
conn, err := c.Stream(servers[0].ServerIdentity, r)
require.NoError(t, err)
for i := 0; i < n; i++ {
if i > stopAt {
sr := &SimpleResponse{}
require.Error(t, conn.ReadMessage(sr))
} else {
sr := &SimpleResponse{}
require.NoError(t, conn.ReadMessage(sr))
require.Equal(t, sr.Val, int64(n))
}
}
}(client)
}
wg.Wait()
for i := range clients {
require.NoError(t, clients[i].Close())
}
}
// Tests the correct returning of values depending on the ParallelOptions structure
func TestParallelOptions_GetList(t *testing.T) {
l := NewLocalTest(tSuite)
defer l.CloseAll()
var po *ParallelOptions
_, roster, _ := l.GenTree(3, false)
nodes := roster.List
count, list := po.GetList(nodes)
require.Equal(t, 2, count)
require.Equal(t, 3, len(list))
require.False(t, po.Quit())
po = &ParallelOptions{}
count, list = po.GetList(nodes)
require.Equal(t, 2, count)
require.Equal(t, 3, len(list))
require.False(t, po.Quit())
first := 0
for i := 0; i < 32; i++ {
_, list := po.GetList(nodes)
if (<-list).Equal(nodes[0]) {
first++
}
}
require.NotEqual(t, 0, first)
require.NotEqual(t, 32, first)
po.DontShuffle = true
first = 0
for i := 0; i < 32; i++ {
_, list := po.GetList(nodes)
if (<-list).Equal(nodes[0]) {
first++
}
}
require.Equal(t, 32, first)
po.IgnoreNodes = append(po.IgnoreNodes, nodes[0])
count, list = po.GetList(nodes)
require.Equal(t, 2, count)
require.Equal(t, 2, len(list))
po.IgnoreNodes = append(po.IgnoreNodes, nodes[1])
count, list = po.GetList(nodes)
require.Equal(t, 2, count)
require.Equal(t, 1, len(list))
po.IgnoreNodes = po.IgnoreNodes[0:1]
po.QuitError = true
require.True(t, po.Quit())
po.AskNodes = 1
count, list = po.GetList(nodes)
require.Equal(t, 1, count)
require.Equal(t, 1, len(list))
po.StartNode = 1
count, list = po.GetList(nodes)
require.Equal(t, 1, count)
require.Equal(t, 1, len(list))
}
func TestClient_SendProtobufParallel(t *testing.T) {
l := NewLocalTest(tSuite)
defer l.CloseAll()
servers, roster, _ := l.GenTree(3, false)
cl := NewClient(tSuite, serviceWebSocket)
tests := 10
firstNodes := make([]*network.ServerIdentity, tests)
for i := 0; i < tests; i++ {
log.Lvl1("Sending", i)
var err error
firstNodes[i], err = cl.SendProtobufParallel(roster.List, &SimpleResponse{}, nil, nil)
require.Nil(t, err)
}
for flags := 0; flags < 8; flags++ {
log.Lvl1("Count errors over all services with error-flags", flags)
_, err := cl.SendProtobufParallel(roster.List, &ErrorRequest{
Roster: *roster,
Flags: flags,
}, nil, nil)
if flags == 7 {
require.Error(t, err)
} else {
require.NoError(t, err)
}
// Need to close here to make sure that all messages are being sent
// before going on to the next stage.
require.NoError(t, cl.Close())
}
var errs int
for _, server := range servers {
errs += server.Service(serviceWebSocket).(*ServiceWebSocket).Errors
}
require.Equal(t, 3, errs)
sort.Slice(firstNodes, func(i, j int) bool {
return bytes.Compare(firstNodes[i].ID[:], firstNodes[j].ID[:]) < 0
})
require.False(t, firstNodes[0].Equal(firstNodes[tests-1]))
}
func TestClient_SendProtobufParallelWithDecoder(t *testing.T) {
l := NewLocalTest(tSuite)
defer l.CloseAll()
_, roster, _ := l.GenTree(3, false)
cl := NewClient(tSuite, serviceWebSocket)
decoderWithError := func(data []byte, ret interface{}) error {
// As an example, the decoder should first decode the response, and it can then make
// further verification like the latest block index.
return xerrors.New("decoder error")
}
_, err := cl.SendProtobufParallelWithDecoder(roster.List, &SimpleResponse{}, &SimpleResponse{}, nil, decoderWithError)
require.Error(t, err)
require.Contains(t, err.Error(), "decoder error")
decoderNoError := func(data []byte, ret interface{}) error {
return nil
}
_, err = cl.SendProtobufParallelWithDecoder(roster.List, &SimpleResponse{}, &SimpleResponse{}, nil, decoderNoError)
require.NoError(t, err)
}
const dummyService3Name = "dummyService3"
type DummyService3 struct {
}
func (ds *DummyService3) ProcessClientRequest(req *http.Request, path string, buf []byte) ([]byte, *StreamingTunnel, error) {
log.Lvl2("Got called with path", path, buf)
return []byte(path), nil, nil
}
func (ds *DummyService3) NewProtocol(tn *TreeNodeInstance, conf *GenericConfig) (ProtocolInstance, error) {
return nil, nil
}
func (ds *DummyService3) Process(env *network.Envelope) {
}
type StreamingService struct {
*ServiceProcessor
stopAt int
gotStopChan chan bool
}
func newStreamingService(c *Context) (Service, error) {
s := &StreamingService{
ServiceProcessor: NewServiceProcessor(c),
stopAt: -1,
}
if err := s.RegisterStreamingHandler(s.StreamValues); err != nil {
panic(err.Error())
}
return s, nil
}
func (ss *StreamingService) StreamValues(msg *SimpleRequest) (chan *SimpleResponse, chan bool, error) {
streamingChan := make(chan *SimpleResponse)
stopChan := make(chan bool)
go func() {
outer:
for i := 0; i < int(msg.Val); i++ {
// Add some delay between every message so that we can
// actually catch the stop signal before everything is
// sent out.
time.Sleep(100 * time.Millisecond)
select {
case <-stopChan:
ss.gotStopChan <- true
break outer
default:
streamingChan <- &SimpleResponse{msg.Val}
}
if ss.stopAt == i {
break outer
}
}
close(streamingChan)
}()
return streamingChan, stopChan, nil
}