1
1
import Logging
2
2
@testable import PostgresNIO
3
+ import Atomics
3
4
import XCTest
4
5
import NIOCore
5
6
import NIOPosix
@@ -112,59 +113,59 @@ final class PostgresNIOTests: XCTestCase {
112
113
XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
113
114
defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
114
115
115
- var receivedNotifications : [ PostgresMessage . NotificationResponse ] = [ ]
116
+ let receivedNotifications = ManagedAtomic < Int > ( 0 )
116
117
conn? . addListener ( channel: " example " ) { context, notification in
117
- receivedNotifications. append ( notification)
118
+ receivedNotifications. wrappingIncrement ( ordering: . relaxed)
119
+ XCTAssertEqual ( notification. channel, " example " )
120
+ XCTAssertEqual ( notification. payload, " " )
118
121
}
119
122
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " LISTEN example " ) . wait ( ) )
120
123
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
121
124
// Notifications are asynchronous, so we should run at least one more query to make sure we'll have received the notification response by then
122
125
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " SELECT 1 " ) . wait ( ) )
123
- XCTAssertEqual ( receivedNotifications. count, 1 )
124
- XCTAssertEqual ( receivedNotifications. first? . channel, " example " )
125
- XCTAssertEqual ( receivedNotifications. first? . payload, " " )
126
+ XCTAssertEqual ( receivedNotifications. load ( ordering: . relaxed) , 1 )
126
127
}
127
128
128
129
func testNotificationsNonEmptyPayload( ) {
129
130
var conn : PostgresConnection ?
130
131
XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
131
132
defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
132
- var receivedNotifications : [ PostgresMessage . NotificationResponse ] = [ ]
133
+ let receivedNotifications = ManagedAtomic < Int > ( 0 )
133
134
conn? . addListener ( channel: " example " ) { context, notification in
134
- receivedNotifications. append ( notification)
135
+ receivedNotifications. wrappingIncrement ( ordering: . relaxed)
136
+ XCTAssertEqual ( notification. channel, " example " )
137
+ XCTAssertEqual ( notification. payload, " Notification payload example " )
135
138
}
136
139
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " LISTEN example " ) . wait ( ) )
137
140
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example, 'Notification payload example' " ) . wait ( ) )
138
141
// Notifications are asynchronous, so we should run at least one more query to make sure we'll have received the notification response by then
139
142
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " SELECT 1 " ) . wait ( ) )
140
- XCTAssertEqual ( receivedNotifications. count, 1 )
141
- XCTAssertEqual ( receivedNotifications. first? . channel, " example " )
142
- XCTAssertEqual ( receivedNotifications. first? . payload, " Notification payload example " )
143
+ XCTAssertEqual ( receivedNotifications. load ( ordering: . relaxed) , 1 )
143
144
}
144
145
145
146
func testNotificationsRemoveHandlerWithinHandler( ) {
146
147
var conn : PostgresConnection ?
147
148
XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
148
149
defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
149
- var receivedNotifications = 0
150
+ let receivedNotifications = ManagedAtomic < Int > ( 0 )
150
151
conn? . addListener ( channel: " example " ) { context, notification in
151
- receivedNotifications += 1
152
+ receivedNotifications. wrappingIncrement ( ordering : . relaxed )
152
153
context. stop ( )
153
154
}
154
155
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " LISTEN example " ) . wait ( ) )
155
156
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
156
157
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
157
158
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " SELECT 1 " ) . wait ( ) )
158
- XCTAssertEqual ( receivedNotifications, 1 )
159
+ XCTAssertEqual ( receivedNotifications. load ( ordering : . relaxed ) , 1 )
159
160
}
160
161
161
162
func testNotificationsRemoveHandlerOutsideHandler( ) {
162
163
var conn : PostgresConnection ?
163
164
XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
164
165
defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
165
- var receivedNotifications = 0
166
+ let receivedNotifications = ManagedAtomic < Int > ( 0 )
166
167
let context = conn? . addListener ( channel: " example " ) { context, notification in
167
- receivedNotifications += 1
168
+ receivedNotifications. wrappingIncrement ( ordering : . relaxed )
168
169
}
169
170
XCTAssertNotNil ( context)
170
171
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " LISTEN example " ) . wait ( ) )
@@ -173,47 +174,47 @@ final class PostgresNIOTests: XCTestCase {
173
174
context? . stop ( )
174
175
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
175
176
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " SELECT 1 " ) . wait ( ) )
176
- XCTAssertEqual ( receivedNotifications, 1 )
177
+ XCTAssertEqual ( receivedNotifications. load ( ordering : . relaxed ) , 1 )
177
178
}
178
179
179
180
func testNotificationsMultipleRegisteredHandlers( ) {
180
181
var conn : PostgresConnection ?
181
182
XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
182
183
defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
183
- var receivedNotifications1 = 0
184
+ let receivedNotifications1 = ManagedAtomic < Int > ( 0 )
184
185
conn? . addListener ( channel: " example " ) { context, notification in
185
- receivedNotifications1 += 1
186
+ receivedNotifications1. wrappingIncrement ( ordering : . relaxed )
186
187
}
187
- var receivedNotifications2 = 0
188
+ let receivedNotifications2 = ManagedAtomic < Int > ( 0 )
188
189
conn? . addListener ( channel: " example " ) { context, notification in
189
- receivedNotifications2 += 1
190
+ receivedNotifications2. wrappingIncrement ( ordering : . relaxed )
190
191
}
191
192
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " LISTEN example " ) . wait ( ) )
192
193
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
193
194
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " SELECT 1 " ) . wait ( ) )
194
- XCTAssertEqual ( receivedNotifications1, 1 )
195
- XCTAssertEqual ( receivedNotifications2, 1 )
195
+ XCTAssertEqual ( receivedNotifications1. load ( ordering : . relaxed ) , 1 )
196
+ XCTAssertEqual ( receivedNotifications2. load ( ordering : . relaxed ) , 1 )
196
197
}
197
198
198
199
func testNotificationsMultipleRegisteredHandlersRemoval( ) throws {
199
200
var conn : PostgresConnection ?
200
201
XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
201
202
defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
202
- var receivedNotifications1 = 0
203
+ let receivedNotifications1 = ManagedAtomic < Int > ( 0 )
203
204
XCTAssertNotNil ( conn? . addListener ( channel: " example " ) { context, notification in
204
- receivedNotifications1 += 1
205
+ receivedNotifications1. wrappingIncrement ( ordering : . relaxed )
205
206
context. stop ( )
206
207
} )
207
- var receivedNotifications2 = 0
208
+ let receivedNotifications2 = ManagedAtomic < Int > ( 0 )
208
209
XCTAssertNotNil ( conn? . addListener ( channel: " example " ) { context, notification in
209
- receivedNotifications2 += 1
210
+ receivedNotifications2. wrappingIncrement ( ordering : . relaxed )
210
211
} )
211
212
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " LISTEN example " ) . wait ( ) )
212
213
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
213
214
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
214
215
XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " SELECT 1 " ) . wait ( ) )
215
- XCTAssertEqual ( receivedNotifications1, 1 )
216
- XCTAssertEqual ( receivedNotifications2, 2 )
216
+ XCTAssertEqual ( receivedNotifications1. load ( ordering : . relaxed ) , 1 )
217
+ XCTAssertEqual ( receivedNotifications2. load ( ordering : . relaxed ) , 2 )
217
218
}
218
219
219
220
func testNotificationHandlerFiltersOnChannel( ) {
@@ -1283,11 +1284,11 @@ final class PostgresNIOTests: XCTestCase {
1283
1284
XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
1284
1285
defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
1285
1286
var queries : [ [ PostgresRow ] ] ?
1286
- XCTAssertNoThrow ( queries = try conn? . prepare ( query: " SELECT $1::text as foo; " , handler: { query in
1287
+ XCTAssertNoThrow ( queries = try conn? . prepare ( query: " SELECT $1::text as foo; " , handler: { [ eventLoop ] query in
1287
1288
let a = query. execute ( [ " a " ] )
1288
1289
let b = query. execute ( [ " b " ] )
1289
1290
let c = query. execute ( [ " c " ] )
1290
- return EventLoopFuture . whenAllSucceed ( [ a, b, c] , on: self . eventLoop)
1291
+ return EventLoopFuture . whenAllSucceed ( [ a, b, c] , on: eventLoop)
1291
1292
} ) . wait ( ) )
1292
1293
XCTAssertEqual ( queries? . count, 3 )
1293
1294
var resultIterator = queries? . makeIterator ( )
0 commit comments