envoy / Ambassador

Super lightweight web framework in Swift based on SWSGI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Local server not responding to requests

Chandlerdea opened this issue · comments

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.

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)