@@ -101,9 +101,8 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
101
101
let channelId = randomString ( )
102
102
let channel = try await chat. createChannel ( id: channelId, name: channelId)
103
103
104
- // Keeping a strong reference to this object for test purposes to simulate that someone is already present on the given channel.
105
- // If this object is not retained, it will be deallocated, resulting in no subscription to the channel,
106
- // which would cause the behavior being tested to fail.
104
+ // Keeps a strong reference to the returned AsyncStream to prevent it from being deallocated. If this object is not retained,
105
+ // the AsyncStream will be deallocated, which would cause the behavior being tested to fail.
107
106
let connectResult = channel. connect ( )
108
107
debugPrint ( connectResult)
109
108
@@ -122,9 +121,8 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
122
121
let channelId = randomString ( )
123
122
let channel = try await chat. createChannel ( id: channelId, name: channelId)
124
123
125
- // Keeping a strong reference to these objects for test purposes to simulate that someone is already present on the given channel.
126
- // If this object is not retained, it will be deallocated, resulting in no subscription to the channel,
127
- // which would cause the behavior being tested to fail.
124
+ // Keeps a strong reference to the returned AsyncStream to prevent it from being deallocated. If this object is not retained,
125
+ // the AsyncStream will be deallocated, which would cause the behavior being tested to fail.
128
126
let connectResult = channel. connect ( )
129
127
let joinResult = try await channel. join ( )
130
128
@@ -229,9 +227,8 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
229
227
name: " ChannelName "
230
228
)
231
229
232
- // Keeping a strong reference to these objects for test purposes to simulate that someone is already present on the given channel.
233
- // If this object is not retained, it will be deallocated, resulting in no subscription to the channel,
234
- // which would cause the behavior being tested to fail.
230
+ // Keeps a strong reference to the returned AsyncStream to prevent it from being deallocated. If this object is not retained,
231
+ // the AsyncStream will be deallocated, which would cause the behavior being tested to fail.
235
232
let joinValue = try await channel. join ( )
236
233
debugPrint ( joinValue)
237
234
@@ -636,5 +633,84 @@ class ChatAsyncIntegrationTests: BaseAsyncIntegrationTestCase {
636
633
try await chat. removeChannelGroup ( id: channelGroup. id)
637
634
}
638
635
636
+ func testChatAsync_ConnectionStatusListener( ) async throws {
637
+ let expectation = expectation ( description: " Status Listener " )
638
+ expectation. assertForOverFulfill = true
639
+ expectation. expectedFulfillmentCount = 2
640
+
641
+ let channel = try await chat. createChannel ( id: randomString ( ) )
642
+
643
+ let statusStreamTask = Task { [ weak chat] in
644
+ if let chat {
645
+ for await status in chat. connectionStatusStream ( ) . prefix ( 2 ) {
646
+ if status == . online {
647
+ expectation. fulfill ( )
648
+ chat. disconnectSubscriptions ( )
649
+ } else if status == . offline {
650
+ expectation. fulfill ( )
651
+ } else {
652
+ XCTFail ( " Unexpected condition " )
653
+ }
654
+ }
655
+ } else {
656
+ XCTFail ( " Unexpected condition " )
657
+ }
658
+ }
659
+
660
+ let connectTask = Task {
661
+ for await message in channel. connect ( ) {
662
+ debugPrint ( message)
663
+ }
664
+ }
665
+
666
+ addTeardownBlock { [ unowned self] in
667
+ statusStreamTask. cancel ( )
668
+ connectTask. cancel ( )
669
+ _ = try ? await chat. deleteChannel ( id: channel. id)
670
+ }
671
+
672
+ await fulfillment ( of: [ expectation] , timeout: 4 )
673
+ }
674
+
675
+ func testChatAsync_ReconnectSubscriptions( ) async throws {
676
+ let expectation = expectation ( description: " Status Listener " )
677
+ expectation. assertForOverFulfill = true
678
+ expectation. expectedFulfillmentCount = 3
679
+
680
+ let channel = try await chat. createChannel ( id: randomString ( ) )
681
+
682
+ let statusStreamTask = Task { [ weak chat] in
683
+ if let chat {
684
+ for await status in chat. connectionStatusStream ( ) . prefix ( 3 ) {
685
+ if status == . online {
686
+ expectation. fulfill ( )
687
+ chat. disconnectSubscriptions ( )
688
+ } else if status == . offline {
689
+ expectation. fulfill ( )
690
+ chat. reconnectSubscriptions ( )
691
+ } else {
692
+ XCTFail ( " Unexpected condition " )
693
+ }
694
+ }
695
+ } else {
696
+ XCTFail ( " Unexpected condition " )
697
+ }
698
+ }
699
+
700
+ let connectTask = Task {
701
+ for await message in channel. connect ( ) {
702
+ debugPrint ( message)
703
+ }
704
+ }
705
+
706
+ addTeardownBlock { [ unowned self] in
707
+ statusStreamTask. cancel ( )
708
+ connectTask. cancel ( )
709
+ _ = try ? await chat. deleteChannel ( id: channel. id)
710
+ }
711
+
712
+ await fulfillment ( of: [ expectation] , timeout: 4 )
713
+ }
714
+
639
715
// swiftlint:disable:next file_length
640
716
}
0 commit comments