Skip to content
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
3 changes: 0 additions & 3 deletions EventSource.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
},
"testTargets" : [
{
"skippedTests" : [
"EventSourceTests\/dispatchError()"
],
"target" : {
"containerPath" : "container:",
"identifier" : "EventSourceTests",
Expand Down
1 change: 0 additions & 1 deletion Sources/EventSource/EventParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ actor EventParser {
private func dispatchEvent() async {
lastEventId = lastEventIdBuffer ?? lastEventId
UserDefaults.eventSource.set(lastEventId, forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
lastEventIdBuffer = nil
guard !data.isEmpty else {
eventType = ""
Expand Down
2 changes: 0 additions & 2 deletions Sources/EventSource/EventSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ public actor EventSource: NSObject {
*/
public var urlSessionConfiguration: URLSessionConfiguration {
get {
// swiftlint:disable:next force_cast
let sessionConfig = _urlSessionConfiguration.copy() as? URLSessionConfiguration
sessionConfig?.httpAdditionalHeaders = ["Accept": "text/event-stream", "Cache-Control": "no-cache"]
sessionConfig?.timeoutIntervalForRequest = idleTimeout
Expand All @@ -237,7 +236,6 @@ public actor EventSource: NSObject {
return sessionConfig ?? .default
}
set {
// swiftlint:disable:next force_cast
_urlSessionConfiguration = newValue.copy() as? URLSessionConfiguration ?? .default
}
}
Expand Down
1 change: 0 additions & 1 deletion Sources/EventSource/EventSourceDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ final class EventSourceDelegate: NSObject, URLSessionDataDelegate {

func handleInitialReply(response: URLResponse) async -> URLSession.ResponseDisposition {
Self.logger.debug("Initial reply received")
// swiftlint:disable:next force_cast
let httpResponse = response as? HTTPURLResponse
let statusCode = httpResponse?.statusCode ?? 500
if (200..<300).contains(statusCode) && statusCode != 204 {
Expand Down
72 changes: 28 additions & 44 deletions Tests/EventSourceTests/EventSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ struct EventSourceTests {
#expect(config.headerTransform(["abc": "123"]) == ["abc": "123"])
await #expect(config.connectionErrorHandler(TestError()) == .proceed)
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand Down Expand Up @@ -107,7 +106,6 @@ struct EventSourceTests {
#expect(config.idleTimeout == 180.0)
await #expect(config.connectionErrorHandler(TestError()) == .shutdown)
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand All @@ -131,7 +129,6 @@ struct EventSourceTests {
#expect(config.urlSessionConfiguration.allowsCellularAccess == false)
#expect(sessionConfig !== config.urlSessionConfiguration)
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand All @@ -144,7 +141,6 @@ struct EventSourceTests {
es = EventSource(config: config, sessionType: MockDataTaskSession.self)
await #expect(es.getLastEventId() == "def")
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand All @@ -156,7 +152,6 @@ struct EventSourceTests {
#expect(configuration.httpAdditionalHeaders?["Accept"] as? String == "text/event-stream")
#expect(configuration.httpAdditionalHeaders?["Cache-Control"] as? String == "no-cache")
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand Down Expand Up @@ -193,35 +188,36 @@ struct EventSourceTests {
#expect(request.timeoutInterval == config.idleTimeout)
#expect(request.allHTTPHeaderFields == overrideHeaders.merging(staticHeaders) { $1 })
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

// @Test
// func dispatchError() async {
// let mockHandler = MockHandler()
//
// var config = EventSource<MockDataTaskSession>.Config(handler: mockHandler, url: URL(string: "abc")!)
//
//// var connectionErrorHandlerCallCount = 0
// config.connectionErrorHandler = { error in
//// connectionErrorHandlerCallCount += 1
// return .proceed
// }
//
// let es = EventSource(config: config, urlSession: MockDataTaskSession.self)
// await #expect(es.dispatchError(error: TestError()) == .proceed)
//// #expect(connectionErrorHandlerCallCount == 1)
// guard
// case .error(let err) = await mockHandler.expectEvent(),
// err is TestError
// else {
// Issue.record("handler should receive error if EventSource is not shutting down")
// return
// }
// await mockHandler.events.expectNoEvent()
// await #expect(es.dispatchError(error: TestError()) == .shutdown)
//// #expect(connectionErrorHandlerCallCount == 2)
// }
@Test
func dispatchError() async {
let mockHandler = MockHandler()

var config = EventSource.Config(handler: mockHandler, url: URL(string: "abc")!)

let connectionErrorHandlerCallCount = LockIsolated(0)
config.connectionErrorHandler = { error in
connectionErrorHandlerCallCount.withValue { $0 += 1 }
// Return shutdown on second call to test both paths
return connectionErrorHandlerCallCount.value == 1 ? .proceed : .shutdown
}

let es = EventSource(config: config, sessionType: MockDataTaskSession.self)
await #expect(es.dispatchError(error: TestError()) == .proceed)
#expect(connectionErrorHandlerCallCount.value == 1)
guard
case .error(let err) = await mockHandler.events.first,
err is TestError
else {
Issue.record("handler should receive error if EventSource is not shutting down")
return
}
await #expect(es.dispatchError(error: TestError()) == .shutdown)
#expect(connectionErrorHandlerCallCount.value == 2)
// Error should not be dispatched to handler when shutdown is returned
await #expect(mockHandler.events.count == 1)
}

#if !os(Linux) && !os(Windows)
@Test
Expand All @@ -242,7 +238,6 @@ struct EventSourceTests {
#expect(session?.lastRequest?.allHTTPHeaderFields?["Last-Event-Id"] == nil)
await es.stop()
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand All @@ -268,7 +263,6 @@ struct EventSourceTests {
#expect(session?.lastRequest?.allHTTPHeaderFields?["X-LD-Header"] == "def")
await es.stop()
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand All @@ -287,7 +281,6 @@ struct EventSourceTests {
#expect(session?.requests.count == 1)
await es.stop()
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand All @@ -303,7 +296,6 @@ struct EventSourceTests {
await #expect(mockHandler.events.first == .opened)
await eventSource.stop()
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand Down Expand Up @@ -343,7 +335,6 @@ struct EventSourceTests {
await #expect((eventSource.urlSession as? MockDataTaskSession)?.requests.last?.allHTTPHeaderFields?["Last-Event-Id"] == "abc")
await eventSource.stop()
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand Down Expand Up @@ -374,7 +365,6 @@ struct EventSourceTests {
await #expect(mockHandler.events[safe: 2] == .opened)
await eventSource.stop()
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand All @@ -397,7 +387,6 @@ struct EventSourceTests {
await eventSource.stop()
await #expect(mockHandler.events[safe: 2] == .closed)
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand All @@ -419,7 +408,6 @@ struct EventSourceTests {
await eventSource.stop()
}
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand All @@ -440,7 +428,6 @@ struct EventSourceTests {
// Error should not have been given to the handler
await #expect(mockHandler.events.isEmpty)
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand All @@ -463,7 +450,6 @@ struct EventSourceTests {
// Error should not have been given to the handler
await #expect(mockHandler.events.count == 2)
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand All @@ -479,7 +465,6 @@ struct EventSourceTests {
// Error should not have been given to the handler
await #expect(mockHandler.events.isEmpty)
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}

@Test
Expand All @@ -500,7 +485,6 @@ struct EventSourceTests {
// Error should not have been given to the handler
await #expect(mockHandler.events.isEmpty)
UserDefaults.eventSource.removeObject(forKey: "com.briannadoubt.event-source.last-event-id")
UserDefaults.eventSource.synchronize()
}
#endif
}