Skip to content

Commit

Permalink
Fix Distributor State Tests/Loosen platform requirements (#5)
Browse files Browse the repository at this point in the history
Loose platform requirements, fix distributor state tests

Verify all tests @1k iterations

Signed-off-by: EandJsFilmCrew <789213+rvsrvs@users.noreply.github.com>
  • Loading branch information
rvsrvs authored Jun 4, 2022
1 parent 11b0561 commit 0307727
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import PackageDescription
let package = Package(
name: "FreeCombine",
platforms: [
.iOS(.v15),
.macOS(.v12)
.iOS(.v13),
.macOS(.v10_15),
.watchOS(.v6),
.tvOS(.v13)
],
products: [
.library(
Expand Down
8 changes: 5 additions & 3 deletions Sources/FreeCombine/Decombinator/DistributorState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public struct DistributorState<Output: Sendable> {
case receive(AsyncStream<Output>.Result, UnsafeContinuation<Void, Never>?)
case subscribe(
@Sendable (AsyncStream<Output>.Result) async throws -> Demand,
UnsafeContinuation<Cancellable<Demand>, Swift.Error>?
UnsafeContinuation<Cancellable<Demand>, Swift.Error>
)
case unsubscribe(Int)
}
Expand Down Expand Up @@ -64,7 +64,7 @@ public struct DistributorState<Output: Sendable> {
if let currentValue = currentValue, try await downstream(.value(currentValue)) == .done {
// FIXME: handle first value cancellation
}
continuation?.resume(returning: repeater)
continuation.resume(returning: repeater)
return .none
case let .unsubscribe(channelId):
guard let downstream = repeaters.removeValue(forKey: channelId) else {
Expand Down Expand Up @@ -144,7 +144,9 @@ public struct DistributorState<Output: Sendable> {
fatalError("Could not get demand. Error: \(error)")
}
},
result: { await repeater.result.map(\.mostRecentDemand) }
result: {
await repeater.result.map(\.mostRecentDemand)
}
)
}
}
36 changes: 28 additions & 8 deletions Tests/FreeCombineTests/DistributorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class DistributorTests: XCTestCase {
}
}

func xtestSimpleSubscribeAndSend() async throws {
func testSimpleSubscribeAndSend() async throws {
let counter = Counter()
let expectation1: CheckedExpectation<Void> = await .init()
let downstream1: @Sendable (AsyncStream<Int>.Result) async throws -> Demand = { result in
Expand Down Expand Up @@ -98,15 +98,29 @@ class DistributorTests: XCTestCase {
}
}

let taskSync = await CheckedExpectation<Void>()
var t: Task<Void, Swift.Error>!
let distributorValue = ValueRef(value: DistributorState(currentValue: 13, nextKey: 0, downstreams: [:]))
let _: Void = await withUnsafeContinuation { c in
t = Task {
let cancellable: Cancellable<Demand> = try await withUnsafeThrowingContinuation { taskC in
let cancellable1: Cancellable<Demand> = try await withUnsafeThrowingContinuation { taskC in
Task {
do {
var distributor = DistributorState(currentValue: 13, nextKey: 0, downstreams: [:])
var distributor = await distributorValue.value
XCTAssert(distributor.repeaters.count == 0, "Incorrect number of repeaters = \(distributor.repeaters.count)")
_ = try await distributor.reduce(action: .subscribe(downstream1, .none))
_ = try await distributor.reduce(action: .subscribe(downstream1, taskC))
await distributorValue.set(value: distributor)
_ = try await taskSync.complete()
} catch {
XCTFail("Caught: \(error)")
}
}
}
let cancellable2: Cancellable<Demand> = try await withUnsafeThrowingContinuation { taskC in
Task {
do {
_ = try await taskSync.value
var distributor = await distributorValue.value
XCTAssert(distributor.repeaters.count == 1, "Incorrect number of repeaters = \(distributor.repeaters.count)")
_ = try await distributor.reduce(action: .subscribe(downstream2, taskC))
XCTAssert(distributor.repeaters.count == 2, "Incorrect number of repeaters = \(distributor.repeaters.count)")
Expand All @@ -117,14 +131,18 @@ class DistributorTests: XCTestCase {
XCTAssert(count2 == 4, "Incorrect number of sends: \(count2)")
_ = try await distributor.reduce(action: .receive(.completion(.finished), c))
XCTAssert(distributor.repeaters.count == 0, "Incorrect number of repeaters = \(distributor.repeaters.count)")
await distributorValue.set(value: distributor)
} catch {
XCTFail("Caught: \(error)")
}
}
}
cancellable.cancel()
_ = try await cancellable.value
c.resume()
do {
_ = try await cancellable1.value
_ = try await cancellable2.value
} catch {
XCTFail("Failed to complete tasks")
}
}
}
do {
Expand All @@ -139,6 +157,8 @@ class DistributorTests: XCTestCase {
do {
_ = try await t.value
}
catch { XCTFail("Should have completed") }
catch {
XCTFail("Should have completed")
}
}
}

0 comments on commit 0307727

Please sign in to comment.