Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions Projects/SSEKit/SSEKit/SSEManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ open class SSEManager : NSObject, URLSessionDelegate {
/// connect to the given SSE endpoint
///
/// - Parameter url: url of the product probably http://<productip>:15081/notify
func connect(toURL url: URL, completion: CompletionClosure? = nil ) {
open func connect(toURL url: URL, completion: CompletionClosure? = nil ) {
_ = self.queue.async {

guard self.connectionState == .idle else {
Expand Down Expand Up @@ -117,9 +117,9 @@ open class SSEManager : NSObject, URLSessionDelegate {
/// disconnect the SSE connection socket
///
/// - Parameter completion: completion closure
public func disconnect(completion:@escaping ()->() = {}) {
open func disconnect(completion:@escaping ()->() = {}) {

_ = self.queue.async {
self.queue.async {
guard self.connectionState != .disconnecting && self.connectionState != .idle else {
completion()
return
Expand All @@ -142,10 +142,10 @@ open class SSEManager : NSObject, URLSessionDelegate {
self.eventSources.forEach({ (eventSource) in
NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: Notification.Disconnected.rawValue), object: eventSource, userInfo: [ Notification.Key.Source.rawValue : self.connectionURL!.absoluteString ])
})

NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: Notification.Disconnected.rawValue), object: self, userInfo: [ Notification.Key.Source.rawValue : self.connectionURL!.absoluteString ])
}

NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: Notification.Disconnected.rawValue), object: self, userInfo: [ Notification.Key.Source.rawValue : self.connectionURL!.absoluteString ])

completion()
}
}
Expand Down Expand Up @@ -217,7 +217,7 @@ open class SSEManager : NSObject, URLSessionDelegate {
/// remove all the listening event sources
///
/// - Parameter completion: completion closure
open func removeAllEventSources(_ completion:@escaping ()->()) {
open func removeAllEventSources(_ completion:@escaping ()->() = {}) {

self.queue.async {

Expand Down Expand Up @@ -251,13 +251,17 @@ extension SSEManager: URLSessionDataDelegate {
return
}

self.connectionState = .idle
// session ended...
self.connectionRetries = self.connectionRetries + 1
if (self.connectionRetries < self.maxConnectionRetries) {
self.connect(toURL: url)
self.connectionState = .idle
self.connect(toURL: url, completion: self.connectCompletionClosure)
}
else {
DispatchQueue.main.sync {
self.connectCompletionClosure?(error as NSError?)
self.connectCompletionClosure = nil
}
self.disconnect() {
}
}
Expand Down
52 changes: 51 additions & 1 deletion Projects/SSEKit/SSEKitTests/SSEKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class SSEKitTests: XCTestCase {
disconnectEventExpectation.fulfill()
}

self.waitForExpectations(timeout: 50) { (error) in
self.waitForExpectations(timeout: 15) { (error) in
XCTAssertNil(error)

NotificationCenter.default.removeObserver(observer)
Expand Down Expand Up @@ -127,6 +127,56 @@ class SSEKitTests: XCTestCase {
NotificationCenter.default.removeObserver(cheeseObserver)
}
}

func testConnectNOK() {
let connectResponseExpectation = self.expectation(description: "should call connect closure")


let manager = SSEManager()

manager.maxConnectionRetries = 2 // or test takes too long!

// connect to an invalid ip (get a request timed out)
manager.connect(toURL: URL(string: "http://999.999.999.999:15081/notify")!, completion: { (error) in
XCTAssert(error != nil)
XCTAssert(Thread.isMainThread, "closure should be on main thread")
connectResponseExpectation.fulfill()
})

self.waitForExpectations(timeout: 15) { (error) in
XCTAssertNil(error)
}
}


func testConnectOKThenFail() {
let connectResponseExpectation = self.expectation(description: "should call connect closure")
let disconnectEventExpectation = self.expectation(description: "should disconnect")

let manager = SSEManager()

manager.maxConnectionRetries = 0 // stop retries

manager.connect(toURL: DUT_SSE_Endpoint, completion: { (error) in
XCTAssert(error == nil)
XCTAssert(Thread.isMainThread, "closure should be on main thread")
connectResponseExpectation.fulfill()

// force an error
manager.urlSession(manager.session!, task: manager.sessionTask!, didCompleteWithError: NSError(domain: "com.naim.ssekittest", code: 1, userInfo: [:]) as Error)
})

let disconnectObserver = NotificationCenter.default.addObserver(forName: Notification.Name(SSEManager.Notification.Disconnected.rawValue), object:manager, queue: nil) { (notification) in
XCTAssert(Thread.isMainThread, "Notifications should be on main thread")
disconnectEventExpectation.fulfill()
}

self.waitForExpectations(timeout: 15) { (error) in
XCTAssertNil(error)
XCTAssert(manager.connectionState == SSEManager.ConnectionState.idle, "should be idle")
NotificationCenter.default.removeObserver(disconnectObserver)
}
}

func testPerformanceExample() {
// This is an example of a performance test case.
Expand Down