@@ -17,135 +17,123 @@ import NIOCore
17
17
@testable import SwiftKafka
18
18
import XCTest
19
19
20
- // TODO: stream logic to setup?
21
20
final class KafkaPollingSystemTests : XCTestCase {
22
21
typealias Message = String // Could be any type, this is just for testing
23
22
typealias TestStateMachine = KafkaPollingSystem < Message > . StateMachine
24
23
25
- func testBackPressure( ) async throws {
26
- let pollInterval = Duration . milliseconds ( 100 )
24
+ let pollInterval = Duration . milliseconds ( 100 )
25
+ var sut : KafkaPollingSystem < Message > !
26
+ var expectationStream : AsyncStream < Void > !
27
+ var pollIterator : AsyncStream < Void > . Iterator !
28
+ var runTask : Task < Void , Error > !
29
+
30
+ override func setUp( ) {
31
+ self . sut = KafkaPollingSystem < Message > ( )
27
32
28
- let sut = KafkaPollingSystem < Message > ( )
29
- let expectationStream = AsyncStream { continuation in
30
- sut. pollClosure = {
33
+ // Enables us to await the next call to pollClosure
34
+ self . expectationStream = AsyncStream { continuation in
35
+ self . sut. pollClosure = {
31
36
continuation. yield ( )
32
37
}
33
38
}
34
- var pollIterator = expectationStream. makeAsyncIterator ( )
39
+ self . pollIterator = self . expectationStream. makeAsyncIterator ( )
35
40
36
- let _ = Task {
37
- try await sut. run ( pollInterval: pollInterval)
41
+ self . runTask = Task {
42
+ try await self . sut. run ( pollInterval: self . pollInterval)
38
43
}
39
44
40
- sut. produceMore ( )
41
- await pollIterator. next ( )
42
- if case . pollAndSleep = sut. nextPollLoopAction ( ) {
45
+ super. setUp ( )
46
+ }
47
+
48
+ override func tearDown( ) {
49
+ self . sut = nil
50
+ self . expectationStream = nil
51
+ self . pollIterator = nil
52
+ self . runTask = nil
53
+
54
+ super. tearDown ( )
55
+ }
56
+
57
+ func testBackPressure( ) async throws {
58
+ self . sut. produceMore ( )
59
+ await self . pollIterator. next ( )
60
+ if case . pollAndSleep = self . sut. nextPollLoopAction ( ) {
43
61
// Test passed
44
62
} else {
45
63
XCTFail ( )
46
64
}
47
65
48
- sut. stopProducing ( )
49
- if case . suspendPollLoop = sut. nextPollLoopAction ( ) {
66
+ self . sut. stopProducing ( )
67
+ if case . suspendPollLoop = self . sut. nextPollLoopAction ( ) {
50
68
// Test passed
51
69
} else {
52
70
XCTFail ( )
53
71
}
54
72
55
- sut. produceMore ( )
56
- await pollIterator. next ( )
57
- if case . pollAndSleep = sut. nextPollLoopAction ( ) {
73
+ self . sut. produceMore ( )
74
+ await self . pollIterator. next ( )
75
+ if case . pollAndSleep = self . sut. nextPollLoopAction ( ) {
58
76
// Test passed
59
77
} else {
60
78
XCTFail ( )
61
79
}
62
80
63
- sut. didTerminate ( )
64
- if case . shutdownPollLoop = sut. nextPollLoopAction ( ) {
81
+ self . sut. didTerminate ( )
82
+ if case . shutdownPollLoop = self . sut. nextPollLoopAction ( ) {
65
83
// Test passed
66
84
} else {
67
85
XCTFail ( )
68
86
}
69
87
}
70
88
71
89
func testNoPollsAfterPollLoopSuspension( ) async throws {
72
- let pollInterval = Duration . milliseconds ( 100 )
73
-
74
- let sut = KafkaPollingSystem < Message > ( )
75
-
76
- let expectationStream = AsyncStream { continuation in
77
- sut. pollClosure = {
78
- continuation. yield ( )
79
- }
80
- }
81
- var pollIterator = expectationStream. makeAsyncIterator ( )
82
-
83
- let _ = Task {
84
- try await sut. run ( pollInterval: pollInterval)
85
- }
86
-
87
- sut. produceMore ( )
88
- await pollIterator. next ( )
89
- if case . pollAndSleep = sut. nextPollLoopAction ( ) {
90
+ self . sut. produceMore ( )
91
+ await self . pollIterator. next ( )
92
+ if case . pollAndSleep = self . sut. nextPollLoopAction ( ) {
90
93
// Test passed
91
94
} else {
92
95
XCTFail ( )
93
96
}
94
97
95
98
// We're definitely running now. Now suspend the poll loop.
96
- sut. stopProducing ( )
97
- if case . suspendPollLoop = sut. nextPollLoopAction ( ) {
99
+ self . sut. stopProducing ( )
100
+ if case . suspendPollLoop = self . sut. nextPollLoopAction ( ) {
98
101
// Test passed
99
102
} else {
100
103
XCTFail ( )
101
104
}
102
105
103
106
// We change the poll closure so that our test fails when the poll closure is invoked.
104
- sut. pollClosure = {
107
+ self . sut. pollClosure = {
105
108
XCTFail ( " Poll loop still running after stopProducing() has been invoked " )
106
109
}
107
110
108
111
try await Task . sleep ( for: . seconds( 5 ) )
109
112
}
110
113
111
114
func testRunTaskCancellationShutsDownStateMachine( ) async throws {
112
- let pollInterval = Duration . milliseconds ( 100 )
113
-
114
- let sut = KafkaPollingSystem < Message > ( )
115
-
116
- let expectationStream = AsyncStream { continuation in
117
- sut. pollClosure = {
118
- continuation. yield ( )
119
- }
120
- }
121
- var pollIterator = expectationStream. makeAsyncIterator ( )
122
-
123
- let runTask = Task {
124
- try await sut. run ( pollInterval: pollInterval)
125
- }
126
-
127
- sut. produceMore ( )
128
- await pollIterator. next ( )
129
- if case . pollAndSleep = sut. nextPollLoopAction ( ) {
115
+ self . sut. produceMore ( )
116
+ await self . pollIterator. next ( )
117
+ if case . pollAndSleep = self . sut. nextPollLoopAction ( ) {
130
118
// Test passed
131
119
} else {
132
120
XCTFail ( )
133
121
}
134
122
135
123
// We're definitely running now. Now suspend the poll loop.
136
- sut. stopProducing ( )
137
- if case . suspendPollLoop = sut. nextPollLoopAction ( ) {
124
+ self . sut. stopProducing ( )
125
+ if case . suspendPollLoop = self . sut. nextPollLoopAction ( ) {
138
126
// Test passed
139
127
} else {
140
128
XCTFail ( )
141
129
}
142
130
143
131
// Cancel the Task that runs the poll loop.
144
132
// This should result in the state machine shutting down.
145
- runTask. cancel ( )
133
+ self . runTask. cancel ( )
146
134
// Sleep for a second to make sure the poll loop's canncellationHandler gets invoked.
147
135
try await Task . sleep ( for: . seconds( 1 ) )
148
- if case . shutdownPollLoop = sut. nextPollLoopAction ( ) {
136
+ if case . shutdownPollLoop = self . sut. nextPollLoopAction ( ) {
149
137
// Test passed
150
138
} else {
151
139
XCTFail ( )
0 commit comments