Skip to content

Commit 2b4f685

Browse files
authored
Fix flaky mongo reactive test (#10897)
1 parent 589df4c commit 2b4f685

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

instrumentation/mongo/mongo-4.0/javaagent/src/test/groovy/Mongo4ReactiveClientTest.groovy

+33-17
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ import spock.lang.Shared
2323

2424
import java.util.concurrent.CompletableFuture
2525
import java.util.concurrent.CountDownLatch
26+
import java.util.concurrent.TimeUnit
2627

2728
class Mongo4ReactiveClientTest extends AbstractMongoClientTest<MongoCollection<Document>> implements AgentTestTrait {
2829

2930
@Shared
3031
MongoClient client
32+
@Shared
33+
List<Closeable> cleanup = []
3134

3235
def setupSpec() throws Exception {
3336
client = MongoClients.create("mongodb://localhost:$port")
@@ -36,18 +39,27 @@ class Mongo4ReactiveClientTest extends AbstractMongoClientTest<MongoCollection<D
3639
def cleanupSpec() throws Exception {
3740
client?.close()
3841
client = null
42+
cleanup.forEach {
43+
it.close()
44+
}
3945
}
4046

4147
@Override
4248
void createCollection(String dbName, String collectionName) {
4349
MongoDatabase db = client.getDatabase(dbName)
44-
db.createCollection(collectionName).subscribe(toSubscriber {})
50+
def latch = new CountDownLatch(1)
51+
db.createCollection(collectionName).subscribe(toSubscriber { latch.countDown() })
52+
latch.await(30, TimeUnit.SECONDS)
4553
}
4654

4755
@Override
4856
void createCollectionNoDescription(String dbName, String collectionName) {
49-
MongoDatabase db = MongoClients.create("mongodb://localhost:${port}").getDatabase(dbName)
50-
db.createCollection(collectionName).subscribe(toSubscriber {})
57+
def tmpClient = MongoClients.create("mongodb://localhost:${port}")
58+
cleanup.add(tmpClient)
59+
MongoDatabase db = tmpClient.getDatabase(dbName)
60+
def latch = new CountDownLatch(1)
61+
db.createCollection(collectionName).subscribe(toSubscriber { latch.countDown() })
62+
latch.await(30, TimeUnit.SECONDS)
5163
}
5264

5365
@Override
@@ -63,16 +75,20 @@ class Mongo4ReactiveClientTest extends AbstractMongoClientTest<MongoCollection<D
6375
new ServerAddress("localhost", port)))
6476
})
6577
settings.build()
66-
MongoDatabase db = MongoClients.create(settings.build()).getDatabase(dbName)
67-
db.createCollection(collectionName).subscribe(toSubscriber {})
78+
def tmpClient = MongoClients.create(settings.build())
79+
cleanup.add(tmpClient)
80+
MongoDatabase db = tmpClient.getDatabase(dbName)
81+
def latch = new CountDownLatch(1)
82+
db.createCollection(collectionName).subscribe(toSubscriber { latch.countDown() })
83+
latch.await(30, TimeUnit.SECONDS)
6884
}
6985

7086
@Override
7187
int getCollection(String dbName, String collectionName) {
7288
MongoDatabase db = client.getDatabase(dbName)
7389
def count = new CompletableFuture<Integer>()
7490
db.getCollection(collectionName).estimatedDocumentCount().subscribe(toSubscriber { count.complete(it) })
75-
return count.join()
91+
return count.get(30, TimeUnit.SECONDS)
7692
}
7793

