Skip to content
This repository was archived by the owner on Nov 2, 2020. It is now read-only.

Commit 90cc20d

Browse files
Added reconnectio tests and repo refactoring work
1 parent d613612 commit 90cc20d

File tree

5 files changed

+130
-44
lines changed

5 files changed

+130
-44
lines changed

Docs/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<h1 align="center">ClusterWS Swift Client</h1>
2+
<h6 align="center">Build Scalable Node.js WebSocket Applications</h6>
3+
4+
<p align="center">
5+
<img src="https://cdn.rawgit.com/goriunov/159120ca6a883d8d4e75543ec395d361/raw/f4c3c36ac1ab75beedcf73312272b60dac33ecfa/clusterws.svg" width="500">
6+
</p>
7+
8+
<p align="center">
9+
<a title="Cocoapod Version" href="https://cocoapods.org/pods/ClusterWS-Client-Swift"><img src="https://img.shields.io/cocoapods/v/ClusterWS-Client-Swift.svg"></a>
10+
<a title="Platforms" href="https://github.com/ClusterWS/ClusterWS-Client-Swift"><img src="https://img.shields.io/cocoapods/p/ClusterWS-Client-Swift.svg"></a>
11+
<a title="License" href="https://github.com/ClusterWS/ClusterWS-Client-Swift/blob/master/LICENSE"><img src="https://img.shields.io/cocoapods/l/ClusterWS-Client-Swift.svg"></a>
12+
</p>
13+
14+
<p align="center">
15+
<i>Official Swift Client library for <a href="https://github.com/ClusterWS/ClusterWS">ClusterWS</a> - lightweight, fast and powerful framework for building horizontally & vertically scalable WebSocket applications in Node.js</i>
16+
</p>
17+
18+
<h1></h1>
19+
<h3 align="center">
20+
<a href="https://github.com/ClusterWS/ClusterWS-Client-Swift/wiki"><strong>ClusterWS Swift Client Documentation</strong></a>
21+
</h3>

README.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

Tests/CWSChannelTests.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//
22
// CWSChannelTests.swift
3-
// ClusterWSTestsTests
3+
// CWSTests
44
//
55
// Created by Roman Baitaliuk on 10/10/17.
66
// Copyright © 2017 ByteKit. All rights reserved.
77
//
88

99
import XCTest
10-
@testable import ClusterWSTests
10+
import ClusterWS_Client_Swift
1111

