Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Point Issue Reporting to xctest-dynamic-overlay repo #97

Merged
merged 2 commits into from
Jul 24, 2024
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
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
}
},
{
"identity" : "swift-issue-reporting",
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-issue-reporting",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "926f43898706eaa127db79ac42138e1ad7e85a3f",
"version" : "1.2.0"
"revision" : "357ca1e5dd31f613a1d43320870ebc219386a495",
"version" : "1.2.2"
}
}
],
Expand Down
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/swift-issue-reporting", from: "1.2.0"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.2.2"),
],
targets: [
.target(
name: "CombineSchedulers",
dependencies: [
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "IssueReporting", package: "swift-issue-reporting"),
.product(name: "IssueReporting", package: "xctest-dynamic-overlay"),
]
),
.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/swift-issue-reporting", from: "1.2.0"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.2.2"),
],
targets: [
.target(
name: "CombineSchedulers",
dependencies: [
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "IssueReporting", package: "swift-issue-reporting"),
.product(name: "IssueReporting", package: "xctest-dynamic-overlay"),
]
),
.testTarget(
Expand Down
147 changes: 87 additions & 60 deletions Tests/CombineSchedulersTests/TestSchedulerTests.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#if canImport(Combine)
import Combine
import CombineSchedulers
import ConcurrencyExtras
import XCTest

final class CombineSchedulerTests: XCTestCase {
var cancellables: Set<AnyCancellable> = []

func testAdvance() {
var cancellables: Set<AnyCancellable> = []

let scheduler = DispatchQueue.test

var value: Int?
Just(1)
.delay(for: 1, scheduler: scheduler)
.sink { value = $0 }
.store(in: &self.cancellables)
.store(in: &cancellables)

XCTAssertEqual(value, nil)

Expand All @@ -35,14 +36,16 @@
}

func testAdvanceTo() {
var cancellables: Set<AnyCancellable> = []

let scheduler = DispatchQueue.test
let start = scheduler.now

var value: Int?
Just(1)
.delay(for: 1, scheduler: scheduler)
.sink { value = $0 }
.store(in: &self.cancellables)
.store(in: &cancellables)

XCTAssertEqual(value, nil)

Expand All @@ -64,13 +67,15 @@
}

func testRunScheduler() {
var cancellables: Set<AnyCancellable> = []

let scheduler = DispatchQueue.test

var value: Int?
Just(1)
.delay(for: 1_000_000_000, scheduler: scheduler)
.sink { value = $0 }
.store(in: &self.cancellables)
.store(in: &cancellables)

XCTAssertEqual(value, nil)

Expand All @@ -84,13 +89,15 @@
}

func testDelay0Advance() {
var cancellables: Set<AnyCancellable> = []

let scheduler = DispatchQueue.test

var value: Int?
Just(1)
.delay(for: 0, scheduler: scheduler)
.sink { value = $0 }
.store(in: &self.cancellables)
.store(in: &cancellables)

XCTAssertEqual(value, nil)

Expand All @@ -100,13 +107,15 @@
}

func testSubscribeOnAdvance() {
var cancellables: Set<AnyCancellable> = []

let scheduler = DispatchQueue.test

var value: Int?
Just(1)
.subscribe(on: scheduler)
.sink { value = $0 }
.store(in: &self.cancellables)
.store(in: &cancellables)

XCTAssertEqual(value, nil)

Expand All @@ -116,13 +125,15 @@
}

func testReceiveOnAdvance() {
var cancellables: Set<AnyCancellable> = []

let scheduler = DispatchQueue.test

var value: Int?
Just(1)
.receive(on: scheduler)
.sink { value = $0 }
.store(in: &self.cancellables)
.store(in: &cancellables)

XCTAssertEqual(value, nil)

Expand All @@ -146,15 +157,17 @@
}

func testTwoIntervalOrdering() {
var cancellables: Set<AnyCancellable> = []

let testScheduler = DispatchQueue.test

var values: [Int] = []

testScheduler.schedule(after: testScheduler.now, interval: 2) { values.append(1) }
.store(in: &self.cancellables)
.store(in: &cancellables)

testScheduler.schedule(after: testScheduler.now, interval: 1) { values.append(42) }
.store(in: &self.cancellables)
.store(in: &cancellables)

XCTAssertEqual(values, [])
testScheduler.advance()
Expand All @@ -164,76 +177,90 @@
}

func testAdvanceToFarFuture() async {
let testScheduler = DispatchQueue.test

var tickCount = 0
Publishers.Timer(every: .seconds(1), scheduler: testScheduler)
.autoconnect()
.sink { _ in tickCount += 1 }
.store(in: &self.cancellables)

XCTAssertEqual(tickCount, 0)
await testScheduler.advance(by: .seconds(1))
XCTAssertEqual(tickCount, 1)
await testScheduler.advance(by: .seconds(1))
XCTAssertEqual(tickCount, 2)
await testScheduler.advance(by: .seconds(1_000))
XCTAssertEqual(tickCount, 1_002)
await withMainSerialExecutor {
var cancellables: Set<AnyCancellable> = []

let testScheduler = DispatchQueue.test

var tickCount = 0
Publishers.Timer(every: .seconds(1), scheduler: testScheduler)
.autoconnect()
.sink { _ in tickCount += 1 }
.store(in: &cancellables)

XCTAssertEqual(tickCount, 0)
await testScheduler.advance(by: .seconds(1))
XCTAssertEqual(tickCount, 1)
await testScheduler.advance(by: .seconds(1))
XCTAssertEqual(tickCount, 2)
await testScheduler.advance(by: .seconds(1_000))
XCTAssertEqual(tickCount, 1_002)
}
}

func testDelay0Advance_Async() async {
let scheduler = DispatchQueue.test
await withMainSerialExecutor {
var cancellables: Set<AnyCancellable> = []

var value: Int?
Just(1)
.delay(for: 0, scheduler: scheduler)
.sink { value = $0 }
.store(in: &self.cancellables)
let scheduler = DispatchQueue.test

XCTAssertEqual(value, nil)
var value: Int?
Just(1)
.delay(for: 0, scheduler: scheduler)
.sink { value = $0 }
.store(in: &cancellables)

await scheduler.advance()
XCTAssertEqual(value, nil)

XCTAssertEqual(value, 1)
await scheduler.advance()

XCTAssertEqual(value, 1)
}
}

func testAsyncSleep() async throws {
let testScheduler = DispatchQueue.test
try await withMainSerialExecutor {
let testScheduler = DispatchQueue.test

let task = Task {
try await testScheduler.sleep(for: .seconds(1))
}
let task = Task {
try await testScheduler.sleep(for: .seconds(1))
}

await testScheduler.advance(by: .seconds(1))
try await task.value
await testScheduler.advance(by: .seconds(1))
try await task.value
}
}

func testAsyncTimer() async throws {
let testScheduler = DispatchQueue.test

let task = Task {
await testScheduler.timer(interval: .seconds(1))
.prefix(10)
.reduce(into: 0) { accum, _ in accum += 1 }
await withMainSerialExecutor {
let testScheduler = DispatchQueue.test

let task = Task {
await testScheduler.timer(interval: .seconds(1))
.prefix(10)
.reduce(into: 0) { accum, _ in accum += 1 }
}

await testScheduler.advance(by: .seconds(10))
let count = await task.value
XCTAssertEqual(count, 10)
}

await testScheduler.advance(by: .seconds(10))
let count = await task.value
XCTAssertEqual(count, 10)
}

func testAsyncRun() async throws {
let testScheduler = DispatchQueue.test

let task = Task {
await testScheduler.timer(interval: .seconds(1))
.prefix(10)
.reduce(into: 0) { accum, _ in accum += 1 }
await withMainSerialExecutor {
let testScheduler = DispatchQueue.test

let task = Task {
await testScheduler.timer(interval: .seconds(1))
.prefix(10)
.reduce(into: 0) { accum, _ in accum += 1 }
}

await testScheduler.run()
let count = await task.value
XCTAssertEqual(count, 10)
}

await testScheduler.run()
let count = await task.value
XCTAssertEqual(count, 10)
}

func testNowIsAdvanced() {
Expand Down