Skip to content

Commit bef5a5d

Browse files
committed
Another attempt to fix tests on Linux
1 parent 8fe6405 commit bef5a5d

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

Tests/OpenAITests/ServerSentEventsStreamInterpreterTests.swift

+13-11
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,16 @@ struct ServerSentEventsStreamInterpreterTests {
8484
#expect(error is APIErrorResponse)
8585
}
8686

87-
// Chunk with 3 objects. I captured it from a real response. It's a very short response that contains just "Hi"
88-
static func chatCompletionChunk() -> Data {
89-
"data: {\"id\":\"chatcmpl-AwnboO5ZnaUyii9xxC5ZVmM5vGark\",\"object\":\"chat.completion.chunk\",\"created\":1738577084,\"model\":\"gpt-4-0613\",\"service_tier\":\"default\",\"system_fingerprint\":\"sysfig\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\",\"refusal\":null},\"logprobs\":null,\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-AwnboO5ZnaUyii9xxC5ZVmM5vGark\",\"object\":\"chat.completion.chunk\",\"created\":1738577084,\"model\":\"gpt-4-0613\",\"service_tier\":\"default\",\"system_fingerprint\":\"sysfig\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hi\"},\"logprobs\":null,\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-AwnboO5ZnaUyii9xxC5ZVmM5vGark\",\"object\":\"chat.completion.chunk\",\"created\":1738577084,\"model\":\"gpt-4-0613\",\"service_tier\":\"default\",\"system_fingerprint\":\"sysfig\",\"choices\":[{\"index\":0,\"delta\":{},\"logprobs\":null,\"finish_reason\":\"stop\"}]}\n\n".data(using: .utf8)!
90-
}
91-
9287
private func chatCompletionChunk() -> Data {
93-
type(of: self).chatCompletionChunk()
88+
MockServerSentEvent.chatCompletionChunk()
9489
}
9590

9691
private func chatCompletionChunkWithComment() -> Data {
9792
": OPENROUTER PROCESSING\n\ndata: {\"id\":\"chatcmpl-AwnboO5ZnaUyii9xxC5ZVmM5vGark\",\"object\":\"chat.completion.chunk\",\"created\":1738577084,\"model\":\"gpt-4-0613\",\"service_tier\":\"default\",\"system_fingerprint\":\"sysfig\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\",\"refusal\":null},\"logprobs\":null,\"finish_reason\":null}]}\n\n".data(using: .utf8)!
9893
}
9994

100-
static func chatCompletionChunkTermination() -> Data {
101-
"data: [DONE]\n\n".data(using: .utf8)!
102-
}
103-
10495
private func chatCompletionChunkTermination() -> Data {
105-
type(of: self).chatCompletionChunkTermination()
96+
MockServerSentEvent.chatCompletionChunkTermination()
10697
}
10798

10899
// Copied from an actual reponse that was an input to inreptreter
@@ -114,3 +105,14 @@ struct ServerSentEventsStreamInterpreterTests {
114105
private actor ChatStreamResultsActor {
115106
var chatStreamResults: [ChatStreamResult] = []
116107
}
108+
109+
struct MockServerSentEvent {
110+
// Chunk with 3 objects. I captured it from a real response. It's a very short response that contains just "Hi"
111+
static func chatCompletionChunk() -> Data {
112+
"data: {\"id\":\"chatcmpl-AwnboO5ZnaUyii9xxC5ZVmM5vGark\",\"object\":\"chat.completion.chunk\",\"created\":1738577084,\"model\":\"gpt-4-0613\",\"service_tier\":\"default\",\"system_fingerprint\":\"sysfig\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\",\"refusal\":null},\"logprobs\":null,\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-AwnboO5ZnaUyii9xxC5ZVmM5vGark\",\"object\":\"chat.completion.chunk\",\"created\":1738577084,\"model\":\"gpt-4-0613\",\"service_tier\":\"default\",\"system_fingerprint\":\"sysfig\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hi\"},\"logprobs\":null,\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-AwnboO5ZnaUyii9xxC5ZVmM5vGark\",\"object\":\"chat.completion.chunk\",\"created\":1738577084,\"model\":\"gpt-4-0613\",\"service_tier\":\"default\",\"system_fingerprint\":\"sysfig\",\"choices\":[{\"index\":0,\"delta\":{},\"logprobs\":null,\"finish_reason\":\"stop\"}]}\n\n".data(using: .utf8)!
113+
}
114+
115+
static func chatCompletionChunkTermination() -> Data {
116+
"data: [DONE]\n\n".data(using: .utf8)!
117+
}
118+
}

Tests/OpenAITests/StreamingSessionIntegrationTests.swift

+6-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import XCTest
99
@testable import OpenAI
1010

11-
@MainActor
1211
final class StreamingSessionIntegrationTests: XCTestCase {
1312
private enum Call {
1413
case content
@@ -34,47 +33,36 @@ final class StreamingSessionIntegrationTests: XCTestCase {
3433
middlewares: [],
3534
executionSerializer: executionSerializer,
3635
onReceiveContent: { _, _ in
37-
Task {
38-
await MainActor.run {
39-
self.calls.append(.content)
40-
self.expectation.fulfill()
41-
}
42-
}
36+
self.calls.append(.content)
37+
self.expectation.fulfill()
4338
},
4439
onProcessingError: { _, _ in },
4540
onComplete: { _,_ in
46-
Task {
47-
await MainActor.run {
48-
self.calls.append(.complete)
49-
self.expectation.fulfill()
50-
}
51-
}
41+
self.calls.append(.complete)
42+
self.expectation.fulfill()
5243
}
5344
)
5445

55-
@MainActor
5646
func testCompleteCalledAfterAllEventsWithMockSerializer() {
5747
XCTAssertTrue(executionSerializer is NoDispatchExecutionSerializer)
5848
let asserted = testCompleteCalledAfterAllEvents()
5949
XCTAssertTrue(asserted)
6050
}
6151

62-
@MainActor
6352
func testCompleteCalledAfterAllEventsWithRealSerializer() {
6453
executionSerializer = GCDQueueAsyncExecutionSerializer(queue: .userInitiated)
6554
let asserted = testCompleteCalledAfterAllEvents()
6655
XCTAssertTrue(asserted)
6756
}
6857

69-
@MainActor
7058
private func testCompleteCalledAfterAllEvents() -> Bool {
7159
_ = streamingSession
7260
let cancellable = streamingSession.makeSession()
7361

74-
let dataEvent = ServerSentEventsStreamInterpreterTests.chatCompletionChunk()
62+
let dataEvent = MockServerSentEvent.chatCompletionChunk()
7563
streamInterpreter.processData(dataEvent)
7664

77-
let doneEvent = ServerSentEventsStreamInterpreterTests.chatCompletionChunkTermination()
65+
let doneEvent = MockServerSentEvent.chatCompletionChunkTermination()
7866
streamInterpreter.processData(doneEvent)
7967

8068
let urlSession = urlSessionFactory.urlSession

0 commit comments

Comments
 (0)