Skip to content

Commit

Permalink
Merge pull request #206 from ogres/MQTTSwift-Swift3
Browse files Browse the repository at this point in the history
#202 Converted MQTTClientTests / SwiftTests to Swift 3
  • Loading branch information
Christoph Krey authored Sep 29, 2016
2 parents 1efc5a2 + 6b864de commit 7a20ed0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
2 changes: 2 additions & 0 deletions MQTTClient/MQTTClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "MQTTClientTests/MQTTClientTests-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
WRAPPER_EXTENSION = xctest;
};
name = Debug;
Expand All @@ -1608,6 +1609,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.gmail.cky6201.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "MQTTClientTests/MQTTClientTests-Bridging-Header.h";
SWIFT_VERSION = 3.0;
WRAPPER_EXTENSION = xctest;
};
name = Release;
Expand Down
81 changes: 43 additions & 38 deletions MQTTClient/MQTTClientTests/SwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class SwiftTests : MQTTTestHelpers {
var sessionReceived = false;
var sessionSubAcked = false;
var broker: NSDictionary = NSDictionary();


override func setUp() {
super.setUp();
}
Expand All @@ -26,35 +26,35 @@ class SwiftTests : MQTTTestHelpers {

func testSwiftSubscribe() {
print("testSwiftSubscribe \(brokers)")

for brokerName in brokers.allKeys {
print("testSwiftSubscribe \(brokerName)")

broker = brokers.value(forKey: brokerName as! String) as! NSDictionary;
print("testSwiftSubscribe \(broker)")

if ((broker.value(forKey: "websocket")) as AnyObject).boolValue != true {

session = MQTTSession();
session!.delegate = self;

session!.connect(toHost: broker.value(forKey: "host") as! String,
port:((broker.value(forKey: "port") as AnyObject).uint32Value)!,
usingSSL: ((broker.value(forKey: "tls") as AnyObject).boolValue)!);
port: UInt32(broker.value(forKey: "port") as! Int),
usingSSL: broker.value(forKey: "tls") as! Bool)
while !sessionConnected && !sessionError {
RunLoop.current.run(until: Date(timeIntervalSinceNow: 1))
}

session!.subscribe(toTopic: "#", at: .atMostOnce)

while sessionConnected && !sessionError && !sessionSubAcked {
RunLoop.current.run(until: Date(timeIntervalSinceNow: 1))
}

session!.publishData("sent from Xcode 8.0 using Swift".data(using: String.Encoding.utf8, allowLossyConversion: false),
onTopic: TOPIC,
retain: false,
qos: .atMostOnce)
onTopic: TOPIC,
retain: false,
qos: .atMostOnce)

while sessionConnected && !sessionError && !sessionReceived {
RunLoop.current.run(until: Date(timeIntervalSinceNow: 1))
Expand All @@ -68,35 +68,40 @@ class SwiftTests : MQTTTestHelpers {
func testSessionManager() {
for brokerName in brokers.allKeys {
broker = brokers.value(forKey: brokerName as! String) as! NSDictionary;
if ((broker.value(forKey: "websocket")) as AnyObject).boolValue != true {

let m = MQTTSessionManager()
m.delegate = self

m.connect(to: broker.value(forKey: "host") as! String,
port: ((broker.value(forKey: "port") as AnyObject).integerValue)!,
tls: ((broker.value(forKey: "tls") as AnyObject).boolValue)!,
keepalive: 60,
clean: true,
auth: false,
user: nil,
pass: nil,
will: false,
willTopic: nil,
willMsg: nil,
willQos: .atMostOnce,
willRetainFlag: false,
withClientId: nil)

while (m.state != .connected) {
print("waiting for connect %d", m.state);
RunLoop.current.run(until: Date(timeIntervalSinceNow: 1))
}

if let websocket = broker.value(forKey: "websocket") as? Bool, websocket == true {
continue
}

let m = MQTTSessionManager()
m.delegate = self

m.connect(to: broker.value(forKey: "host") as! String,
port: broker.value(forKey: "port") as! Int,
tls: broker.value(forKey: "tls") as! Bool,
keepalive: 60,
clean: true,
auth: false,
user: nil,
pass: nil,
will: false,
willTopic: nil,
willMsg: nil,
willQos: .atMostOnce,
willRetainFlag: false,
withClientId: nil,
securityPolicy: MQTTTestHelpers.securityPolicy(broker as! [AnyHashable: Any]),
certificates: MQTTTestHelpers.clientCerts(broker as! [AnyHashable: Any])
)

while (m.state != .connected) {
print("waiting for connect %d", m.state);
RunLoop.current.run(until: Date(timeIntervalSinceNow: 1))
}

}
}

override func handleEvent(_ session: MQTTSession!, event eventCode: MQTTSessionEvent, error: Error!) {
switch eventCode {
case .connected:
Expand All @@ -107,12 +112,12 @@ class SwiftTests : MQTTTestHelpers {
sessionError = true
}
}

override func newMessage(_ session: MQTTSession!, data: Data!, onTopic topic: String!, qos: MQTTQosLevel, retained: Bool, mid: UInt32) {
print("Received \(data) on:\(topic) q\(qos) r\(retained) m\(mid)")
sessionReceived = true;
}

override func subAckReceived(_ session: MQTTSession!, msgID: UInt16, grantedQoss qoss: [NSNumber]!) {
sessionSubAcked = true;
}
Expand Down

0 comments on commit 7a20ed0

Please sign in to comment.