-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAsyncPollerTests.swift
66 lines (56 loc) · 1.97 KB
/
AsyncPollerTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import AsyncPoller
import Testing
@Suite("Async Poller Tests")
struct AsyncPollerTests {
@Test("Happy Path")
func happyPath() async throws {
let configuration = SimplePollingConfiguration(
pollingInterval: 0.0001,
timeoutInterval: 1
)
let condition: (String) -> Bool = { $0 == "success" }
let pollingJob: @Sendable () async -> String = {
"success"
}
let poller = AsyncPoller(configuration: configuration, completionCondition: condition, pollingJob: pollingJob)
try await confirmation { confirmed in
let result = try await poller.start()
#expect(result == "success")
confirmed()
}
}
@Test("Polling timeout")
func timeout() async throws {
let configuration = SimplePollingConfiguration(
pollingInterval: 0.000001,
timeoutInterval: 0.0001
)
let condition: (String) -> Bool = { $0 == "success" }
let pollingJob: @Sendable () async -> String = {
"not success"
}
let poller = AsyncPoller(configuration: configuration, completionCondition: condition, pollingJob: pollingJob)
await #expect(throws: PollingError.timeout) {
let _ = try await poller.start()
}
}
@Test("Already Polling")
func alreadyPolling() async throws {
let configuration = SimplePollingConfiguration(
pollingInterval: 0.01,
timeoutInterval: 2
)
let condition: (String) -> Bool = { $0 == "success" }
let pollingJob: @Sendable () async -> String = {
"not success"
}
let poller = AsyncPoller(configuration: configuration, completionCondition: condition, pollingJob: pollingJob)
Task {
try await poller.start()
}
await Task.yield()
await #expect(throws: PollingError.alreadyPolling) {
try await poller.start()
}
}
}