|
|
@@ -74,8 +74,6 @@ class AsyncServerHandlerTests: ServerHandlerTestCaseBase {
|
|
|
)
|
|
|
|
|
|
handler.receiveMetadata([:])
|
|
|
- await assertThat(self.recorder.metadata, .is([:]))
|
|
|
-
|
|
|
handler.receiveMessage(ByteBuffer(string: "1"))
|
|
|
handler.receiveMessage(ByteBuffer(string: "2"))
|
|
|
handler.receiveMessage(ByteBuffer(string: "3"))
|
|
|
@@ -86,6 +84,7 @@ class AsyncServerHandlerTests: ServerHandlerTestCaseBase {
|
|
|
|
|
|
handler.finish()
|
|
|
|
|
|
+ await assertThat(self.recorder.metadata, .is([:]))
|
|
|
await assertThat(
|
|
|
self.recorder.messages,
|
|
|
.is([ByteBuffer(string: "1"), ByteBuffer(string: "2"), ByteBuffer(string: "3")])
|
|
|
@@ -145,14 +144,46 @@ class AsyncServerHandlerTests: ServerHandlerTestCaseBase {
|
|
|
await assertThat(self.recorder.messageMetadata.map { $0.compress }, .is([false, false, false]))
|
|
|
} }
|
|
|
|
|
|
+ func testResponseHeadersAndTrailersSentFromContext() { XCTAsyncTest {
|
|
|
+ let handler = self.makeHandler { _, responseStreamWriter, context in
|
|
|
+ context.initialResponseMetadata = ["pontiac": "bandit"]
|
|
|
+ try await responseStreamWriter.send("1")
|
|
|
+ context.trailingResponseMetadata = ["disco": "strangler"]
|
|
|
+ }
|
|
|
+ handler.receiveMetadata([:])
|
|
|
+ handler.receiveEnd()
|
|
|
+
|
|
|
+ // Wait for tasks to finish.
|
|
|
+ await handler.userHandlerTask?.value
|
|
|
+
|
|
|
+ await assertThat(self.recorder.metadata, .is(["pontiac": "bandit"]))
|
|
|
+ await assertThat(self.recorder.trailers, .is(["disco": "strangler"]))
|
|
|
+ } }
|
|
|
+
|
|
|
+ func testResponseHeadersDroppedIfSetAfterFirstResponse() { XCTAsyncTest {
|
|
|
+ let handler = self.makeHandler { _, responseStreamWriter, context in
|
|
|
+ try await responseStreamWriter.send("1")
|
|
|
+ context.initialResponseMetadata = ["pontiac": "bandit"]
|
|
|
+ context.trailingResponseMetadata = ["disco": "strangler"]
|
|
|
+ }
|
|
|
+ handler.receiveMetadata([:])
|
|
|
+ handler.receiveEnd()
|
|
|
+
|
|
|
+ // Wait for tasks to finish.
|
|
|
+ await handler.userHandlerTask?.value
|
|
|
+
|
|
|
+ await assertThat(self.recorder.metadata, .is([:]))
|
|
|
+ await assertThat(self.recorder.trailers, .is(["disco": "strangler"]))
|
|
|
+ } }
|
|
|
+
|
|
|
func testTaskOnlyCreatedAfterHeaders() { XCTAsyncTest {
|
|
|
let handler = self.makeHandler(observer: self.echo(requests:responseStreamWriter:context:))
|
|
|
|
|
|
- await assertThat(handler.userHandlerTask, .is(.nil()))
|
|
|
+ await assertThat(handler.userHandlerTask, .nil())
|
|
|
|
|
|
handler.receiveMetadata([:])
|
|
|
|
|
|
- await assertThat(handler.userHandlerTask, .is(.notNil()))
|
|
|
+ await assertThat(handler.userHandlerTask, .notNil())
|
|
|
} }
|
|
|
|
|
|
func testThrowingDeserializer() { XCTAsyncTest {
|
|
|
@@ -165,18 +196,12 @@ class AsyncServerHandlerTests: ServerHandlerTestCaseBase {
|
|
|
)
|
|
|
|
|
|
handler.receiveMetadata([:])
|
|
|
-
|
|
|
- // Wait for the async user function to have processed the metadata.
|
|
|
- try self.recorder.recordedMetadataPromise.futureResult.wait()
|
|
|
-
|
|
|
- await assertThat(self.recorder.metadata, .is([:]))
|
|
|
-
|
|
|
- let buffer = ByteBuffer(string: "hello")
|
|
|
- handler.receiveMessage(buffer)
|
|
|
+ handler.receiveMessage(ByteBuffer(string: "hello"))
|
|
|
|
|
|
// Wait for tasks to finish.
|
|
|
await handler.userHandlerTask?.value
|
|
|
|
|
|
+ await assertThat(self.recorder.metadata, .nil())
|
|
|
await assertThat(self.recorder.messages, .isEmpty())
|
|
|
await assertThat(self.recorder.status, .notNil(.hasCode(.internalError)))
|
|
|
} }
|
|
|
@@ -191,15 +216,13 @@ class AsyncServerHandlerTests: ServerHandlerTestCaseBase {
|
|
|
)
|
|
|
|
|
|
handler.receiveMetadata([:])
|
|
|
- await assertThat(self.recorder.metadata, .is([:]))
|
|
|
-
|
|
|
- let buffer = ByteBuffer(string: "hello")
|
|
|
- handler.receiveMessage(buffer)
|
|
|
+ handler.receiveMessage(ByteBuffer(string: "hello"))
|
|
|
handler.receiveEnd()
|
|
|
|
|
|
// Wait for tasks to finish.
|
|
|
await handler.userHandlerTask?.value
|
|
|
|
|
|
+ await assertThat(self.recorder.metadata, .is([:]))
|
|
|
await assertThat(self.recorder.messages, .isEmpty())
|
|
|
await assertThat(self.recorder.status, .notNil(.hasCode(.internalError)))
|
|
|
} }
|
|
|
@@ -213,28 +236,22 @@ class AsyncServerHandlerTests: ServerHandlerTestCaseBase {
|
|
|
// Wait for tasks to finish.
|
|
|
await handler.userHandlerTask?.value
|
|
|
|
|
|
- await assertThat(self.recorder.metadata, .is(.nil()))
|
|
|
+ await assertThat(self.recorder.metadata, .nil())
|
|
|
await assertThat(self.recorder.messages, .isEmpty())
|
|
|
await assertThat(self.recorder.status, .notNil(.hasCode(.internalError)))
|
|
|
} }
|
|
|
|
|
|
- // TODO: Running this 1000 times shows up a segfault in NIO event loop group.
|
|
|
func testReceiveMultipleHeaders() { XCTAsyncTest {
|
|
|
let handler = self
|
|
|
.makeHandler(observer: self.neverReceivesMessage(requests:responseStreamWriter:context:))
|
|
|
|
|
|
handler.receiveMetadata([:])
|
|
|
-
|
|
|
- // Wait for the async user function to have processed the metadata.
|
|
|
- try self.recorder.recordedMetadataPromise.futureResult.wait()
|
|
|
-
|
|
|
- await assertThat(self.recorder.metadata, .is([:]))
|
|
|
-
|
|
|
handler.receiveMetadata([:])
|
|
|
|
|
|
// Wait for tasks to finish.
|
|
|
await handler.userHandlerTask?.value
|
|
|
|
|
|
+ await assertThat(self.recorder.metadata, .nil())
|
|
|
await assertThat(self.recorder.messages, .isEmpty())
|
|
|
await assertThat(self.recorder.status, .notNil(.hasCode(.internalError)))
|
|
|
} }
|
|
|
@@ -244,26 +261,22 @@ class AsyncServerHandlerTests: ServerHandlerTestCaseBase {
|
|
|
.makeHandler(observer: self.neverCalled(requests:responseStreamWriter:context:))
|
|
|
|
|
|
handler.finish()
|
|
|
- await assertThat(self.recorder.metadata, .is(.nil()))
|
|
|
+ await assertThat(self.recorder.metadata, .nil())
|
|
|
await assertThat(self.recorder.messages, .isEmpty())
|
|
|
- await assertThat(self.recorder.status, .is(.nil()))
|
|
|
- await assertThat(self.recorder.trailers, .is(.nil()))
|
|
|
+ await assertThat(self.recorder.status, .nil())
|
|
|
+ await assertThat(self.recorder.trailers, .nil())
|
|
|
} }
|
|
|
|
|
|
func testFinishAfterHeaders() { XCTAsyncTest {
|
|
|
let handler = self.makeHandler(observer: self.echo(requests:responseStreamWriter:context:))
|
|
|
- handler.receiveMetadata([:])
|
|
|
-
|
|
|
- // Wait for the async user function to have processed the metadata.
|
|
|
- try self.recorder.recordedMetadataPromise.futureResult.wait()
|
|
|
-
|
|
|
- await assertThat(self.recorder.metadata, .is([:]))
|
|
|
|
|
|
+ handler.receiveMetadata([:])
|
|
|
handler.finish()
|
|
|
|
|
|
// Wait for tasks to finish.
|
|
|
await handler.userHandlerTask?.value
|
|
|
|
|
|
+ await assertThat(self.recorder.metadata, .nil())
|
|
|
await assertThat(self.recorder.messages, .isEmpty())
|
|
|
await assertThat(self.recorder.status, .notNil(.hasCode(.unavailable)))
|
|
|
await assertThat(self.recorder.trailers, .is([:]))
|
|
|
@@ -304,8 +317,6 @@ class AsyncServerHandlerTests: ServerHandlerTestCaseBase {
|
|
|
await assertThat(self.recorder.status, .notNil(.hasCode(.unknown)))
|
|
|
} }
|
|
|
|
|
|
- // TODO: We should be consistent about where we put the tasks... maybe even use a task group to simplify cancellation (unless they both go in the enum state which might be better).
|
|
|
-
|
|
|
func testResponseStreamDrain() { XCTAsyncTest {
|
|
|
// Set up echo handler.
|
|
|
let handler = self.makeHandler(
|
|
|
@@ -317,14 +328,14 @@ class AsyncServerHandlerTests: ServerHandlerTestCaseBase {
|
|
|
|
|
|
// Send two requests and end, pausing the writer in the middle.
|
|
|
switch handler.state {
|
|
|
- case let .active(_, _, responseStreamWriter, promise):
|
|
|
+ case let .active(activeState):
|
|
|
handler.receiveMessage(ByteBuffer(string: "diaz"))
|
|
|
- await responseStreamWriter.asyncWriter.toggleWritability()
|
|
|
+ await activeState.responseStreamWriter.asyncWriter.toggleWritability()
|
|
|
handler.receiveMessage(ByteBuffer(string: "santiago"))
|
|
|
handler.receiveEnd()
|
|
|
- await responseStreamWriter.asyncWriter.toggleWritability()
|
|
|
+ await activeState.responseStreamWriter.asyncWriter.toggleWritability()
|
|
|
await handler.userHandlerTask?.value
|
|
|
- _ = try await promise.futureResult.get()
|
|
|
+ _ = try await activeState._userHandlerPromise.futureResult.get()
|
|
|
default:
|
|
|
XCTFail("Unexpected handler state: \(handler.state)")
|
|
|
}
|