Skip to content

Commit

Permalink
feat: Update event source to use AnyURLSession (#1)
Browse files Browse the repository at this point in the history
* Update event source to use AnyURLSession

* Fix targeting for CI runs

* Fix swift versions

* Point to different anyurlsession commit

* Adjust test for session creation swapping

* Update comment in ci
  • Loading branch information
brianmichel authored Jan 18, 2024
1 parent b5c1e27 commit 171e089
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
19 changes: 12 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: Run CI
on:
push:
branches: [ main ]
branches: [ main-bcny ]
paths-ignore:
- '**.md' # Do not need to run CI for markdown changes.
pull_request:
branches: [ main ]
branches: [ main-bcny ]
paths-ignore:
- '**.md'

Expand Down Expand Up @@ -44,11 +44,16 @@ jobs:
strategy:
matrix:
swift-version:
- 5.1
- 5.2
- 5.3
- 5.4
- 5.5
# This package originally goes back to Swift 5.0
# however, we're unable to specify a dependency at a specific SHA
# until Swift version 5.5+ so while these reflect what used to be
# supported by the base package, this fork requires a different
# minimum toolchain and package description.
# - 5.1
# - 5.2
# - 5.3
# - 5.4
# - 5.5
- 5.6
- 5.7
- 5.8
Expand Down
16 changes: 16 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "swift-anyurlsession",
"repositoryURL": "https://github.com/thebrowsercompany/AnyURLSession",
"state": {
"branch": "9b8e39b",
"revision": "9b8e39b95f8290909b976619eb00dc8c9ba472bf",
"version": null
}
}
]
},
"version": 1
}
9 changes: 7 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.0
// swift-tools-version:5.5

import PackageDescription

Expand All @@ -13,10 +13,15 @@ let package = Package(
products: [
.library(name: "LDSwiftEventSource", targets: ["LDSwiftEventSource"]),
],
dependencies: [],
dependencies: [
.package(url: "https://github.com/thebrowsercompany/AnyURLSession", revision: "9b8e39b")
],
targets: [
.target(
name: "LDSwiftEventSource",
dependencies: [
.product(name: "AnyURLSession", package: "AnyURLSession")
],
path: "Source"),
.testTarget(
name: "LDSwiftEventSourceTests",
Expand Down
7 changes: 6 additions & 1 deletion Source/LDSwiftEventSource.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import Foundation

#if os(Linux) || os(Windows)
import FoundationNetworking
import AnyURLSession

import class FoundationNetworking.HTTPURLResponse
import struct FoundationNetworking.URLRequest
import class FoundationNetworking.URLResponse
import class FoundationNetworking.URLSessionConfiguration
#endif

/**
Expand Down
23 changes: 23 additions & 0 deletions Tests/LDSwiftEventSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import XCTest

#if os(Linux) || os(Windows)
import FoundationNetworking
import AnyURLSession
#endif

final class LDSwiftEventSourceTests: XCTestCase {
Expand Down Expand Up @@ -103,6 +104,28 @@ final class LDSwiftEventSourceTests: XCTestCase {
}

func testCreatedSession() {
#if os(Linux) || os(Windows)
final class TestURLSessionGuts: URLSessionGuts {
var configuration: URLSessionConfiguration

init(configuration: URLSessionConfiguration, delegate: (any AnyURLSession.URLSessionDelegate)?, delegateQueue queue: OperationQueue?) {
self.configuration = configuration
}

func uploadTask(with request: URLRequest, fromFile file: URL, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> AnyURLSession.URLSessionUploadTask { AnyURLSession.URLSessionUploadTask() }
func dataTask(with request: URLRequest) -> AnyURLSession.URLSessionDataTask { AnyURLSession.URLSessionDataTask() }
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> AnyURLSession.URLSessionDataTask { AnyURLSession.URLSessionDataTask() }
func invalidateAndCancel() {}
func finishTasksAndInvalidate() {}
}

AnyURLSession.Dependencies.current.setValue(
Dependencies(gutsFactory: { (config, delegate, queue) in
TestURLSessionGuts(configuration: config, delegate: delegate, delegateQueue: queue)
})
)

#endif
let config = EventSource.Config(handler: mockHandler, url: URL(string: "abc")!)
let session = EventSourceDelegate(config: config).createSession()
XCTAssertEqual(session.configuration.timeoutIntervalForRequest, config.idleTimeout)
Expand Down

0 comments on commit 171e089

Please sign in to comment.