Skip to content

tests: instead of pre-populating, make use of testOnly_exact #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ extension HTTPClientInternalTests {
("testRaceBetweenAsynchronousCloseAndChannelUsabilityDetection", testRaceBetweenAsynchronousCloseAndChannelUsabilityDetection),
("testResponseFutureIsOnCorrectEL", testResponseFutureIsOnCorrectEL),
("testUncleanCloseThrows", testUncleanCloseThrows),
("testUploadStreamingIsCalledOnTaskEL", testUploadStreamingIsCalledOnTaskEL),
("testWeCanActuallyExactlySetTheEventLoops", testWeCanActuallyExactlySetTheEventLoops),
]
}
}
77 changes: 77 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -806,4 +806,81 @@ class HTTPClientInternalTests: XCTestCase {
}
}
}

func testUploadStreamingIsCalledOnTaskEL() throws {
let group = getDefaultEventLoopGroup(numberOfThreads: 4)
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}

let httpBin = HTTPBin()
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(group))
defer {
XCTAssertNoThrow(try httpClient.syncShutdown())
XCTAssertNoThrow(try httpBin.shutdown())
}

let el1 = group.next()
let el2 = group.next()
XCTAssert(el1 !== el2)

let body: HTTPClient.Body = .stream(length: 8) { writer in
XCTAssert(el1.inEventLoop)
let buffer = ByteBuffer.of(string: "1234")
return writer.write(.byteBuffer(buffer)).flatMap {
XCTAssert(el1.inEventLoop)
let buffer = ByteBuffer.of(string: "4321")
return writer.write(.byteBuffer(buffer))
}
}
let request = try HTTPClient.Request(url: "http://localhost:\(httpBin.port)/post", method: .POST, body: body)
let response = httpClient.execute(request: request,
delegate: ResponseAccumulator(request: request),
eventLoop: HTTPClient.EventLoopPreference(.testOnly_exact(channelOn: el2,
delegateOn: el1)))
XCTAssert(el1 === response.eventLoop)
XCTAssertNoThrow(try response.wait())
}

func testWeCanActuallyExactlySetTheEventLoops() throws {
let group = getDefaultEventLoopGroup(numberOfThreads: 3)
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}

let httpBin = HTTPBin()
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(group))
defer {
XCTAssertNoThrow(try httpClient.syncShutdown())
XCTAssertNoThrow(try httpBin.shutdown())
}

let el1 = group.next()
let el2 = group.next()
XCTAssert(el1 !== el2)

let taskPromise = group.next().makePromise(of: HTTPClient.Task<HTTPClient.Response>.self)
let body: HTTPClient.Body = .stream(length: 8) { writer in
XCTAssert(el1.inEventLoop)
let buffer = ByteBuffer.of(string: "1234")
return writer.write(.byteBuffer(buffer)).flatMap {
XCTAssert(el1.inEventLoop)
let buffer = ByteBuffer.of(string: "4321")
return taskPromise.futureResult.map { (task: HTTPClient.Task<HTTPClient.Response>) -> Void in
XCTAssertNotNil(task.connection)
XCTAssert(task.connection?.channel.eventLoop === el2)
}.flatMap {
writer.write(.byteBuffer(buffer))
}
}
}
let request = try HTTPClient.Request(url: "http://localhost:\(httpBin.port)/post", method: .POST, body: body)
let response = httpClient.execute(request: request,
delegate: ResponseAccumulator(request: request),
eventLoop: HTTPClient.EventLoopPreference(.testOnly_exact(channelOn: el2,
delegateOn: el1)))
taskPromise.succeed(response)
XCTAssert(el1 === response.eventLoop)
XCTAssertNoThrow(try response.wait())
}
}
1 change: 0 additions & 1 deletion Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ extension HTTPClientTests {
("testValidationErrorsAreSurfaced", testValidationErrorsAreSurfaced),
("testUploadsReallyStream", testUploadsReallyStream),
("testUploadStreamingCallinToleratedFromOtsideEL", testUploadStreamingCallinToleratedFromOtsideEL),
("testUploadStreamingIsCalledOnTaskEL", testUploadStreamingIsCalledOnTaskEL),
]
}
}
37 changes: 0 additions & 37 deletions Tests/AsyncHTTPClientTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1836,41 +1836,4 @@ class HTTPClientTests: XCTestCase {
})
XCTAssertNoThrow(try httpClient.execute(request: request).wait())
}

func testUploadStreamingIsCalledOnTaskEL() throws {
let group = getDefaultEventLoopGroup(numberOfThreads: 4)
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}

let httpBin = HTTPBin()
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(group))
defer {
XCTAssertNoThrow(try httpClient.syncShutdown())
XCTAssertNoThrow(try httpBin.shutdown())
}

let el1 = group.next()
let el2 = group.next()
XCTAssertFalse(el1 === el2)

do {
// Pre-populate pool with a connection on a different EL
let request = try HTTPClient.Request(url: "http://localhost:\(httpBin.port)/get", method: .GET)
XCTAssertNoThrow(try httpClient.execute(request: request, delegate: ResponseAccumulator(request: request), eventLoop: .delegateAndChannel(on: el2)).wait())
}

let body: HTTPClient.Body = .stream(length: 8) { writer in
XCTAssert(el1.inEventLoop)
let buffer = ByteBuffer.of(string: "1234")
return writer.write(.byteBuffer(buffer)).flatMap {
XCTAssert(el1.inEventLoop)
let buffer = ByteBuffer.of(string: "4321")
return writer.write(.byteBuffer(buffer))
}
}
let request = try HTTPClient.Request(url: "http://localhost:\(httpBin.port)/post", method: .POST, body: body)
let response = httpClient.execute(request: request, delegate: ResponseAccumulator(request: request), eventLoop: .delegate(on: el1))
XCTAssertNoThrow(try response.wait())
}
}