1
1
import Testing
2
+
2
3
@testable import AWSLambdaRuntime
3
4
4
5
struct PoolTests {
5
-
6
+
6
7
@Test
7
8
func testBasicPushAndIteration( ) async throws {
8
9
let pool = LambdaHTTPServer . Pool < String > ( )
9
-
10
+
10
11
// Push values
11
12
await pool. push ( " first " )
12
13
await pool. push ( " second " )
13
-
14
+
14
15
// Iterate and verify order
15
16
var values = [ String] ( )
16
17
for try await value in pool {
17
18
values. append ( value)
18
19
if values. count == 2 { break }
19
20
}
20
-
21
+
21
22
#expect( values == [ " first " , " second " ] )
22
23
}
23
-
24
+
24
25
@Test
25
26
func testCancellation( ) async throws {
26
27
let pool = LambdaHTTPServer . Pool < String > ( )
27
-
28
+
28
29
// Create a task that will be cancelled
29
30
let task = Task {
30
31
for try await _ in pool {
31
32
Issue . record ( " Should not receive any values after cancellation " )
32
33
}
33
34
}
34
-
35
+
35
36
// Cancel the task immediately
36
37
task. cancel ( )
37
-
38
+
38
39
// This should complete without receiving any values
39
40
try await task. value
40
41
}
41
-
42
+
42
43
@Test
43
44
func testConcurrentPushAndIteration( ) async throws {
44
45
let pool = LambdaHTTPServer . Pool < Int > ( )
45
46
let iterations = 1000
46
47
var receivedValues = Set < Int > ( )
47
-
48
+
48
49
// Start consumer task first
49
50
let consumer = Task {
50
51
var count = 0
@@ -54,7 +55,7 @@ struct PoolTests {
54
55
if count >= iterations { break }
55
56
}
56
57
}
57
-
58
+
58
59
// Create multiple producer tasks
59
60
try await withThrowingTaskGroup ( of: Void . self) { group in
60
61
for i in 0 ..< iterations {
@@ -64,45 +65,45 @@ struct PoolTests {
64
65
}
65
66
try await group. waitForAll ( )
66
67
}
67
-
68
+
68
69
// Wait for consumer to complete
69
70
try await consumer. value
70
-
71
+
71
72
// Verify all values were received exactly once
72
73
#expect( receivedValues. count == iterations)
73
74
#expect( Set ( 0 ..< iterations) == receivedValues)
74
75
}
75
-
76
+
76
77
@Test
77
78
func testPushToWaitingConsumer( ) async throws {
78
79
let pool = LambdaHTTPServer . Pool < String > ( )
79
80
let expectedValue = " test value "
80
-
81
+
81
82
// Start a consumer that will wait for a value
82
83
let consumer = Task {
83
84
for try await value in pool {
84
85
#expect( value == expectedValue)
85
86
break
86
87
}
87
88
}
88
-
89
+
89
90
// Give consumer time to start waiting
90
- try await Task . sleep ( nanoseconds: 100_000_000 ) // 0.1 seconds
91
-
91
+ try await Task . sleep ( nanoseconds: 100_000_000 ) // 0.1 seconds
92
+
92
93
// Push a value
93
94
await pool. push ( expectedValue)
94
-
95
+
95
96
// Wait for consumer to complete
96
97
try await consumer. value
97
98
}
98
-
99
+
99
100
@Test
100
101
func testStressTest( ) async throws {
101
102
let pool = LambdaHTTPServer . Pool < Int > ( )
102
103
let producerCount = 10
103
104
let messagesPerProducer = 1000
104
105
var receivedValues = [ Int] ( )
105
-
106
+
106
107
// Start consumer
107
108
let consumer = Task {
108
109
var count = 0
@@ -112,7 +113,7 @@ struct PoolTests {
112
113
if count >= producerCount * messagesPerProducer { break }
113
114
}
114
115
}
115
-
116
+
116
117
// Create multiple producers
117
118
try await withThrowingTaskGroup ( of: Void . self) { group in
118
119
for p in 0 ..< producerCount {
@@ -124,12 +125,12 @@ struct PoolTests {
124
125
}
125
126
try await group. waitForAll ( )
126
127
}
127
-
128
+
128
129
// Wait for consumer to complete
129
130
try await consumer. value
130
-
131
+
131
132
// Verify we received all values
132
133
#expect( receivedValues. count == producerCount * messagesPerProducer)
133
134
#expect( Set ( receivedValues) . count == producerCount * messagesPerProducer)
134
135
}
135
- }
136
+ }
0 commit comments