7894
@Override
@@ -81,7 +97,7 @@ class Mongo4ReactiveClientTest extends AbstractMongoClientTest<MongoCollection<D
8197
MongoDatabase db = client.getDatabase(dbName)
8298
def latch1 = new CountDownLatch(1)
8399
db.createCollection(collectionName).subscribe(toSubscriber { latch1.countDown() })
84-
latch1.await()
100+
latch1.await(30, TimeUnit.SECONDS)
85101
return db.getCollection(collectionName)
86102
}
87103
ignoreTracesAndClear(1)
@@ -94,7 +110,7 @@ class Mongo4ReactiveClientTest extends AbstractMongoClientTest<MongoCollection<D
94110
collection.insertOne(new Document("password", "SECRET")).subscribe(toSubscriber {
95111
collection.estimatedDocumentCount().subscribe(toSubscriber { count.complete(it) })
96112
})
97-
return count.join()
113+
return count.get(30, TimeUnit.SECONDS)
98114
}
99115

100116
@Override
@@ -103,11 +119,11 @@ class Mongo4ReactiveClientTest extends AbstractMongoClientTest<MongoCollection<D
103119
MongoDatabase db = client.getDatabase(dbName)
104120
def latch1 = new CountDownLatch(1)
105121
db.createCollection(collectionName).subscribe(toSubscriber { latch1.countDown() })
106-
latch1.await()
122+
latch1.await(30, TimeUnit.SECONDS)
107123
def coll = db.getCollection(collectionName)
108124
def latch2 = new CountDownLatch(1)
109125
coll.insertOne(new Document("password", "OLDPW")).subscribe(toSubscriber { latch2.countDown() })
110-
latch2.await()
126+
latch2.await(30, TimeUnit.SECONDS)
111127
return coll
112128
}
113129
ignoreTracesAndClear(1)
@@ -124,7 +140,7 @@ class Mongo4ReactiveClientTest extends AbstractMongoClientTest<MongoCollection<D
124140
result.complete(it)
125141
collection.estimatedDocumentCount().subscribe(toSubscriber { count.complete(it) })
126142
})
127-
return result.join().modifiedCount
143+
return result.get(30, TimeUnit.SECONDS).modifiedCount
128144
}
129145

130146
@Override
@@ -133,11 +149,11 @@ class Mongo4ReactiveClientTest extends AbstractMongoClientTest<MongoCollection<D
133149
MongoDatabase db = client.getDatabase(dbName)
134150
def latch1 = new CountDownLatch(1)
135151
db.createCollection(collectionName).subscribe(toSubscriber { latch1.countDown() })
136-
latch1.await()
152+
latch1.await(30, TimeUnit.SECONDS)
137153
def coll = db.getCollection(collectionName)
138154
def latch2 = new CountDownLatch(1)
139155
coll.insertOne(new Document("password", "SECRET")).subscribe(toSubscriber { latch2.countDown() })
140-
latch2.await()
156+
latch2.await(30, TimeUnit.SECONDS)
141157
return coll
142158
}
143159
ignoreTracesAndClear(1)
@@ -152,7 +168,7 @@ class Mongo4ReactiveClientTest extends AbstractMongoClientTest<MongoCollection<D
152168
result.complete(it)
153169
collection.estimatedDocumentCount().subscribe(toSubscriber { count.complete(it) })
154170
})
155-
return result.join().deletedCount
171+
return result.get(30, TimeUnit.SECONDS).deletedCount
156172
}
157173

158174
@Override
@@ -173,18 +189,18 @@ class Mongo4ReactiveClientTest extends AbstractMongoClientTest<MongoCollection<D
173189
db.createCollection(collectionName).subscribe(toSubscriber {
174190
latch.countDown()
175191
})
176-
latch.await()
192+
latch.await(30, TimeUnit.SECONDS)
177193
return db.getCollection(collectionName)
178194
}
179195
ignoreTracesAndClear(1)
180196
def result = new CompletableFuture<Throwable>()
181197
collection.updateOne(new BsonDocument(), new BsonDocument()).subscribe(toSubscriber {
182198
result.complete(it)
183199
})
184-
throw result.join()
200+
throw result.get(30, TimeUnit.SECONDS)
185201
}
186202

187-
def Subscriber<?> toSubscriber(Closure closure) {
203+
Subscriber<?> toSubscriber(Closure closure) {
188204
return new Subscriber() {
189205
boolean hasResult
190206

0 commit comments

Comments
 (0)