Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local server not responding to requests #4

Open
Chandlerdea opened this issue Aug 1, 2016 · 1 comment
Open

Local server not responding to requests #4

Chandlerdea opened this issue Aug 1, 2016 · 1 comment

Comments

@Chandlerdea
Copy link
Contributor

I am able to connect to the local server on port 8080, but it does not respond to requests. Here is what it looks like when I curl:

screen shot 2016-07-31 at 8 12 43 pm

I used the code from your medium post to setup and tear down the local server:

`private extension UITests {

func startHTTPServer() {
    self.eventLoop = try! SelectorEventLoop(selector: try! KqueueSelector())
    self.router = DefaultRouter()
    self.server = HTTPServer(eventLoop: self.eventLoop, app: self.router.app, port: self.port)
    try! self.server.start()
    self.eventLoopThreadCondition = NSCondition()
    self.eventLoopThread = NSThread(target: self, selector: #selector(self.runEventLoop), object: nil)
    self.eventLoopThread.start()
}

func stopHTTPServer() {
    self.server.stopAndWait()
    self.eventLoopThreadCondition.lock()
    self.eventLoop.stop()
    while self.eventLoop.running {
        if !self.eventLoopThreadCondition.waitUntilDate(NSDate().dateByAddingTimeInterval(10)) {
            fatalError("Join eventLoopThread timeout")
        }
    }
}

@objc func runEventLoop() {
    self.eventLoop.runForever()
    self.eventLoopThreadCondition.lock()
    self.eventLoopThreadCondition.signal()
    self.eventLoopThreadCondition.unlock()
}

}`

Let me know if you have any questions that I can answer.

@fangpenlin
Copy link
Contributor

fangpenlin commented Aug 2, 2016

Hi @Chandlerdea,

I cannot reproduce the bug.

Here's the code I wrote

import XCTest

import Embassy
import EnvoyAmbassador

class ServerTest: XCTestCase {
  let port = 8080
  var router: Router!
  var eventLoop: EventLoopType!
  var server: HTTPServer!
  var app: XCUIApplication!

  var eventLoopThreadCondition: NSCondition!
  var eventLoopThread: NSThread!

  func startHTTPServer() {
    self.eventLoop = try! SelectorEventLoop(selector: try! KqueueSelector())
    self.router = Router()
    self.server = HTTPServer(eventLoop: self.eventLoop, app: self.router.app, port: self.port)
    try! self.server.start()
    self.eventLoopThreadCondition = NSCondition()
    self.eventLoopThread = NSThread(target: self, selector: #selector(self.runEventLoop), object: nil)
    self.eventLoopThread.start()
  }

  func stopHTTPServer() {
    self.server.stopAndWait()
    self.eventLoopThreadCondition.lock()
    self.eventLoop.stop()
    while self.eventLoop.running {
      if !self.eventLoopThreadCondition.waitUntilDate(NSDate().dateByAddingTimeInterval(10)) {
        fatalError("Join eventLoopThread timeout")
      }
    }
  }

  @objc func runEventLoop() {
    self.eventLoop.runForever()
    self.eventLoopThreadCondition.lock()
    self.eventLoopThreadCondition.signal()
    self.eventLoopThreadCondition.unlock()
  }

  func testServer() {
    startHTTPServer()

    // run server for ever
    let exp = expectationWithDescription("")
    waitForExpectationsWithTimeout(999999, handler: nil)
  }

}

And it works perfectly fine to return a 404 error

$ curl -v 'http://localhost:8080/api/me'
*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /api/me HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 404 Not found
< Content-Type: application/octet-stream
< Content-Length: 0
< Connection: close
< Server: Embassy
< 
* Closing connection 0

Did you try to hold the test case a little while like what I did?

    let exp = expectationWithDescription("")
    waitForExpectationsWithTimeout(999999, handler: nil)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants