Skip to content

Commit f1ff379

Browse files
committed
EventStreams: add tests for custom terminating byte sequences
1 parent dd47f34 commit f1ff379

File tree

1 file changed

+132
-4
lines changed

1 file changed

+132
-4
lines changed

Tests/OpenAPIRuntimeTests/EventStreams/Test_ServerSentEventsDecoding.swift

Lines changed: 132 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,31 @@ import XCTest
1616
import Foundation
1717

1818
final class Test_ServerSentEventsDecoding: Test_Runtime {
19-
func _test(input: String, output: [ServerSentEvent], file: StaticString = #filePath, line: UInt = #line)
19+
func _test(input: String, output: [ServerSentEvent], file: StaticString = #filePath, line: UInt = #line, terminate: ((ArraySlice<UInt8>) -> Bool)? = nil, eventCountOffset: Int = 0)
2020
async throws
2121
{
2222
let sequence = asOneBytePerElementSequence(ArraySlice(input.utf8)).asDecodedServerSentEvents()
2323
let events = try await [ServerSentEvent](collecting: sequence)
24-
XCTAssertEqual(events.count, output.count, file: file, line: line)
24+
let eventCount = events.count + eventCountOffset
25+
XCTAssertEqual(eventCount, output.count, file: file, line: line)
2526
for (index, linePair) in zip(events, output).enumerated() {
2627
let (actualEvent, expectedEvent) = linePair
2728
XCTAssertEqual(actualEvent, expectedEvent, "Event: \(index)", file: file, line: line)
2829
}
2930
}
31+
32+
func _test(input: String, output: [ServerSentEvent], file: StaticString = #filePath, line: UInt = #line, terminatingData: ArraySlice<UInt8>, eventCountOffset: Int)
33+
async throws {
34+
try await _test(
35+
input: input,
36+
output: output,
37+
file: file,
38+
line: line,
39+
terminate: { incomingData in incomingData == terminatingData },
40+
eventCountOffset: eventCountOffset
41+
)
42+
}
43+
3044
func test() async throws {
3145
// Simple event.
3246
try await _test(
@@ -83,22 +97,80 @@ final class Test_ServerSentEventsDecoding: Test_Runtime {
8397
.init(id: "123", data: "This is a message with an ID."),
8498
]
8599
)
100+
101+
try await _test(
102+
input: #"""
103+
data: hello
104+
data: world
105+
106+
data: [DONE]
107+
108+
data: hello2
109+
data: world2
110+
111+
112+
"""#,
113+
output: [
114+
.init(data: "hello\nworld")
115+
],
116+
terminate: { incomingData in
117+
incomingData == ArraySlice<UInt8>(Data("[DONE]".utf8))
118+
},
119+
eventCountOffset: -2
120+
)
121+
122+
try await _test(
123+
input: #"""
124+
data: hello
125+
data: world
126+
127+
data: [DONE]
128+
129+
data: hello2
130+
data: world2
131+
132+
133+
"""#,
134+
output: [
135+
.init(data: "hello\nworld")
136+
],
137+
terminatingData: ArraySlice<UInt8>(Data("[DONE]".utf8)),
138+
eventCountOffset: -2
139+
)
86140
}
87141
func _testJSONData<JSONType: Decodable & Hashable & Sendable>(
88142
input: String,
89143
output: [ServerSentEventWithJSONData<JSONType>],
90144
file: StaticString = #filePath,
91-
line: UInt = #line
145+
line: UInt = #line,
146+
terminate: (@Sendable (ArraySlice<UInt8>) -> Bool)? = nil
92147
) async throws {
93148
let sequence = asOneBytePerElementSequence(ArraySlice(input.utf8))
94-
.asDecodedServerSentEventsWithJSONData(of: JSONType.self)
149+
.asDecodedServerSentEventsWithJSONData(of: JSONType.self, terminate: terminate)
95150
let events = try await [ServerSentEventWithJSONData<JSONType>](collecting: sequence)
96151
XCTAssertEqual(events.count, output.count, file: file, line: line)
97152
for (index, linePair) in zip(events, output).enumerated() {
98153
let (actualEvent, expectedEvent) = linePair
99154
XCTAssertEqual(actualEvent, expectedEvent, "Event: \(index)", file: file, line: line)
100155
}
101156
}
157+
158+
func _testJSONData<JSONType: Decodable & Hashable & Sendable>(
159+
input: String,
160+
output: [ServerSentEventWithJSONData<JSONType>],
161+
file: StaticString = #filePath,
162+
line: UInt = #line,
163+
terminatingDataSequence: ArraySlice<UInt8>?
164+
) async throws {
165+
try await _testJSONData(
166+
input: input,
167+
output: output,
168+
file: file,
169+
terminate: { incomingData in incomingData == ArraySlice<UInt8>(Data("[DONE]".utf8))
170+
}
171+
)
172+
}
173+
102174
struct TestEvent: Decodable, Hashable, Sendable { var index: Int }
103175
func testJSONData() async throws {
104176
// Simple event.
@@ -121,6 +193,62 @@ final class Test_ServerSentEventsDecoding: Test_Runtime {
121193
.init(event: "event2", data: TestEvent(index: 2), id: "2"),
122194
]
123195
)
196+
197+
try await _testJSONData(
198+
input: #"""
199+
event: event1
200+
id: 1
201+
data: {"index":1}
202+
203+
event: event2
204+
id: 2
205+
data: {
206+
data: "index": 2
207+
data: }
208+
209+
data: [DONE]
210+
211+
event: event3
212+
id: 1
213+
data: {"index":3}
214+
215+
216+
"""#,
217+
output: [
218+
.init(event: "event1", data: TestEvent(index: 1), id: "1"),
219+
.init(event: "event2", data: TestEvent(index: 2), id: "2"),
220+
],
221+
terminate: { incomingData in
222+
incomingData == ArraySlice<UInt8>(Data("[DONE]".utf8))
223+
}
224+
)
225+
226+
try await _testJSONData(
227+
input: #"""
228+
event: event1
229+
id: 1
230+
data: {"index":1}
231+
232+
event: event2
233+
id: 2
234+
data: {
235+
data: "index": 2
236+
data: }
237+
238+
data: [DONE]
239+
240+
event: event3
241+
id: 1
242+
data: {"index":3}
243+
244+
245+
"""#,
246+
output: [
247+
.init(event: "event1", data: TestEvent(index: 1), id: "1"),
248+
.init(event: "event2", data: TestEvent(index: 2), id: "2"),
249+
],
250+
terminatingDataSequence: ArraySlice<UInt8>(Data("[DONE]".utf8))
251+
)
124252
}
125253
}
126254

0 commit comments

Comments
 (0)