forked from httpswift/swifter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request httpswift#399 from Vkt0r/threading-issue
Fix a crash when several request for the same URL or route are launched together
- Loading branch information
Showing
11 changed files
with
248 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// | ||
// ServerThreadingTests.swift | ||
// Swifter | ||
// | ||
// Created by Victor Sigler on 4/22/19. | ||
// Copyright © 2019 Damian Kołakowski. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import Swifter | ||
|
||
class ServerThreadingTests: XCTestCase { | ||
|
||
var server: HttpServer! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
server = HttpServer() | ||
} | ||
|
||
override func tearDown() { | ||
if server.operating { | ||
server.stop() | ||
} | ||
server = nil | ||
super.tearDown() | ||
} | ||
|
||
func testShouldHandleTheSameRequestWithDifferentTimeIntervals() { | ||
|
||
let path = "/a/:b/c" | ||
let queue = DispatchQueue(label: "com.swifter.threading") | ||
let hostURL: URL | ||
|
||
server.GET[path] = { .ok(.html("You asked for " + $0.path)) } | ||
|
||
do { | ||
|
||
#if os(Linux) | ||
try server.start(9081) | ||
hostURL = URL(string: "http://localhost:9081")! | ||
#else | ||
try server.start() | ||
hostURL = defaultLocalhost | ||
#endif | ||
|
||
let requestExpectation = expectation(description: "Request should finish.") | ||
requestExpectation.expectedFulfillmentCount = 3 | ||
|
||
(1...3).forEach { index in | ||
queue.asyncAfter(deadline: .now() + .seconds(index)) { | ||
let task = URLSession.shared.executeAsyncTask(hostURL: hostURL, path: path) { (_, response, _ ) in | ||
requestExpectation.fulfill() | ||
let statusCode = (response as? HTTPURLResponse)?.statusCode | ||
XCTAssertNotNil(statusCode) | ||
XCTAssertEqual(statusCode, 200, "\(hostURL)") | ||
} | ||
|
||
task.resume() | ||
} | ||
} | ||
|
||
} catch let error { | ||
XCTFail("\(error)") | ||
} | ||
|
||
waitForExpectations(timeout: 10, handler: nil) | ||
} | ||
|
||
func testShouldHandleTheSameRequestConcurrently() { | ||
|
||
let path = "/a/:b/c" | ||
server.GET[path] = { .ok(.html("You asked for " + $0.path)) } | ||
|
||
var requestExpectation: XCTestExpectation? = expectation(description: "Should handle the request concurrently") | ||
|
||
do { | ||
|
||
try server.start() | ||
let downloadGroup = DispatchGroup() | ||
|
||
DispatchQueue.concurrentPerform(iterations: 3) { _ in | ||
downloadGroup.enter() | ||
|
||
let task = URLSession.shared.executeAsyncTask(path: path) { (_, response, _ ) in | ||
|
||
let statusCode = (response as? HTTPURLResponse)?.statusCode | ||
XCTAssertNotNil(statusCode) | ||
XCTAssertEqual(statusCode, 200) | ||
requestExpectation?.fulfill() | ||
requestExpectation = nil | ||
downloadGroup.leave() | ||
} | ||
|
||
task.resume() | ||
} | ||
|
||
} catch let error { | ||
XCTFail("\(error)") | ||
} | ||
|
||
waitForExpectations(timeout: 15, handler: nil) | ||
} | ||
} | ||
|
||
extension URLSession { | ||
|
||
func executeAsyncTask( | ||
hostURL: URL = defaultLocalhost, | ||
path: String, | ||
completionHandler handler: @escaping (Data?, URLResponse?, Error?) -> Void | ||
) -> URLSessionDataTask { | ||
return self.dataTask(with: hostURL.appendingPathComponent(path), completionHandler: handler) | ||
} | ||
} |
Oops, something went wrong.