Skip to content

Commit

Permalink
Merge pull request #130 from d-exclaimation/chore/test-cleanup-1
Browse files Browse the repository at this point in the history
Task starvation solutions + better test websocket client
  • Loading branch information
d-exclaimation authored Feb 19, 2023
2 parents 92d353b + 5076ed1 commit d174002
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 126 deletions.
17 changes: 17 additions & 0 deletions Sources/Pioneer/Extensions/Futures/Task+Starvation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Task+Starvation.swift
// pioneer
//
// Created by d-exclaimation on 22:38.
//

public extension Task where Success == Never, Failure == Never {
/// Postpone the current task and allows other tasks to execute.
///
/// A task can voluntarily suspend itself in the middle of a long-running operation that doesn’t contain any suspension points, to let other tasks run for a while before execution returns to this task.
///
/// This is a sister method to ``Task/yield()``, which is a suspension point that allows other tasks to run that forces the suspension of the current task and ignores the current task’s priority.
static func postpone() async throws {
try await Task.sleep(nanoseconds: 0)
}
}
12 changes: 6 additions & 6 deletions Tests/PioneerTests/ActorTests/DroneTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ final class DroneTests: XCTestCase {

/// Setup a GraphQLSchema, Pioneer drone, and a TestConsumer
/// - Returns: The configured consumer and drone itself
func setup() throws -> (TestConsumer, Pioneer<Resolver, Void>.Drone) {
let consumer = TestConsumer()
func setup() throws -> (TestClient, Pioneer<Resolver, Void>.Drone) {
let consumer = TestClient()
let drone: Pioneer<Resolver, Void>.Drone = .init(
.init(
id: UUID(),
Expand Down Expand Up @@ -100,11 +100,11 @@ final class DroneTests: XCTestCase {
)

// Get the first message
let result = await consumer.wait()
let result = await consumer.pull()
XCTAssert(result.contains("payload") && result.contains("Hello") && result.contains("1"))

// Get the second message (completion)
let completion = await consumer.wait()
let completion = await consumer.pull()
XCTAssert(completion.contains("1"))
}

Expand All @@ -122,7 +122,7 @@ final class DroneTests: XCTestCase {
await drone.stop(for: "2")

// Should not give anything even completion
let result = await consumer.waitUntil(time: 0.3)
let result = await consumer.pull(until: 0.3)
XCTAssert(result == nil)
}

Expand All @@ -138,7 +138,7 @@ final class DroneTests: XCTestCase {
await drone.acid()

// Should not give anything even completion
let result = await consumer.waitUntil(time: 0.3)
let result = await consumer.pull(until: 0.3)
XCTAssert(result == nil)
}
}
14 changes: 7 additions & 7 deletions Tests/PioneerTests/ActorTests/ProbeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ final class ProbeTests: XCTestCase {
}

private let group = MultiThreadedEventLoopGroup(numberOfThreads: 4)
private let schema = try! Schema<Resolver, Void>.init {
private let schema = try! Schema<Resolver, Void> {
Query {
Graphiti.Field("hello", at: Resolver.test)
Field("hello", at: Resolver.test)
}
Subscription {
SubscriptionField("simple", as: String.self, atSub: Resolver.subscription)
Expand All @@ -45,8 +45,8 @@ final class ProbeTests: XCTestCase {
}

/// Setup a Process using a custom test consumer
func consumer() -> (Pioneer<Resolver, Void>.WebSocketClient, TestConsumer) {
let consumer = TestConsumer()
func consumer() -> (Pioneer<Resolver, Void>.WebSocketClient, TestClient) {
let consumer = TestClient()
return (
.init(
id: UUID(),
Expand All @@ -71,7 +71,7 @@ final class ProbeTests: XCTestCase {
await probe.outgoing(with: "1", to: process, given: message)

// Should receive the message and the completion
let results = await con.waitAllWithValue(requirement: 2)
let results = await con.pullMany(of: 2)
guard let _ = results.first(where: { $0.contains("\"complete\"") && $0.contains("\"1\"") }) else {
return XCTFail("No completion")
}
Expand Down Expand Up @@ -100,7 +100,7 @@ final class ProbeTests: XCTestCase {
await probe.once(for: process.id, with: "2", given: .init(query: "query { hello }", operationName: nil, variables: nil))

// Should receive the message and the completion
let results = await consumer.waitAllWithValue(requirement: 2)
let results = await consumer.pullMany(of: 2)
guard let _ = results.first(where: { $0.contains("\"complete\"") && $0.contains("\"2\"") }) else {
return XCTFail("No completion")
}
Expand Down Expand Up @@ -142,7 +142,7 @@ final class ProbeTests: XCTestCase {
await probe.once(for: process.id, with: "3", given: .init(query: "query { idk }", operationName: nil, variables: nil))

// Should receive the message and the completion
let results = await consumer.waitAllWithValue(requirement: 2)
let results = await consumer.pullMany(of: 2)
guard let _ = results.first(where: { $0.contains("\"complete\"") && $0.contains("\"3\"") }) else {
return XCTFail("No completion")
}
Expand Down
Loading

0 comments on commit d174002

Please sign in to comment.