11import Logging
22@testable import PostgresNIO
3+ import Atomics
34import XCTest
45import NIOCore
56import NIOPosix
@@ -112,59 +113,59 @@ final class PostgresNIOTests: XCTestCase {
112113 XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
113114 defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
114115
115- var receivedNotifications : [ PostgresMessage . NotificationResponse ] = [ ]
116+ let receivedNotifications = ManagedAtomic < Int > ( 0 )
116117 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, " " )
118121 }
119122 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " LISTEN example " ) . wait ( ) )
120123 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
121124 // Notifications are asynchronous, so we should run at least one more query to make sure we'll have received the notification response by then
122125 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 )
126127 }
127128
128129 func testNotificationsNonEmptyPayload( ) {
129130 var conn : PostgresConnection ?
130131 XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
131132 defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
132- var receivedNotifications : [ PostgresMessage . NotificationResponse ] = [ ]
133+ let receivedNotifications = ManagedAtomic < Int > ( 0 )
133134 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 " )
135138 }
136139 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " LISTEN example " ) . wait ( ) )
137140 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example, 'Notification payload example' " ) . wait ( ) )
138141 // Notifications are asynchronous, so we should run at least one more query to make sure we'll have received the notification response by then
139142 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 )
143144 }
144145
145146 func testNotificationsRemoveHandlerWithinHandler( ) {
146147 var conn : PostgresConnection ?
147148 XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
148149 defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
149- var receivedNotifications = 0
150+ let receivedNotifications = ManagedAtomic < Int > ( 0 )
150151 conn? . addListener ( channel: " example " ) { context, notification in
151- receivedNotifications += 1
152+ receivedNotifications. wrappingIncrement ( ordering : . relaxed )
152153 context. stop ( )
153154 }
154155 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " LISTEN example " ) . wait ( ) )
155156 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
156157 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
157158 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " SELECT 1 " ) . wait ( ) )
158- XCTAssertEqual ( receivedNotifications, 1 )
159+ XCTAssertEqual ( receivedNotifications. load ( ordering : . relaxed ) , 1 )
159160 }
160161
161162 func testNotificationsRemoveHandlerOutsideHandler( ) {
162163 var conn : PostgresConnection ?
163164 XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
164165 defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
165- var receivedNotifications = 0
166+ let receivedNotifications = ManagedAtomic < Int > ( 0 )
166167 let context = conn? . addListener ( channel: " example " ) { context, notification in
167- receivedNotifications += 1
168+ receivedNotifications. wrappingIncrement ( ordering : . relaxed )
168169 }
169170 XCTAssertNotNil ( context)
170171 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " LISTEN example " ) . wait ( ) )
@@ -173,47 +174,47 @@ final class PostgresNIOTests: XCTestCase {
173174 context? . stop ( )
174175 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
175176 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " SELECT 1 " ) . wait ( ) )
176- XCTAssertEqual ( receivedNotifications, 1 )
177+ XCTAssertEqual ( receivedNotifications. load ( ordering : . relaxed ) , 1 )
177178 }
178179
179180 func testNotificationsMultipleRegisteredHandlers( ) {
180181 var conn : PostgresConnection ?
181182 XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
182183 defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
183- var receivedNotifications1 = 0
184+ let receivedNotifications1 = ManagedAtomic < Int > ( 0 )
184185 conn? . addListener ( channel: " example " ) { context, notification in
185- receivedNotifications1 += 1
186+ receivedNotifications1. wrappingIncrement ( ordering : . relaxed )
186187 }
187- var receivedNotifications2 = 0
188+ let receivedNotifications2 = ManagedAtomic < Int > ( 0 )
188189 conn? . addListener ( channel: " example " ) { context, notification in
189- receivedNotifications2 += 1
190+ receivedNotifications2. wrappingIncrement ( ordering : . relaxed )
190191 }
191192 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " LISTEN example " ) . wait ( ) )
192193 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
193194 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 )
196197 }
197198
198199 func testNotificationsMultipleRegisteredHandlersRemoval( ) throws {
199200 var conn : PostgresConnection ?
200201 XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
201202 defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
202- var receivedNotifications1 = 0
203+ let receivedNotifications1 = ManagedAtomic < Int > ( 0 )
203204 XCTAssertNotNil ( conn? . addListener ( channel: " example " ) { context, notification in
204- receivedNotifications1 += 1
205+ receivedNotifications1. wrappingIncrement ( ordering : . relaxed )
205206 context. stop ( )
206207 } )
207- var receivedNotifications2 = 0
208+ let receivedNotifications2 = ManagedAtomic < Int > ( 0 )
208209 XCTAssertNotNil ( conn? . addListener ( channel: " example " ) { context, notification in
209- receivedNotifications2 += 1
210+ receivedNotifications2. wrappingIncrement ( ordering : . relaxed )
210211 } )
211212 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " LISTEN example " ) . wait ( ) )
212213 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
213214 XCTAssertNoThrow ( _ = try conn? . simpleQuery ( " NOTIFY example " ) . wait ( ) )
214215 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 )
217218 }
218219
219220 func testNotificationHandlerFiltersOnChannel( ) {
@@ -1283,11 +1284,11 @@ final class PostgresNIOTests: XCTestCase {
12831284 XCTAssertNoThrow ( conn = try PostgresConnection . test ( on: eventLoop) . wait ( ) )
12841285 defer { XCTAssertNoThrow ( try conn? . close ( ) . wait ( ) ) }
12851286 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
12871288 let a = query. execute ( [ " a " ] )
12881289 let b = query. execute ( [ " b " ] )
12891290 let c = query. execute ( [ " c " ] )
1290- return EventLoopFuture . whenAllSucceed ( [ a, b, c] , on: self . eventLoop)
1291+ return EventLoopFuture . whenAllSucceed ( [ a, b, c] , on: eventLoop)
12911292 } ) . wait ( ) )
12921293 XCTAssertEqual ( queries? . count, 3 )
12931294 var resultIterator = queries? . makeIterator ( )
0 commit comments