Skip to content

Commit 52f572e

Browse files
committed
testNoPollsAfterPollLoopSuspension
Modifications: * remove flakey tests testing that poll is not invoked when loop is suspended * add dedicated test that tests the case mentioned above
1 parent f434b91 commit 52f572e

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

Tests/SwiftKafkaTests/KafkaBackPressurePollingSystemTests.swift

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ import XCTest
1919
final class KafkaBackPressurePollingSystemTests: XCTestCase {
2020
typealias TestStateMachine = KafkaBackPressurePollingSystem.StateMachine
2121

22-
func testBackPressure() {
22+
func testBackPressure() async throws {
2323
let pollInterval = Duration.milliseconds(100)
2424

2525
var expectation: XCTestExpectation?
2626
let sut = KafkaBackPressurePollingSystem(logger: .kafkaTest)
2727
sut.pollClosure = {
28-
XCTAssertNotNil(expectation, "Unexpected invocation of poll closure")
2928
expectation?.fulfill()
3029
}
3130

@@ -38,7 +37,6 @@ final class KafkaBackPressurePollingSystemTests: XCTestCase {
3837
XCTAssertEqual(XCTWaiter().wait(for: [expectation!], timeout: 1), .completed)
3938
XCTAssertEqual(TestStateMachine.PollLoopAction.pollAndSleep, sut.nextPollLoopAction())
4039

41-
expectation = nil
4240
sut.stopProducing()
4341
XCTAssertEqual(TestStateMachine.PollLoopAction.suspendPollLoop, sut.nextPollLoopAction())
4442

@@ -47,20 +45,49 @@ final class KafkaBackPressurePollingSystemTests: XCTestCase {
4745
XCTAssertEqual(XCTWaiter().wait(for: [expectation!], timeout: 1), .completed)
4846
XCTAssertEqual(TestStateMachine.PollLoopAction.pollAndSleep, sut.nextPollLoopAction())
4947

50-
expectation = nil
5148
sut.shutDown()
5249
XCTAssertEqual(TestStateMachine.PollLoopAction.shutdownPollLoop, sut.nextPollLoopAction())
5350

5451
runTask.cancel()
5552
}
5653

54+
func testNoPollsAfterPollLoopSuspension() async throws {
55+
let pollInterval = Duration.milliseconds(100)
56+
57+
var expectation: XCTestExpectation?
58+
let sut = KafkaBackPressurePollingSystem(logger: .kafkaTest)
59+
sut.pollClosure = {
60+
expectation?.fulfill()
61+
}
62+
63+
let runTask = Task {
64+
await sut.run(pollInterval: pollInterval)
65+
}
66+
67+
expectation = XCTestExpectation(description: "Poll closure invoked after initial produceMore()")
68+
sut.produceMore()
69+
XCTAssertEqual(XCTWaiter().wait(for: [expectation!], timeout: 1), .completed)
70+
XCTAssertEqual(TestStateMachine.PollLoopAction.pollAndSleep, sut.nextPollLoopAction())
71+
72+
// We're definitely running now. Now suspend the poll loop.
73+
sut.stopProducing()
74+
XCTAssertEqual(TestStateMachine.PollLoopAction.suspendPollLoop, sut.nextPollLoopAction())
75+
// We change the poll closure so that our test fails when the poll closure is invoked.
76+
sut.pollClosure = {
77+
XCTFail("Poll loop still running after stopProducing() has been invoked")
78+
}
79+
80+
try await Task.sleep(for: .seconds(5))
81+
82+
runTask.cancel()
83+
}
84+
5785
func testRunTaskCancellationShutsDownStateMachine() async throws {
5886
let pollInterval = Duration.milliseconds(100)
5987

6088
var expectation: XCTestExpectation?
6189
let sut = KafkaBackPressurePollingSystem(logger: .kafkaTest)
6290
sut.pollClosure = {
63-
XCTAssertNotNil(expectation, "Unexpected invocation of poll closure")
6491
expectation?.fulfill()
6592
}
6693

@@ -74,7 +101,6 @@ final class KafkaBackPressurePollingSystemTests: XCTestCase {
74101
XCTAssertEqual(TestStateMachine.PollLoopAction.pollAndSleep, sut.nextPollLoopAction())
75102

76103
// We're definitely running now. Now suspend the poll loop.
77-
expectation = nil
78104
sut.stopProducing()
79105
XCTAssertEqual(TestStateMachine.PollLoopAction.suspendPollLoop, sut.nextPollLoopAction())
80106

0 commit comments

Comments
 (0)