@@ -21,17 +21,19 @@ class CWSChannelTests: XCTestCase {
2121 override func tearDown( ) {
2222 // Put teardown code here. This method is called after the invocation of each test method in the class.
2323 super. tearDown ( )
24+ self . webSocket. disconnect ( )
2425 }
2526
2627 func testGetChannel( ) {
2728 self . webSocket. connect ( )
2829 let connectionExpectation = expectation ( description: " connection expectation " )
29- Timer . scheduledTimer ( withTimeInterval: 1.9 , repeats: false ) { ( _ ) in
30+ Timer . scheduledTimer ( withTimeInterval: 1.0 , repeats: true ) { ( timer ) in
3031 if self . webSocket. getState ( ) == . open {
3132 connectionExpectation. fulfill ( )
33+ timer. invalidate ( )
3234 }
3335 }
34- wait ( for: [ connectionExpectation] , timeout: 2 .0)
36+ wait ( for: [ connectionExpectation] , timeout: 5 .0)
3537 let channelName = " test channel "
3638 let subscribedChannel = self . webSocket. subscribe ( channelName)
3739 let recievedChannel = self . webSocket. getChannel ( by: channelName)
@@ -41,50 +43,148 @@ class CWSChannelTests: XCTestCase {
4143 func testGetAllChannels( ) {
4244 self . webSocket. connect ( )
4345 let connectionExpectation = expectation ( description: " connection expectation " )
44- Timer . scheduledTimer ( withTimeInterval: 1.9 , repeats: false ) { ( _ ) in
46+ Timer . scheduledTimer ( withTimeInterval: 1.0 , repeats: true ) { ( timer ) in
4547 if self . webSocket. getState ( ) == . open {
4648 connectionExpectation. fulfill ( )
49+ timer. invalidate ( )
4750 }
4851 }
49- wait ( for: [ connectionExpectation] , timeout: 2 .0)
52+ wait ( for: [ connectionExpectation] , timeout: 5 .0)
5053 let channels = [ " first channel " , " second channel " , " third channel " ]
5154 _ = channels. map { self . webSocket. subscribe ( $0) }
5255 let recievedChannels = self . webSocket. getChannels ( ) . map { $0. mChannelName }
5356 XCTAssertEqual ( channels, recievedChannels)
5457 }
5558
56- func testPublishAndWatch ( ) {
59+ func testPublishWatchString ( ) {
5760 self . webSocket. connect ( )
5861 let connectionExpectation = expectation ( description: " connection expectation " )
59- Timer . scheduledTimer ( withTimeInterval: 1.9 , repeats: false ) { ( _ ) in
62+ Timer . scheduledTimer ( withTimeInterval: 1.0 , repeats: true ) { ( timer ) in
6063 if self . webSocket. getState ( ) == . open {
6164 connectionExpectation. fulfill ( )
65+ timer. invalidate ( )
6266 }
6367 }
64- wait ( for: [ connectionExpectation] , timeout: 2 .0)
68+ wait ( for: [ connectionExpectation] , timeout: 5 .0)
6569 let channelName = " test channel "
66- let currentData = " test string "
67- _ = self . webSocket. subscribe ( channelName) . publish ( data: currentData ) . watch { ( data) in
68- guard let recievedData = data as? String else {
70+ let currentString = " test string "
71+ _ = self . webSocket. subscribe ( channelName) . publish ( data: currentString ) . watch { ( data) in
72+ guard let recievedString = data as? String else {
6973 return XCTFail ( )
7074 }
71- XCTAssertEqual ( recievedData, currentData)
75+ XCTAssertEqual ( recievedString, currentString)
76+ }
77+ }
78+
79+ func testPublishWatchInt( ) {
80+ self . webSocket. connect ( )
81+ let connectionExpectation = expectation ( description: " connection expectation " )
82+ Timer . scheduledTimer ( withTimeInterval: 1.0 , repeats: true ) { ( timer) in
83+ if self . webSocket. getState ( ) == . open {
84+ connectionExpectation. fulfill ( )
85+ timer. invalidate ( )
86+ }
87+ }
88+ wait ( for: [ connectionExpectation] , timeout: 5.0 )
89+ let channelName = " test channel "
90+ let currentInt = 30
91+ _ = self . webSocket. subscribe ( channelName) . publish ( data: currentInt) . watch { ( data) in
92+ guard let recievedInt = data as? String else {
93+ return XCTFail ( )
94+ }
95+ XCTAssertEqual ( currentInt, Int ( recievedInt) )
96+ }
97+ }
98+
99+ func testPublishWatchDictionary( ) {
100+ self . webSocket. connect ( )
101+ let connectionExpectation = expectation ( description: " connection expectation " )
102+ Timer . scheduledTimer ( withTimeInterval: 1.0 , repeats: true ) { ( timer) in
103+ if self . webSocket. getState ( ) == . open {
104+ connectionExpectation. fulfill ( )
105+ timer. invalidate ( )
106+ }
107+ }
108+ wait ( for: [ connectionExpectation] , timeout: 5.0 )
109+ let channelName = " test channel "
110+ let key = " id "
111+ let value = 0
112+ let currentDictionary = [ key: value]
113+ _ = self . webSocket. subscribe ( channelName) . publish ( data: currentDictionary) . watch { ( data) in
114+ guard let recievedDictionaryString = data as? String else {
115+ return XCTFail ( )
116+ }
117+ if !recievedDictionaryString. contains ( key) && !recievedDictionaryString. contains ( String ( value) ) {
118+ return XCTFail ( )
119+ }
120+ }
121+ }
122+
123+ func testPublishWatchArray( ) {
124+ self . webSocket. connect ( )
125+ let connectionExpectation = expectation ( description: " connection expectation " )
126+ Timer . scheduledTimer ( withTimeInterval: 1.0 , repeats: true ) { ( timer) in
127+ if self . webSocket. getState ( ) == . open {
128+ connectionExpectation. fulfill ( )
129+ timer. invalidate ( )
130+ }
131+ }
132+ wait ( for: [ connectionExpectation] , timeout: 5.0 )
133+
134+ let channelName = " test channel "
135+ let value1 = 30
136+ let value2 = " test "
137+ let currentArray = [ value1, value2] as [ Any ]
138+ _ = self . webSocket. subscribe ( channelName) . publish ( data: currentArray) . watch { ( data) in
139+ guard let recievedArrayString = data as? String else {
140+ return XCTFail ( )
141+ }
142+ if !recievedArrayString. contains ( String ( value1) ) && !recievedArrayString. contains ( value2) {
143+ return XCTFail ( )
144+ }
145+ }
146+ }
147+
148+ func testPublishWatchBoolean( ) {
149+ self . webSocket. connect ( )
150+ let connectionExpectation = expectation ( description: " connection expectation " )
151+ Timer . scheduledTimer ( withTimeInterval: 1.0 , repeats: true ) { ( timer) in
152+ if self . webSocket. getState ( ) == . open {
153+ connectionExpectation. fulfill ( )
154+ timer. invalidate ( )
155+ }
156+ }
157+ wait ( for: [ connectionExpectation] , timeout: 5.0 )
158+
159+ let channelName = " test channel "
160+ let currentBoolean = false
161+ _ = self . webSocket. subscribe ( channelName) . publish ( data: currentBoolean) . watch { ( data) in
162+ guard let recievedBooleanStringNumber = data as? String else {
163+ return XCTFail ( )
164+ }
165+ if recievedBooleanStringNumber == " 0 " || recievedBooleanStringNumber == " 1 " {
166+ XCTAssertEqual ( Int ( recievedBooleanStringNumber) , currentBoolean. hashValue)
167+ } else {
168+ XCTFail ( )
169+ }
72170 }
73171 }
74172
75173 func testUnsubscribe( ) {
76174 self . webSocket. connect ( )
77175 let connectionExpectation = expectation ( description: " connection expectation " )
78- Timer . scheduledTimer ( withTimeInterval: 1.9 , repeats: false ) { ( _ ) in
176+ Timer . scheduledTimer ( withTimeInterval: 1.0 , repeats: true ) { ( timer ) in
79177 if self . webSocket. getState ( ) == . open {
80178 connectionExpectation. fulfill ( )
179+ timer. invalidate ( )
81180 }
82181 }
83- wait ( for: [ connectionExpectation] , timeout: 2.0 )
182+ wait ( for: [ connectionExpectation] , timeout: 5.0 )
183+
84184 let channelName = " test channel "
85185 let subscribedChannel = self . webSocket. subscribe ( channelName)
86186 subscribedChannel. unsubscribe ( )
87- let recievedChannel = self . webSocket. getChannel ( by: channelName)
187+ let recievedChannel = self . webSocket? . getChannel ( by: channelName)
88188 XCTAssertEqual ( nil , recievedChannel)
89189 }
90190}
0 commit comments