1212
class CWSChannelTests: XCTestCase {
1313
var webSocket: ClusterWS!
@@ -53,6 +53,25 @@ class CWSChannelTests: XCTestCase {
5353
XCTAssertEqual(channels, recievedChannels)
5454
}
5555

56+
func testPublishAndWatch() {
57+
self.webSocket.connect()
58+
let connectionExpectation = expectation(description: "connection expectation")
59+
Timer.scheduledTimer(withTimeInterval: 1.9, repeats: false) { (_) in
60+
if self.webSocket.getState() == .open {
61+
connectionExpectation.fulfill()
62+
}
63+
}
64+
wait(for: [connectionExpectation], timeout: 2.0)
65+
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 {
69+
return XCTFail()
70+
}
71+
XCTAssertEqual(recievedData, currentData)
72+
}
73+
}
74+
5675
func testUnsubscribe() {
5776
self.webSocket.connect()
5877
let connectionExpectation = expectation(description: "connection expectation")

Tests/CWSReconnectionTests.swift

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// CWSReconnectionTests.swift
3+
// CWSTests
4+
//
5+
// Created by Roman Baitaliuk on 4/02/18.
6+
// Copyright © 2018 ByteKit. All rights reserved.
7+
//
8+
9+
import XCTest
10+
import ClusterWS_Client_Swift
11+
12+
extension CWSReconnectionTests: CWSDelegate {
13+
func onConnect() {
14+
print("Connected")
15+
}
16+
17+
func onDisconnect(code: Int?, reason: String?) {
18+
print("Disconnected")
19+
self.currentAttamts += 1
20+
}
21+
22+
func onError(error: Error) {
23+
print(error.localizedDescription)
24+
}
25+
}
26+
27+
class CWSReconnectionTests: XCTestCase {
28+
var webSocket: ClusterWS!
29+
let reconnectionIntervalMin = 1.0
30+
let reconnectionIntervalMax = 3.0
31+
let reconnectionAttempts = 3
32+
33+
var currentAttamts = 0
34+
35+
override func setUp() {
36+
super.setUp()
37+
// Put setup code here. This method is called before the invocation of each test method in the class.
38+
self.webSocket = ClusterWS(url: "wss://localhost:0000")
39+
self.webSocket.delegate = self
40+
}
41+
42+
override func tearDown() {
43+
// Put teardown code here. This method is called after the invocation of each test method in the class.
44+
super.tearDown()
45+
self.currentAttamts = 0
46+
}
47+
48+
/**
49+
Make sure your localhost server shut down or you're using wrong ClusterWS url.
50+
*/
51+
func testReconnectionAttamts() {
52+
self.webSocket.setReconnection(autoReconnect: true, reconnectionIntervalMin: reconnectionIntervalMin, reconnectionIntervalMax: reconnectionIntervalMax, reconnectionAttempts: reconnectionAttempts)
53+
self.webSocket.connect()
54+
let timeout = reconnectionIntervalMax * Double(reconnectionAttempts)
55+
let connectionExpectation = expectation(description: "connection expectation")
56+
Timer.scheduledTimer(withTimeInterval: timeout - 0.1, repeats: false) { (_) in
57+
if self.currentAttamts == self.reconnectionAttempts {
58+
connectionExpectation.fulfill()
59+
}
60+
}
61+
wait(for: [connectionExpectation], timeout: timeout)
62+
}
63+
64+
/**
65+
Make sure your localhost server shut down or you're using wrong ClusterWS url.
66+
*/
67+
func testAutoReconnect() {
68+
self.webSocket.setReconnection(autoReconnect: true, reconnectionIntervalMin: reconnectionIntervalMin, reconnectionIntervalMax: reconnectionIntervalMax, reconnectionAttempts: reconnectionAttempts)
69+
self.webSocket.connect()
70+
let timeout = reconnectionIntervalMax * Double(reconnectionAttempts)
71+
let connectionExpectation = expectation(description: "connection expectation")
72+
Timer.scheduledTimer(withTimeInterval: timeout - 0.1, repeats: false) { (_) in
73+
if self.currentAttamts > 1 {
74+
connectionExpectation.fulfill()
75+
}
76+
}
77+
wait(for: [connectionExpectation], timeout: timeout)
78+
}
79+
}

Tests/ClusterWSTests.swift

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//
2-
// ClusterWSTestsTests.swift
3-
// ClusterWSTestsTests
2+
// ClusterWSTests.swift
3+
// CWSTests
44
//
55
// Created by Roman Baitaliuk on 9/10/17.
66
// Copyright © 2017 ByteKit. All rights reserved.
77
//
88

99
import XCTest
10-
@testable import ClusterWSTests
10+
import ClusterWS_Client_Swift
1111

1212
class ClusterWSTests: XCTestCase {
1313
var webSocket: ClusterWS!
@@ -56,8 +56,8 @@ class ClusterWSTests: XCTestCase {
5656

5757
let sendOnExpectation = expectation(description: "Send, on expectation result")
5858

59-
self.webSocket.send(event: "String", data: 30)
60-
self.webSocket.on(event: "String") { (data) in
59+
self.webSocket.send(event: "Int", data: 30)
60+
self.webSocket.on(event: "Int") { (data) in
6161
guard ((data as? Int) != nil) else {
6262
return XCTFail()
6363
}
@@ -71,8 +71,8 @@ class ClusterWSTests: XCTestCase {
7171

7272
let sendOnExpectation = expectation(description: "Send, on expectation result")
7373

74-
self.webSocket.send(event: "String", data: [30,23])
75-
self.webSocket.on(event: "String") { (data) in
74+
self.webSocket.send(event: "Array", data: [30,23])
75+
self.webSocket.on(event: "Array") { (data) in
7676
guard ((data as? [Any]) != nil) else {
7777
return XCTFail()
7878
}
@@ -84,8 +84,8 @@ class ClusterWSTests: XCTestCase {
8484
func testSendOnDictionary() {
8585
self.testOnConnect()
8686
let sendOnExpectation = expectation(description: "Send, on expectation result")
87-
self.webSocket.send(event: "String", data: ["id": 0])
88-
self.webSocket.on(event: "String") { (data) in
87+
self.webSocket.send(event: "Dictionary", data: ["id": 0])
88+
self.webSocket.on(event: "Dictionary") { (data) in
8989
guard ((data as? [String: Any]) != nil) else {
9090
return XCTFail()
9191
}
@@ -104,18 +104,6 @@ class ClusterWSTests: XCTestCase {
104104
XCTAssertEqual(self.webSocket.getState(), .open)
105105
}
106106

107-
func testGetChannel() {
108-
self.testOnConnect()
109-
110-
let channelName = "channel name"
111-
let newChannel = self.webSocket.subscribe(channelName)
112-
guard let recievedChannel = self.webSocket.getChannel(by: channelName) else {
113-
return XCTFail()
114-
}
115-
116-
XCTAssertEqual(newChannel, recievedChannel)
117-
}
118-
119107
func testDisconnect() {
120108
self.testOnConnect()
121109

0 commit comments

Comments
 (0)