-
Notifications
You must be signed in to change notification settings - Fork 554
Expand file tree
/
Copy pathServerThreadingTests.swift
More file actions
118 lines (90 loc) · 3.27 KB
/
ServerThreadingTests.swift
File metadata and controls
118 lines (90 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// ServerThreadingTests.swift
// Swifter
//
// Created by Victor Sigler on 4/22/19.
// Copyright © 2019 Damian Kołakowski. All rights reserved.
//
import XCTest
#if os(Linux)
import FoundationNetworking
#endif
@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 testShouldHandleTheRequestInDifferentTimeIntervals() {
let path = "/a/:b/c"
let queue = DispatchQueue(label: "com.swifter.threading")
let hostURL: URL
server.GET[path] = { .ok(.htmlBody("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(.htmlBody("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)
}
}