Last active
July 27, 2016 23:07
-
-
Save fangpenlin/b180cc4a3c7bbe123ee298b03067b6ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import XCTest | |
import Embassy | |
import EnvoyAmbassador | |
class UITestBase: XCTestCase { | |
let port = 8080 | |
var router: Router! | |
var eventLoop: EventLoopType! | |
var server: HTTPServer! | |
var app: XCUIApplication! | |
var eventLoopThreadCondition: NSCondition! | |
var eventLoopThread: NSThread! | |
override func setUp() { | |
super.setUp() | |
setupWebApp() | |
setupApp() | |
} | |
// setup the Embassy web server for testing | |
private func setupWebApp() { | |
eventLoop = try! SelectorEventLoop(selector: try! KqueueSelector()) | |
router = DefaultRouter() | |
server = HTTPServer(eventLoop: eventLoop, app: router.app, port: port) | |
// Start HTTP server to listen on the port | |
try! server.start() | |
eventLoopThreadCondition = NSCondition() | |
eventLoopThread = NSThread(target: self, selector: #selector(runEventLoop), object: nil) | |
eventLoopThread.start() | |
} | |
// set up XCUIApplication | |
private func setupApp() { | |
app = XCUIApplication() | |
app.launchEnvironment["RESET_LOGIN"] = "1" | |
app.launchEnvironment["ENVOY_BASEURL"] = "http://localhost:\(port)" | |
} | |
override func tearDown() { | |
super.tearDown() | |
app.terminate() | |
server.stopAndWait() | |
eventLoopThreadCondition.lock() | |
eventLoop.stop() | |
while eventLoop.running { | |
if !eventLoopThreadCondition.waitUntilDate(NSDate().dateByAddingTimeInterval(10)) { | |
fatalError("Join eventLoopThread timeout") | |
} | |
} | |
} | |
@objc private func runEventLoop() { | |
eventLoop.runForever() | |
eventLoopThreadCondition.lock() | |
eventLoopThreadCondition.signal() | |
eventLoopThreadCondition.unlock() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment