Skip to content

Commit

Permalink
Swift Testing support (#96)
Browse files Browse the repository at this point in the history
* Swift Testing support

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* restore main actor

* wip
  • Loading branch information
stephencelis authored Jul 22, 2024
1 parent 416d722 commit 487a4d1
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ jobs:
run: sudo xcode-select -s /Applications/Xcode_15.4.app
- name: Run tests
run: make test
- name: Build for library evolution
run: make build-for-library-evolution
# - name: Build for library evolution
# run: make build-for-library-evolution

windows:
name: Windows
Expand Down
42 changes: 20 additions & 22 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
{
"object": {
"pins": [
{
"package": "swift-concurrency-extras",
"repositoryURL": "https://github.com/pointfreeco/swift-concurrency-extras",
"state": {
"branch": null,
"revision": "ea631ce892687f5432a833312292b80db238186a",
"version": "1.0.0"
}
},
{
"package": "xctest-dynamic-overlay",
"repositoryURL": "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state": {
"branch": null,
"revision": "23cbf2294e350076ea4dbd7d5d047c1e76b03631",
"version": "1.0.2"
}
"pins" : [
{
"identity" : "swift-concurrency-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-concurrency-extras",
"state" : {
"revision" : "bb5059bde9022d69ac516803f4f227d8ac967f71",
"version" : "1.1.0"
}
]
},
"version": 1
},
{
"identity" : "swift-issue-reporting",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-issue-reporting",
"state" : {
"revision" : "926f43898706eaa127db79ac42138e1ad7e85a3f",
"version" : "1.2.0"
}
}
],
"version" : 2
}
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.0.2"),
.package(url: "https://github.com/pointfreeco/swift-issue-reporting", from: "1.2.0"),
],
targets: [
.target(
name: "CombineSchedulers",
dependencies: [
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"),
.product(name: "IssueReporting", package: "swift-issue-reporting"),
]
),
.testTarget(
Expand Down
4 changes: 2 additions & 2 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-issue-reporting", from: "1.2.0"),
],
targets: [
.target(
name: "CombineSchedulers",
dependencies: [
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"),
.product(name: "IssueReporting", package: "swift-issue-reporting"),
]
),
.testTarget(
Expand Down
6 changes: 4 additions & 2 deletions Sources/CombineSchedulers/Concurrency.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if canImport(Combine)
@preconcurrency import Combine
import ConcurrencyExtras

extension Scheduler {
/// Suspends the current task for at least the given duration.
Expand Down Expand Up @@ -76,14 +77,15 @@
tolerance: SchedulerTimeType.Stride = .zero,
options: SchedulerOptions? = nil
) -> AsyncStream<SchedulerTimeType> {
AsyncStream { continuation in
@UncheckedSendable var scheduler = self
return AsyncStream { [$scheduler] continuation in
let cancellable = self.schedule(
after: self.now.advanced(by: interval),
interval: interval,
tolerance: tolerance,
options: options
) {
continuation.yield(self.now)
continuation.yield($scheduler.wrappedValue.now)
}
continuation.onTermination =
{ _ in
Expand Down
3 changes: 3 additions & 0 deletions Sources/CombineSchedulers/TestScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
/// - Parameter duration: A stride. By default this argument is `.zero`, which does not advance
/// the scheduler's time but does cause the scheduler to execute any units of work that are
/// waiting to be performed for right now.
@MainActor
public func advance(by duration: SchedulerTimeType.Stride = .zero) async {
await self.advance(to: self.now.advanced(by: duration))
}
Expand Down Expand Up @@ -129,6 +130,7 @@
/// Advances the scheduler to the given instant.
///
/// - Parameter instant: An instant in time to advance to.
@MainActor
public func advance(to instant: SchedulerTimeType) async {
while self.lock.sync(operation: { self.now }) <= instant {
await Task.megaYield()
Expand Down Expand Up @@ -194,6 +196,7 @@
}
}

@MainActor
public func run() async {
await Task.megaYield()
while let date = self.lock.sync(operation: { self.scheduled.first?.date }) {
Expand Down
12 changes: 6 additions & 6 deletions Sources/CombineSchedulers/UnimplementedScheduler.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if canImport(Combine)
import Combine
import Foundation
import XCTestDynamicOverlay
import IssueReporting

/// A scheduler that causes the current XCTest test case to fail if it is used.
///
Expand Down Expand Up @@ -78,7 +78,7 @@
SchedulerTimeType.Stride: SchedulerTimeIntervalConvertible
{
public var minimumTolerance: SchedulerTimeType.Stride {
XCTFail(
reportIssue(
"""
\(self.prefix.isEmpty ? "" : "\(self.prefix) - ")\
An unimplemented scheduler was asked its minimum tolerance.
Expand All @@ -88,7 +88,7 @@
}

public var now: SchedulerTimeType {
XCTFail(
reportIssue(
"""
\(self.prefix.isEmpty ? "" : "\(self.prefix) - ")\
An unimplemented scheduler was asked the current time.
Expand All @@ -112,7 +112,7 @@
}

public func schedule(options _: SchedulerOptions?, _ action: () -> Void) {
XCTFail(
reportIssue(
"""
\(self.prefix.isEmpty ? "" : "\(self.prefix) - ")\
An unimplemented scheduler scheduled an action to run immediately.
Expand All @@ -127,7 +127,7 @@
options _: SchedulerOptions?,
_ action: () -> Void
) {
XCTFail(
reportIssue(
"""
\(self.prefix.isEmpty ? "" : "\(self.prefix) - ")\
An unimplemented scheduler scheduled an action to run later.
Expand All @@ -143,7 +143,7 @@
options _: SchedulerOptions?,
_ action: () -> Void
) -> Cancellable {
XCTFail(
reportIssue(
"""
\(self.prefix.isEmpty ? "" : "\(self.prefix) - ")\
An unimplemented scheduler scheduled an action to run on a timer.
Expand Down

0 comments on commit 487a4d1

Please sign in to comment.