ServerThreadingTests.swift 3.3 KB

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