-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathRealtimeClientConnection.swift
1434 lines (1222 loc) · 63 KB
/
RealtimeClientConnection.swift
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// RealtimeClient.connection.swift
// ably
//
// Created by Ricardo Pereira on 03/11/2015.
// Copyright © 2015 Ably. All rights reserved.
//
import Quick
import Nimble
func countChannels(channels: ARTRealtimeChannels) -> Int {
var i = 0
for _ in channels {
i++
}
return i
}
class RealtimeClientConnection: QuickSpec {
override func spec() {
describe("Connection") {
// RTN2
context("url") {
it("should connect to the default host") {
let options = ARTClientOptions(key: "keytest:secret")
options.autoConnect = false
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
if let transport = client.transport as? TestProxyTransport, let url = transport.lastUrl {
expect(url.host).to(equal("realtime.ably.io"))
}
else {
XCTFail("MockTransport isn't working")
}
client.close()
}
it("should connect with query string params") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
waitUntil(timeout: testTimeout) { done in
client.connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let errorInfo = stateChange.reason
switch state {
case .Failed:
AblyTests.checkError(errorInfo, withAlternative: "Failed state")
done()
case .Connected:
if let transport = client.transport as? TestProxyTransport, let query = transport.lastUrl?.query {
expect(query).to(haveParam("key", withValue: options.key ?? ""))
expect(query).to(haveParam("echo", withValue: "true"))
expect(query).to(haveParam("format", withValue: "json"))
}
else {
XCTFail("MockTransport isn't working")
}
done()
break
default:
break
}
}
}
client.close()
}
it("should connect with query string params including clientId") {
let options = AblyTests.commonAppSetup()
options.clientId = "client_string"
options.autoConnect = false
options.echoMessages = false
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
waitUntil(timeout: testTimeout) { done in
client.connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let errorInfo = stateChange.reason
switch state {
case .Failed:
AblyTests.checkError(errorInfo, withAlternative: "Failed state")
done()
case .Connected:
if let transport = client.transport as? TestProxyTransport, let query = transport.lastUrl?.query {
expect(query).to(haveParam("accessToken", withValue: client.auth.tokenDetails?.token ?? ""))
expect(query).to(haveParam("echo", withValue: "false"))
expect(query).to(haveParam("format", withValue: "json"))
expect(query).to(haveParam("client_id", withValue: "client_string"))
}
else {
XCTFail("MockTransport isn't working")
}
done()
break
default:
break
}
}
}
client.close()
}
}
// RTN3
it("should connect automatically") {
let options = AblyTests.commonAppSetup()
var connected = false
// Default
expect(options.autoConnect).to(beTrue(), description: "autoConnect should be true by default")
// The only way to control this functionality is with the options flag
ARTRealtime(options: options).connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let errorInfo = stateChange.reason
switch state {
case .Connected:
connected = true
default:
break
}
}
expect(connected).toEventually(beTrue(), timeout: 10.0, description: "Can't connect automatically")
}
it("should connect manually") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
let client = ARTRealtime(options: options)
var waiting = true
waitUntil(timeout: testTimeout) { done in
client.connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let errorInfo = stateChange.reason
switch state {
case .Connected:
if waiting {
XCTFail("Expected to be disconnected")
}
done()
default:
break
}
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2.0 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
waiting = false
client.connect()
}
}
client.close()
}
// RTN4
context("event emitter") {
// RTN4a
it("should emit events for state changes") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
let client = ARTRealtime(options: options)
let connection = client.connection
var events: [ARTRealtimeConnectionState] = []
waitUntil(timeout: testTimeout) { done in
connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let errorInfo = stateChange.reason
switch state {
case .Connecting:
events += [state]
case .Connected:
events += [state]
client.onDisconnected()
case .Disconnected:
events += [state]
client.close()
case .Suspended:
events += [state]
client.onError(AblyTests.newErrorProtocolMessage())
case .Closing:
events += [state]
case .Closed:
events += [state]
client.onSuspended()
case .Failed:
events += [state]
expect(errorInfo).toNot(beNil(), description: "Error is nil")
connection.off()
done()
default:
break
}
}
events += [connection.state]
connection.connect()
}
if events.count != 8 {
fail("Missing some states")
return
}
expect(events[0].rawValue).to(equal(ARTRealtimeConnectionState.Initialized.rawValue), description: "Should be INITIALIZED state")
expect(events[1].rawValue).to(equal(ARTRealtimeConnectionState.Connecting.rawValue), description: "Should be CONNECTING state")
expect(events[2].rawValue).to(equal(ARTRealtimeConnectionState.Connected.rawValue), description: "Should be CONNECTED state")
expect(events[3].rawValue).to(equal(ARTRealtimeConnectionState.Disconnected.rawValue), description: "Should be DISCONNECTED state")
expect(events[4].rawValue).to(equal(ARTRealtimeConnectionState.Closing.rawValue), description: "Should be CLOSING state")
expect(events[5].rawValue).to(equal(ARTRealtimeConnectionState.Closed.rawValue), description: "Should be CLOSED state")
expect(events[6].rawValue).to(equal(ARTRealtimeConnectionState.Suspended.rawValue), description: "Should be SUSPENDED state")
expect(events[7].rawValue).to(equal(ARTRealtimeConnectionState.Failed.rawValue), description: "Should be FAILED state")
client.close()
}
// RTN4b
it("should emit states on a new connection") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
let client = ARTRealtime(options: options)
let connection = client.connection
var events: [ARTRealtimeConnectionState] = []
waitUntil(timeout: testTimeout) { done in
connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let errorInfo = stateChange.reason
switch state {
case .Connecting:
events += [state]
case .Connected:
events += [state]
done()
default:
break
}
}
connection.connect()
}
expect(events).to(haveCount(2), description: "Missing CONNECTING or CONNECTED state")
if events.count != 2 {
return
}
expect(events[0].rawValue).to(equal(ARTRealtimeConnectionState.Connecting.rawValue), description: "Should be CONNECTING state")
expect(events[1].rawValue).to(equal(ARTRealtimeConnectionState.Connected.rawValue), description: "Should be CONNECTED state")
connection.close()
}
// RTN4c
it("should emit states when connection is closed") {
let connection = ARTRealtime(options: AblyTests.commonAppSetup()).connection
var events: [ARTRealtimeConnectionState] = []
waitUntil(timeout: testTimeout) { done in
connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let errorInfo = stateChange.reason
switch state {
case .Connected:
connection.close()
case .Closing:
events += [state]
case .Closed:
events += [state]
done()
default:
break
}
}
}
expect(events).to(haveCount(2), description: "Missing CLOSING or CLOSED state")
if events.count != 2 {
return
}
expect(events[0].rawValue).to(equal(ARTRealtimeConnectionState.Closing.rawValue), description: "Should be CLOSING state")
expect(events[1].rawValue).to(equal(ARTRealtimeConnectionState.Closed.rawValue), description: "Should be CLOSED state")
connection.close()
}
// RTN4d
it("should have the current state") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
let client = ARTRealtime(options: options)
let connection = client.connection
expect(connection.state.rawValue).to(equal(ARTRealtimeConnectionState.Initialized.rawValue), description: "Missing INITIALIZED state")
waitUntil(timeout: testTimeout) { done in
connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let errorInfo = stateChange.reason
switch state {
case .Connecting:
expect(connection.state.rawValue).to(equal(ARTRealtimeConnectionState.Connecting.rawValue), description: "Missing CONNECTING state")
case .Connected:
expect(connection.state.rawValue).to(equal(ARTRealtimeConnectionState.Connected.rawValue), description: "Missing CONNECTED state")
done()
default:
break
}
}
client.connect()
}
connection.close()
}
// RTN4f
it("should have the reason which contains an ErrorInfo") {
let options = AblyTests.commonAppSetup()
let client = ARTRealtime(options: options)
let connection = client.connection
// TODO: ConnectionStateChange object
var errorInfo: ARTErrorInfo?
waitUntil(timeout: testTimeout) { done in
connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let reason = stateChange.reason
switch state {
case .Connected:
client.onError(AblyTests.newErrorProtocolMessage())
case .Failed:
errorInfo = reason
done()
default:
break
}
}
}
expect(errorInfo).toNot(beNil())
connection.close()
}
}
class TotalReach {
// Easy way to create an atomic var
static var shared = 0
// This prevents others from using the default '()' initializer
private init() {}
}
// RTN5
it("basic operations should work simultaneously") {
let options = AblyTests.commonAppSetup()
options.echoMessages = false
var disposable = [ARTRealtime]()
let max = 50
let channelName = "chat"
TotalReach.shared = 0
for _ in 1...max {
let client = ARTRealtime(options: options)
disposable.append(client)
let channel = client.channels.get(channelName)
channel.on { errorInfo in
if channel.state == .Attached {
TotalReach.shared++
}
}
channel.attach()
}
// All channels attached
expect(TotalReach.shared).toEventually(equal(max), timeout: testTimeout, description: "Channels not attached")
TotalReach.shared = 0
for client in disposable {
let channel = client.channels.get(channelName)
expect(channel.state).to(equal(ARTRealtimeChannelState.Attached))
channel.subscribe { message in
expect(message.data as? String).to(equal("message_string"))
TotalReach.shared++
}
channel.publish(nil, data: "message_string", cb: nil)
}
// Sends 50 messages from different clients to the same channel
// 50 messages for 50 clients = 50*50 total messages
// echo is off, so we need to subtract one message per client
expect(TotalReach.shared).toEventually(equal(max*max - max), timeout: testTimeout)
expect(disposable.count).to(equal(max))
expect(countChannels(disposable.first!.channels)).to(equal(1))
expect(countChannels(disposable.last!.channels)).to(equal(1))
}
// RTN6
it("should have an opened websocket connection and received a CONNECTED ProtocolMessage") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
defer {
client.dispose()
client.close()
}
waitUntil(timeout: testTimeout) { done in
client.connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let error = stateChange.reason
expect(error).to(beNil())
if state == .Connected && error == nil {
done()
}
}
}
if let webSocketTransport = client.transport as? ARTWebSocketTransport {
expect(webSocketTransport.isConnected).to(beTrue())
}
else {
XCTFail("WebSocket is not the default transport")
}
if let transport = client.transport as? TestProxyTransport {
// CONNECTED ProtocolMessage
expect(transport.protocolMessagesReceived.map{ $0.action }).to(contain(ARTProtocolMessageAction.Connected))
}
else {
XCTFail("MockTransport is not working")
}
}
// RTN7
context("ACK and NACK") {
// RTN7a
context("should expect either an ACK or NACK to confirm") {
it("successful receipt and acceptance of message") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
options.clientId = "client_string"
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
defer { client.close() }
waitUntil(timeout: testTimeout) { done in
publishFirstTestMessage(client, completion: { error in
expect(error).to(beNil())
done()
})
}
let transport = client.transport as! TestProxyTransport
guard let publishedMessage = transport.protocolMessagesSent.filter({ $0.action == .Message }).last else {
XCTFail("No MESSAGE action was sent"); return
}
guard let receivedAck = transport.protocolMessagesReceived.filter({ $0.action == .Ack }).last else {
XCTFail("No ACK action was received"); return
}
expect(publishedMessage.msgSerial).to(equal(receivedAck.msgSerial))
}
it("successful receipt and acceptance of presence") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
options.clientId = "client_string"
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
defer { client.close() }
waitUntil(timeout: testTimeout) { done in
client.connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let error = stateChange.reason
if state == .Connected {
let channel = client.channels.get("test")
channel.on { errorInfo in
if channel.state == .Attached {
channel.presence.enterClient("client_string", data: nil, cb: { errorInfo in
expect(errorInfo).to(beNil())
done()
})
}
}
channel.attach()
}
}
}
let transport = client.transport as! TestProxyTransport
guard let publishedMessage = transport.protocolMessagesSent.filter({ $0.action == .Presence }).last else {
XCTFail("No PRESENCE action was sent"); return
}
guard let receivedAck = transport.protocolMessagesReceived.filter({ $0.action == .Ack }).last else {
XCTFail("No ACK action was received"); return
}
expect(publishedMessage.msgSerial).to(equal(receivedAck.msgSerial))
}
it("message failure") {
let options = AblyTests.clientOptions()
options.token = getTestToken(capability: "{ \"test\":[\"subscribe\"] }")
options.autoConnect = false
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
defer { client.close() }
waitUntil(timeout: testTimeout) { done in
publishFirstTestMessage(client, completion: { error in
expect(error).toNot(beNil())
done()
})
}
let transport = client.transport as! TestProxyTransport
guard let publishedMessage = transport.protocolMessagesSent.filter({ $0.action == .Message }).last else {
XCTFail("No MESSAGE action was sent"); return
}
guard let receivedNack = transport.protocolMessagesReceived.filter({ $0.action == .Nack }).last else {
XCTFail("No NACK action was received"); return
}
expect(publishedMessage.msgSerial).to(equal(receivedNack.msgSerial))
}
it("presence failure") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
options.clientId = "client_string"
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
defer { client.close() }
waitUntil(timeout: testTimeout) { done in
client.connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let error = stateChange.reason
if state == .Connected {
let channel = client.channels.get("test")
channel.on { errorInfo in
if channel.state == .Attached {
channel.presence.enterClient("invalid", data: nil, cb: { errorInfo in
expect(errorInfo).toNot(beNil())
done()
})
}
}
channel.attach()
}
}
}
let transport = client.transport as! TestProxyTransport
guard let publishedMessage = transport.protocolMessagesSent.filter({ $0.action == .Presence }).last else {
XCTFail("No PRESENCE action was sent"); return
}
guard let receivedNack = transport.protocolMessagesReceived.filter({ $0.action == .Nack }).last else {
XCTFail("No NACK action was received"); return
}
expect(publishedMessage.msgSerial).to(equal(receivedNack.msgSerial))
}
}
// RTN7b
context("ProtocolMessage") {
class TotalMessages {
static var expected: Int32 = 0
static var succeeded: Int32 = 0
private init() {}
}
it("should contain unique serially incrementing msgSerial along with the count") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
options.clientId = "client_string"
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
defer { client.close() }
let channel = client.channels.get("channel")
channel.attach()
waitUntil(timeout: testTimeout) { done in
channel.publish(nil, data: "message") { errorInfo in
expect(errorInfo).to(beNil())
done()
}
}
TotalMessages.expected = 5
for index in 1...TotalMessages.expected {
channel.publish(nil, data: "message\(index)") { errorInfo in
if errorInfo == nil {
TotalMessages.succeeded++
}
}
}
expect(TotalMessages.succeeded).toEventually(equal(TotalMessages.expected), timeout: testTimeout)
waitUntil(timeout: testTimeout) { done in
channel.presence.enterClient("invalid", data: nil, cb: { errorInfo in
expect(errorInfo).toNot(beNil())
done()
})
}
let transport = client.transport as! TestProxyTransport
let acks = transport.protocolMessagesReceived.filter({ $0.action == .Ack })
let nacks = transport.protocolMessagesReceived.filter({ $0.action == .Nack })
if acks.count != 2 {
fail("Received invalid number of ACK responses: \(acks.count)")
return
}
expect(acks[0].msgSerial).to(equal(0))
expect(acks[0].count).to(equal(1))
// Messages covered in a single ACK response
expect(acks[1].msgSerial).to(equal(1))
expect(acks[1].count).to(equal(TotalMessages.expected))
if nacks.count != 1 {
fail("Received invalid number of NACK responses: \(nacks.count)")
return
}
expect(nacks[0].msgSerial).to(equal(6))
expect(nacks[0].count).to(equal(1))
}
}
// RTN7c
context("should trigger the failure callback for the remaining pending messages if") {
it("connection is closed") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
options.clientId = "client_string"
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
defer { client.close() }
let channel = client.channels.get("channel")
let transport = client.transport as! TestProxyTransport
transport.actionsIgnored += [.Ack, .Nack]
waitUntil(timeout: testTimeout) { done in
channel.on { errorInfo in
if channel.state == .Attached {
channel.publish(nil, data: "message", cb: { errorInfo in
expect(errorInfo).toNot(beNil())
done()
})
// Wait until the message is pushed to Ably first
delay(1.0) {
transport.simulateIncomingNormalClose()
}
}
}
channel.attach()
}
}
it("connection state enters FAILED") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
options.clientId = "client_string"
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
defer { client.close() }
let channel = client.channels.get("channel")
let transport = client.transport as! TestProxyTransport
transport.actionsIgnored += [.Ack, .Nack]
waitUntil(timeout: testTimeout) { done in
channel.on { errorInfo in
if channel.state == .Attached {
channel.publish(nil, data: "message", cb: { errorInfo in
expect(errorInfo).toNot(beNil())
done()
})
// Wait until the message is pushed to Ably first
delay(1.0) {
transport.simulateIncomingError()
}
}
}
channel.attach()
}
}
it("lost connection state") {
let options = AblyTests.commonAppSetup()
options.autoConnect = false
let client = ARTRealtime(options: options)
client.setTransportClass(TestProxyTransport.self)
client.connect()
defer {
client.dispose()
client.close()
}
let channel = client.channels.get("channel")
let transport = client.transport as! TestProxyTransport
transport.actionsIgnored += [.Ack, .Nack]
channel.attach()
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.Attached), timeout: testTimeout)
var gotPublishedCallback = false
channel.publish(nil, data: "message", cb: { errorInfo in
expect(errorInfo).toNot(beNil())
gotPublishedCallback = true
})
// Wait until the message is pushed to Ably first
delay(1.0) {
client.simulateLostConnection()
}
expect(gotPublishedCallback).toEventually(beTrue(), timeout: testTimeout)
}
}
}
// RTN8
context("connection#id") {
// RTN8a
it("should be null until connected") {
let options = AblyTests.commonAppSetup()
let client = ARTRealtime(options: options)
let connection = client.connection
defer {
client.dispose()
client.close()
}
expect(connection.id).to(beNil())
waitUntil(timeout: testTimeout) { done in
connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let errorInfo = stateChange.reason
expect(errorInfo).to(beNil())
if state == .Connected {
expect(connection.id).toNot(beNil())
done()
}
else if state == .Connecting {
expect(connection.id).to(beNil())
}
}
}
}
// RTN8b
it("should have unique IDs") {
let options = AblyTests.commonAppSetup()
var disposable = [ARTRealtime]()
var ids = [String]()
let max = 25
waitUntil(timeout: testTimeout) { done in
for _ in 1...max {
disposable.append(ARTRealtime(options: options))
let currentConnection = disposable.last!.connection
currentConnection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let errorInfo = stateChange.reason
if state == .Connected {
guard let connectionId = currentConnection.id else {
fail("connectionId is nil on CONNECTED")
done()
return
}
expect(ids).toNot(contain(connectionId))
ids.append(connectionId)
currentConnection.close()
if ids.count == max {
done()
}
}
}
}
}
expect(ids).to(haveCount(max))
}
}
// RTN9
context("connection#key") {
// RTN9a
it("should be null until connected") {
let options = AblyTests.commonAppSetup()
let client = ARTRealtime(options: options)
defer {
client.dispose()
client.close()
}
let connection = client.connection
expect(connection.key).to(beNil())
waitUntil(timeout: testTimeout) { done in
connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let errorInfo = stateChange.reason
expect(errorInfo).to(beNil())
if state == .Connected {
expect(connection.id).toNot(beNil())
done()
}
else if state == .Connecting {
expect(connection.key).to(beNil())
}
}
}
}
// RTN9b
it("should have unique connection keys") {
let options = AblyTests.commonAppSetup()
var disposable = [ARTRealtime]()
var keys = [String]()
let max = 25
waitUntil(timeout: testTimeout) { done in
for _ in 1...max {
disposable.append(ARTRealtime(options: options))
let currentConnection = disposable.last!.connection
currentConnection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let errorInfo = stateChange.reason
if state == .Connected {
guard let connectionKey = currentConnection.key else {
fail("connectionKey is nil on CONNECTED")
done()
return
}
expect(keys).toNot(contain(connectionKey))
keys.append(connectionKey)
currentConnection.close()
if keys.count == max {
done()
}
}
}
}
}
expect(keys).to(haveCount(max))
}
}
// RTN10
context("serial") {
// RTN10a
it("should be -1 once connected") {
let client = ARTRealtime(options: AblyTests.commonAppSetup())
defer {
client.dispose()
client.close()
}
waitUntil(timeout: testTimeout) { done in
client.connection.on { stateChange in
let stateChange = stateChange!
let state = stateChange.current
let errorInfo = stateChange.reason
if state == .Connected {
expect(client.connection.serial).to(equal(-1))
done()
}
}
}
}
// RTN10b
pending("should not update when a message is sent but increments by one when ACK is received") {
let client = ARTRealtime(options: AblyTests.commonAppSetup())
defer {
client.dispose()
client.close()
}
let channel = client.channels.get("test")
for index in 0...5 {
waitUntil(timeout: testTimeout) { done in
channel.publish(nil, data: "message", cb: { errorInfo in
expect(errorInfo).to(beNil())
// Updated
expect(client.connection.serial).to(equal(Int64(index)))
done()
})
// Not updated
expect(client.connection.serial).to(equal(Int64(index - 1)))
}
}
}
// RTN10c
pending("should have last known connection serial from restored connection") {
let options = AblyTests.commonAppSetup()
let client = ARTRealtime(options: options)
defer {
client.dispose()
client.close()
}
let channel = client.channels.get("test")
var lastSerial: Int64 = 0
for _ in 1...5 {
channel.publish(nil, data: "message", cb: { errorInfo in
expect(errorInfo).to(beNil())
lastSerial = client.connection.serial
})
}
expect(lastSerial).toEventually(equal(4), timeout: testTimeout)
options.recover = client.connection.recoveryKey
let recoveredClient = ARTRealtime(options: options)
defer { recoveredClient.close() }
let recoveredChannel = recoveredClient.channels.get("test")
waitUntil(timeout: testTimeout) { done in