1
0

ServerThreadingTests.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // ServerThreadingTests.swift
  3. // Swifter
  4. //
  5. // Created by Victor Sigler on 4/22/19.
  6. // Copyright © 2019 Damian Kołakowski. All rights reserved.
  7. //
  8. import XCTest
  9. @testable import Swifter
  10. class ServerThreadingTests: XCTestCase {
  11. var server: HttpServer!
  12. override func setUp() {
  13. super.setUp()
  14. server = HttpServer()
  15. }
  16. override func tearDown() {
  17. if server.operating {
  18. server.stop()
  19. }
  20. server = nil
  21. super.tearDown()
  22. }
  23. func testShouldHandleTheSameRequestWithDifferentTimeIntervals() {
  24. let path = "/a/:b/c"
  25. let queue = DispatchQueue(label: "com.swifter.threading")
  26. let hostURL: URL
  27. server.GET[path] = { .ok(.html("You asked for " + $0.path)) }
  28. do {
  29. #if os(Linux)
  30. try server.start(9081)
  31. hostURL = URL(string: "http://localhost:9081")!
  32. #else
  33. try server.start()
  34. hostURL = defaultLocalhost
  35. #endif
  36. let requestExpectation = expectation(description: "Request should finish.")
  37. requestExpectation.expectedFulfillmentCount = 3
  38. (1...3).forEach { index in
  39. queue.asyncAfter(deadline: .now() + .seconds(index)) {
  40. let task = URLSession.shared.executeAsyncTask(hostURL: hostURL, path: path) { (_, response, _ ) in
  41. requestExpectation.fulfill()
  42. let statusCode = (response as? HTTPURLResponse)?.statusCode
  43. XCTAssertNotNil(statusCode)
  44. XCTAssertEqual(statusCode, 200, "\(hostURL)")
  45. }
  46. task.resume()
  47. }
  48. }
  49. } catch let error {
  50. XCTFail("\(error)")
  51. }
  52. waitForExpectations(timeout: 10, handler: nil)
  53. }
  54. func testShouldHandleTheSameRequestConcurrently() {
  55. let path = "/a/:b/c"
  56. server.GET[path] = { .ok(.html("You asked for " + $0.path)) }
  57. var requestExpectation: XCTestExpectation? = expectation(description: "Should handle the request concurrently")
  58. do {
  59. try server.start()
  60. let downloadGroup = DispatchGroup()
  61. DispatchQueue.concurrentPerform(iterations: 3) { _ in
  62. downloadGroup.enter()
  63. let task = URLSession.shared.executeAsyncTask(path: path) { (_, response, _ ) in
  64. let statusCode = (response as? HTTPURLResponse)?.statusCode
  65. XCTAssertNotNil(statusCode)
  66. XCTAssertEqual(statusCode, 200)
  67. requestExpectation?.fulfill()
  68. requestExpectation = nil
  69. downloadGroup.leave()
  70. }
  71. task.resume()
  72. }
  73. } catch let error {
  74. XCTFail("\(error)")
  75. }
  76. waitForExpectations(timeout: 15, handler: nil)
  77. }
  78. }
  79. extension URLSession {
  80. func executeAsyncTask(
  81. hostURL: URL = defaultLocalhost,
  82. path: String,
  83. completionHandler handler: @escaping (Data?, URLResponse?, Error?) -> Void
  84. ) -> URLSessionDataTask {
  85. return self.dataTask(with: hostURL.appendingPathComponent(path), completionHandler: handler)
  86. }
  87. }