Skip to content

Commit

Permalink
temp make notify/listen internal
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed Apr 25, 2018
1 parent c057de6 commit 3fb6e2f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import Async
extension PostgreSQLConnection {
/// Note: after calling `listen'` on a connection, it can no longer handle other database operations. Do not try to send other SQL commands through this connection afterwards.
/// IAlso, notifications will only be sent for as long as this connection remains open; you are responsible for opening a new connection to listen on when this one closes.
public func listen(
_ channelName: String,
handler: @escaping (String) throws -> ()
) throws -> Future<Void> {
internal func listen(_ channelName: String, handler: @escaping (String) throws -> ()) throws -> Future<Void> {
closeHandlers.append({ conn in
let query = PostgreSQLQuery(query: "UNLISTEN \"\(channelName)\";")
return conn.send([.query(query)], onResponse: { _ in })
Expand All @@ -28,13 +25,12 @@ extension PostgreSQLConnection {
})
}

public func notify(
_ channelName: String, message: String) throws -> Future<Void> {
internal func notify(_ channelName: String, message: String) throws -> Future<Void> {
let query = PostgreSQLQuery(query: "NOTIFY \"\(channelName)\", '\(message)';")
return send([.query(query)]).map(to: Void.self, { _ in })
}

public func unlisten(_ channelName: String, unlistenHandler: (() -> Void)? = nil) throws -> Future<Void> {
internal func unlisten(_ channelName: String, unlistenHandler: (() -> Void)? = nil) throws -> Future<Void> {
notificationHandlers.removeValue(forKey: channelName)
let query = PostgreSQLQuery(query: "UNLISTEN \"\(channelName)\";")
return send([.query(query)], onResponse: { _ in unlistenHandler?() })
Expand Down
164 changes: 82 additions & 82 deletions Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,85 +343,85 @@ class PostgreSQLConnectionTests: XCTestCase {
_ = try categories.wait()
}

func testNotifyAndListen() throws {
let completionHandlerExpectation1 = expectation(description: "first completion handler called")
let completionHandlerExpectation2 = expectation(description: "final completion handler called")
let notifyConn = try PostgreSQLConnection.makeTest()
let listenConn = try PostgreSQLConnection.makeTest()
let channelName = "Fooze"
let messageText = "Bar"
let finalMessageText = "Baz"

try listenConn.listen(channelName) { text in
if text == messageText {
completionHandlerExpectation1.fulfill()
} else if text == finalMessageText {
completionHandlerExpectation2.fulfill()
}
}.catch({ err in XCTFail("error \(err)") })

try notifyConn.notify(channelName, message: messageText).wait()
try notifyConn.notify(channelName, message: finalMessageText).wait()

waitForExpectations(timeout: defaultTimeout)
notifyConn.close()
listenConn.close()
}

func testNotifyAndListenOnMultipleChannels() throws {
let completionHandlerExpectation1 = expectation(description: "first completion handler called")
let completionHandlerExpectation2 = expectation(description: "final completion handler called")
let notifyConn = try PostgreSQLConnection.makeTest()
let listenConn = try PostgreSQLConnection.makeTest()
let channelName = "Fooze"
let channelName2 = "Foozalz"
let messageText = "Bar"
let finalMessageText = "Baz"

try listenConn.listen(channelName) { text in
if text == messageText {
completionHandlerExpectation1.fulfill()
}
}.catch({ err in XCTFail("error \(err)") })

try listenConn.listen(channelName2) { text in
if text == finalMessageText {
completionHandlerExpectation2.fulfill()
}
}.catch({ err in XCTFail("error \(err)") })

try notifyConn.notify(channelName, message: messageText).wait()
try notifyConn.notify(channelName2, message: finalMessageText).wait()

waitForExpectations(timeout: defaultTimeout)
notifyConn.close()
listenConn.close()
}

func testUnlisten() throws {
let unlistenHandlerExpectation = expectation(description: "unlisten completion handler called")

let listenHandlerExpectation = expectation(description: "listen completion handler called")

let notifyConn = try PostgreSQLConnection.makeTest()
let listenConn = try PostgreSQLConnection.makeTest()
let channelName = "Foozers"
let messageText = "Bar"

try listenConn.listen(channelName) { text in
if text == messageText {
listenHandlerExpectation.fulfill()
}
}.catch({ err in XCTFail("error \(err)") })

try notifyConn.notify(channelName, message: messageText).wait()
try notifyConn.unlisten(channelName, unlistenHandler: {
unlistenHandlerExpectation.fulfill()
}).wait()
waitForExpectations(timeout: defaultTimeout)
notifyConn.close()
listenConn.close()
}
// func testNotifyAndListen() throws {
// let completionHandlerExpectation1 = expectation(description: "first completion handler called")
// let completionHandlerExpectation2 = expectation(description: "final completion handler called")
// let notifyConn = try PostgreSQLConnection.makeTest()
// let listenConn = try PostgreSQLConnection.makeTest()
// let channelName = "Fooze"
// let messageText = "Bar"
// let finalMessageText = "Baz"
//
// try listenConn.listen(channelName) { text in
// if text == messageText {
// completionHandlerExpectation1.fulfill()
// } else if text == finalMessageText {
// completionHandlerExpectation2.fulfill()
// }
// }.catch({ err in XCTFail("error \(err)") })
//
// try notifyConn.notify(channelName, message: messageText).wait()
// try notifyConn.notify(channelName, message: finalMessageText).wait()
//
// waitForExpectations(timeout: defaultTimeout)
// notifyConn.close()
// listenConn.close()
// }
//
// func testNotifyAndListenOnMultipleChannels() throws {
// let completionHandlerExpectation1 = expectation(description: "first completion handler called")
// let completionHandlerExpectation2 = expectation(description: "final completion handler called")
// let notifyConn = try PostgreSQLConnection.makeTest()
// let listenConn = try PostgreSQLConnection.makeTest()
// let channelName = "Fooze"
// let channelName2 = "Foozalz"
// let messageText = "Bar"
// let finalMessageText = "Baz"
//
// try listenConn.listen(channelName) { text in
// if text == messageText {
// completionHandlerExpectation1.fulfill()
// }
// }.catch({ err in XCTFail("error \(err)") })
//
// try listenConn.listen(channelName2) { text in
// if text == finalMessageText {
// completionHandlerExpectation2.fulfill()
// }
// }.catch({ err in XCTFail("error \(err)") })
//
// try notifyConn.notify(channelName, message: messageText).wait()
// try notifyConn.notify(channelName2, message: finalMessageText).wait()
//
// waitForExpectations(timeout: defaultTimeout)
// notifyConn.close()
// listenConn.close()
// }
//
// func testUnlisten() throws {
// let unlistenHandlerExpectation = expectation(description: "unlisten completion handler called")
//
// let listenHandlerExpectation = expectation(description: "listen completion handler called")
//
// let notifyConn = try PostgreSQLConnection.makeTest()
// let listenConn = try PostgreSQLConnection.makeTest()
// let channelName = "Foozers"
// let messageText = "Bar"
//
// try listenConn.listen(channelName) { text in
// if text == messageText {
// listenHandlerExpectation.fulfill()
// }
// }.catch({ err in XCTFail("error \(err)") })
//
// try notifyConn.notify(channelName, message: messageText).wait()
// try notifyConn.unlisten(channelName, unlistenHandler: {
// unlistenHandlerExpectation.fulfill()
// }).wait()
// waitForExpectations(timeout: defaultTimeout)
// notifyConn.close()
// listenConn.close()
// }

func testURLParsing() throws {
let databaseURL = "postgres://username:[email protected]:5432/database"
Expand All @@ -442,9 +442,9 @@ class PostgreSQLConnectionTests: XCTestCase {
("testStruct", testStruct),
("testNull", testNull),
("testGH24", testGH24),
("testNotifyAndListen", testNotifyAndListen),
("testNotifyAndListenOnMultipleChannels", testNotifyAndListenOnMultipleChannels),
("testUnlisten", testUnlisten),
// ("testNotifyAndListen", testNotifyAndListen),
// ("testNotifyAndListenOnMultipleChannels", testNotifyAndListenOnMultipleChannels),
// ("testUnlisten", testUnlisten),
("testURLParsing", testURLParsing),
]
}
Expand Down

0 comments on commit 3fb6e2f

Please sign in to comment.