Skip to content

Commit e266e7b

Browse files
Stanislav DimitrovStanislav Dimitrov
authored andcommitted
fix messing handle instances in tests
1 parent 7f6a977 commit e266e7b

File tree

3 files changed

+70
-62
lines changed

3 files changed

+70
-62
lines changed

Example/Tests/MockPubSubSocket.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class MockPubSubSocket: Socket {
2020

2121
init(keys: [String], options: PubSubOptions?) {
2222
self.keys = keys
23-
23+
2424
if let ops = options {
2525
self.options = ops
2626
} else {
@@ -59,7 +59,7 @@ final class MockPubSubSocket: Socket {
5959
if readKeyIndex != nil {
6060
self.channels.append(params["channel"] as! String)
6161

62-
response = [
62+
response = [
6363
"seq": params["seq"] as Any,
6464
"action": PubSubAction.subscribe.rawValue,
6565
"code": PubSubResponseCode.success.rawValue,
@@ -176,7 +176,7 @@ final class MockPubSubSocket: Socket {
176176
let writeKeyIndex = keys.index(where: { $0.contains("W") })
177177

178178
if writeKeyIndex != nil {
179-
response = [
179+
response = [
180180
"id": UUID().uuidString.lowercased(),
181181
"action": PubSubAction.message.rawValue,
182182
"time": Date().toISO8601,
@@ -237,8 +237,8 @@ final class MockPubSubSocket: Socket {
237237
let jsonData = try JSONSerialization.data(withJSONObject: response, options: .init(rawValue: 0))
238238

239239
let stringResponse = String(NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue)!)
240-
241-
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
240+
241+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
242242
self.onText?(stringResponse)
243243
}
244244
} catch {

Example/Tests/PubSubFullSweepTests.swift

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Nimble
44
@testable import CogsSDK
55

66
class PubSubFullSweepTests: QuickSpec {
7-
7+
88
override func spec() {
99
let testChannel = "Test channel"
1010
let testMessage = "Test message"
@@ -37,25 +37,6 @@ class PubSubFullSweepTests: QuickSpec {
3737

3838
let defaultOptions = PubSubOptions.defaultOptions
3939

40-
func getSessionUUID(_ connectionHandle: PubSubConnectionHandle, completion: @escaping (String) -> Void) {
41-
connectionHandle.getSessionUuid() { outcome in
42-
switch outcome {
43-
case .pubSubSuccess(let object):
44-
if let uuid = object as? String {
45-
expect(uuid).toNot(beNil())
46-
expect(uuid).toNot(beEmpty())
47-
48-
completion(uuid)
49-
} else {
50-
fail("Expected String, got \(object)")
51-
}
52-
53-
default:
54-
fail("Expected success, got error response")
55-
}
56-
}
57-
}
58-
5940
describe("Full Sweep Test") {
6041

6142
it("pubsub successfully connects, subscribes, lists subscribtions, publishes and receives message, closes the connection") {
@@ -117,10 +98,10 @@ class PubSubFullSweepTests: QuickSpec {
11798

11899
default:
119100
fail("Expected success, got error response")
120-
101+
121102
done()
122103
}
123-
104+
124105
connectionHandle.close()
125106

126107
connectionHandle.onClose = { (error) in
@@ -245,23 +226,35 @@ class PubSubFullSweepTests: QuickSpec {
245226

246227
waitUntil(timeout: 15) { done in
247228
let socket = PubSubSocket(keys: allKeys, options: defaultOptions)
248-
let connectionHandle = PubSubService.connect(socket: socket)
229+
var connectionHandle: PubSubConnectionHandle! = PubSubService.connect(socket: socket)
249230

250-
connectionHandle.onNewSession = { _ in
251-
getSessionUUID(connectionHandle) { uuid in
252-
sessionUUID = uuid
231+
connectionHandle.onNewSession = { uuid in
232+
sessionUUID = uuid
253233

254-
connectionHandle.dropConnection()
255-
}
234+
connectionHandle.dropConnection()
256235
}
257236

258237
connectionHandle.onReconnect = {
259-
getSessionUUID(connectionHandle) { uuid in
238+
connectionHandle.getSessionUuid { outcome in
239+
switch outcome {
240+
case .pubSubSuccess(let object):
241+
if let uuid = object as? String {
242+
expect(uuid).toNot(beNil())
243+
expect(uuid).toNot(beEmpty())
260244

261-
expect(uuid == sessionUUID).to(beTruthy())
245+
expect(uuid == sessionUUID).to(beTruthy())
262246

263-
connectionHandle.close()
264-
done()
247+
connectionHandle.close()
248+
connectionHandle = nil
249+
done()
250+
251+
} else {
252+
fail("Expected String, got \(object)")
253+
}
254+
255+
default:
256+
fail("Expected success, got error response")
257+
}
265258
}
266259
}
267260
}
@@ -294,22 +287,36 @@ class PubSubFullSweepTests: QuickSpec {
294287
describe("Get Session Uuid Test") {
295288

296289
it("returns the same uuid when session is restored") {
290+
var oldUuid: String!
291+
297292
waitUntil(timeout: 15) { done in
298293
let socket = PubSubSocket(keys: allKeys, options: defaultOptions)
299-
let connectionHandle = PubSubService.connect(socket: socket)
294+
var connectionHandle: PubSubConnectionHandle! = PubSubService.connect(socket: socket)
300295

301-
connectionHandle.onNewSession = { _ in
302-
getSessionUUID(connectionHandle) { oldUuid in
296+
connectionHandle.onNewSession = { uuid in
297+
oldUuid = uuid
298+
connectionHandle.dropConnection()
303299

304-
connectionHandle.dropConnection()
300+
connectionHandle.onReconnect = {
301+
connectionHandle.getSessionUuid { outcome in
302+
switch outcome {
303+
case .pubSubSuccess(let object):
304+
if let newUuid = object as? String {
305+
expect(newUuid).toNot(beNil())
306+
expect(newUuid).toNot(beEmpty())
305307

306-
connectionHandle.onReconnect = {
307-
getSessionUUID(connectionHandle) { newUuid in
308+
expect(oldUuid == newUuid).to(beTruthy())
308309

309-
expect(oldUuid == newUuid).to(beTruthy())
310+
connectionHandle.close()
311+
connectionHandle = nil
312+
done()
310313

311-
connectionHandle.close()
312-
done()
314+
} else {
315+
fail("Expected String, got \(object)")
316+
}
317+
318+
default:
319+
fail("Expected success, got error response")
313320
}
314321
}
315322
}
@@ -401,7 +408,7 @@ class PubSubFullSweepTests: QuickSpec {
401408
}
402409
}
403410
}
404-
411+
405412
context("when write key is not supplied") {
406413
var isEmitting: Bool = false
407414
var error: PubSubErrorResponse?
@@ -414,7 +421,7 @@ class PubSubFullSweepTests: QuickSpec {
414421
connectionHandle.onNewSession = { _ in
415422
connectionHandle.publish(channel: testChannel, message: testMessage) { _ in }
416423
}
417-
424+
418425
connectionHandle.onErrorResponse = { responseError in
419426
isEmitting = true
420427
error = responseError
@@ -423,19 +430,19 @@ class PubSubFullSweepTests: QuickSpec {
423430
done()
424431
}
425432
}
426-
433+
427434
waitUntil(timeout: 11) { done in
428435
expect(isEmitting == true).to(beTruthy())
429436
expect(error).toNot(beNil())
430437
expect(error!.action == PubSubAction.publish.rawValue).to(beTruthy())
431438
expect(error!.code == PubSubResponseCode.unauthorised.rawValue).to(beTruthy())
432-
439+
433440
done()
434441
}
435442
}
436443
}
437444
}
438-
445+
439446
describe("onReconnect Test") {
440447
var isEmitting: Bool = false
441448

@@ -455,10 +462,10 @@ class PubSubFullSweepTests: QuickSpec {
455462
done()
456463
}
457464
}
458-
465+
459466
waitUntil(timeout: 16) { done in
460467
expect(isEmitting == true).to(beTruthy())
461-
468+
462469
done()
463470
}
464471
}
@@ -468,18 +475,18 @@ class PubSubFullSweepTests: QuickSpec {
468475

469476
it("emits close event") {
470477
var isEmitting: Bool = false
471-
478+
472479
waitUntil(timeout: defaultTimeout) { done in
473480
let socket = PubSubSocket(keys: allKeys, options: defaultOptions)
474481
let connectionHandle = PubSubService.connect(socket: socket)
475-
482+
476483
connectionHandle.onNewSession = { _ in
477484
connectionHandle.close()
478485
}
479486

480487
connectionHandle.onClose = { (error) in
481488
isEmitting = true
482-
489+
483490
connectionHandle.close()
484491
done()
485492
}
@@ -494,17 +501,17 @@ class PubSubFullSweepTests: QuickSpec {
494501
}
495502

496503
describe("onNewSession Test") {
497-
504+
498505
it("emits on new session event") {
499506
var isEmitting: Bool = false
500-
507+
501508
waitUntil(timeout: defaultTimeout) { done in
502509
let socket = PubSubSocket(keys: allKeys, options: defaultOptions)
503510
let connectionHandle = PubSubService.connect(socket: socket)
504511

505512
connectionHandle.onNewSession = { _ in
506513
isEmitting = true
507-
514+
508515
connectionHandle.close()
509516
done()
510517
}

Example/Tests/PubSubUnitTests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class PubSubUnitTests: QuickSpec {
3636

3737
describe("get sessionUUID") {
3838

39-
xit("returns sessionUUID") {
39+
it("returns sessionUUID") {
4040
waitUntil(timeout: defaultTimeout) { done in
4141
let socket = MockPubSubSocket(keys: allKeys, options: defaultOptions)
42-
let connectionHandle = PubSubService.connect(socket: socket)
42+
var connectionHandle: PubSubConnectionHandle! = PubSubService.connect(socket: socket)
4343
connectionHandle.onNewSession = { _ in
4444
connectionHandle.getSessionUuid() { outcome in
4545
switch outcome {
@@ -52,7 +52,7 @@ class PubSubUnitTests: QuickSpec {
5252
}
5353

5454
connectionHandle.close()
55-
55+
connectionHandle = nil
5656
done()
5757

5858
case .pubSubResponseError(let errorResponse):
@@ -61,6 +61,7 @@ class PubSubUnitTests: QuickSpec {
6161
expect(errorResponse.code).toNot(equal(PubSubResponseCode.success.rawValue))
6262

6363
connectionHandle.close()
64+
connectionHandle = nil
6465
done()
6566
}
6667
}

0 commit comments

Comments
 (0)