@@ -2,6 +2,7 @@ import Foundation
22@testable import MongoSwift
33import Nimble
44import NIO
5+ import NIOConcurrencyHelpers
56import TestsCommon
67
78private let doc1 : Document = [ " _id " : 1 , " x " : 1 ]
@@ -210,35 +211,38 @@ final class AsyncMongoCursorTests: MongoSwiftTestCase {
210211 }
211212
212213 func testForEach( ) throws {
213- var count = 0
214- let increment : ( Document ) -> Void = { _ in count += 1 }
214+ let count = NIOAtomic< Int> . makeAtomic( value: 0 )
215+ let increment : ( Document ) -> Void = { _ in
216+ _ = count. add ( 1 )
217+ }
215218
216219 // non-tailable
217220 try self . withTestNamespace { _, _, coll in
218221 // empty collection
219222 var cursor = try coll. find ( ) . wait ( )
220223 _ = try cursor. forEach ( increment) . wait ( )
221- expect ( count) . to ( equal ( 0 ) )
224+ expect ( count. load ( ) ) . to ( equal ( 0 ) )
222225 expect ( try cursor. isAlive ( ) . wait ( ) ) . to ( beFalse ( ) )
223226
224227 _ = try coll. insertMany ( [ doc1, doc2] ) . wait ( )
225228
226229 // non empty
227230 cursor = try coll. find ( ) . wait ( )
228231 _ = try cursor. forEach ( increment) . wait ( )
229- expect ( count) . to ( equal ( 2 ) )
232+ expect ( count. load ( ) ) . to ( equal ( 2 ) )
230233 expect ( try cursor. isAlive ( ) . wait ( ) ) . to ( beFalse ( ) )
231234 }
232235
233- count = 0
236+ count. store ( 0 )
237+
234238 // tailable
235239 let collOptions = CreateCollectionOptions ( capped: true , max: 3 , size: 1000 )
236240 try self . withTestNamespace ( collectionOptions: collOptions) { _, _, coll in
237241 let cursorOpts = FindOptions ( cursorType: . tailable)
238242
239243 var cursor = try coll. find ( options: cursorOpts) . wait ( )
240244 _ = try cursor. forEach ( increment) . wait ( )
241- expect ( count) . to ( equal ( 0 ) )
245+ expect ( count. load ( ) ) . to ( equal ( 0 ) )
242246 // no documents matched initial query, so cursor is dead
243247 expect ( try cursor. isAlive ( ) . wait ( ) ) . to ( beFalse ( ) )
244248
@@ -247,7 +251,7 @@ final class AsyncMongoCursorTests: MongoSwiftTestCase {
247251
248252 // start running forEach; future will not resolve since cursor is tailable
249253 let future = cursor. forEach ( increment)
250- expect ( count) . toEventually ( equal ( 2 ) )
254+ expect ( count. load ( ) ) . toEventually ( equal ( 2 ) )
251255
252256 // killing the cursor should resolve the future and not error
253257 expect ( try cursor. kill ( ) . wait ( ) ) . toNot ( throwError ( ) )
0 commit comments