Skip to content

Commit e6aa286

Browse files
committed
Tests: Used a delayElements Publisher to remove race condition in upload test
1 parent d66c2e7 commit e6aa286

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

driver-core/src/test/unit/com/mongodb/connection/ServerDescriptionTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.junit.Test;
2525

2626
import java.io.IOException;
27-
import java.net.UnknownHostException;
2827
import java.util.Collections;
2928
import java.util.Date;
3029
import java.util.HashSet;
@@ -40,11 +39,11 @@
4039
import static java.util.Arrays.asList;
4140
import static java.util.Collections.singletonList;
4241
import static org.hamcrest.CoreMatchers.not;
42+
import static org.hamcrest.MatcherAssert.assertThat;
4343
import static org.junit.Assert.assertEquals;
4444
import static org.junit.Assert.assertFalse;
4545
import static org.junit.Assert.assertNotEquals;
4646
import static org.junit.Assert.assertNull;
47-
import static org.hamcrest.MatcherAssert.assertThat;
4847
import static org.junit.Assert.assertTrue;
4948

5049
public class ServerDescriptionTest {

driver-reactive-streams/src/test/functional/com/mongodb/reactivestreams/client/gridfs/GridFSPublisherSpecification.groovy

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import java.nio.ByteBuffer
4040
import java.nio.channels.Channels
4141
import java.nio.channels.WritableByteChannel
4242
import java.security.SecureRandom
43+
import java.time.Duration
4344

4445
import static com.mongodb.ClusterFixture.TIMEOUT_DURATION
4546
import static com.mongodb.client.model.Filters.eq
@@ -433,14 +434,14 @@ class GridFSPublisherSpecification extends FunctionalSpecification {
433434

434435
def 'should cleanup when unsubscribing'() {
435436
given:
436-
def contentSize = 1024 * 1024
437+
def contentSize = 1024
437438
def contentBytes = new byte[contentSize]
438439
new SecureRandom().nextBytes(contentBytes)
439440
def options = new GridFSUploadOptions().chunkSizeBytes(1024)
440441
def data = (0..1024).collect { ByteBuffer.wrap(contentBytes) }
442+
def publisher = createPublisher(*data).delayElements(Duration.ofMillis(1000))
441443
def subscriber = new Subscriber<ObjectId>() {
442444
Subscription subscription
443-
boolean completed = false
444445

445446
@Override
446447
void onSubscribe(final Subscription s) {
@@ -457,12 +458,11 @@ class GridFSPublisherSpecification extends FunctionalSpecification {
457458

458459
@Override
459460
void onComplete() {
460-
completed = true
461461
}
462462
}
463463

464464
when:
465-
gridFSBucket.uploadFromPublisher('myFile', createPublisher(*data), options)
465+
gridFSBucket.uploadFromPublisher('myFile', publisher, options)
466466
.subscribe(subscriber)
467467
subscriber.subscription.request(1)
468468

@@ -474,8 +474,8 @@ class GridFSPublisherSpecification extends FunctionalSpecification {
474474
subscriber.subscription.cancel()
475475

476476
then:
477-
retry(50) { subscriber.completed || run(chunksCollection.&countDocuments) == 0 }
478-
subscriber.completed || run(filesCollection.&countDocuments) == 0
477+
retry(50) { run(chunksCollection.&countDocuments) == 0 }
478+
run(filesCollection.&countDocuments) == 0
479479
}
480480

481481
def retry(Integer times, Closure<Boolean> closure) {

0 commit comments

Comments
 (0)