Skip to content

Commit

Permalink
refactor: add maxTime to UPDATE_AFTER_OPTIONS (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Sep 4, 2023
1 parent d58c250 commit ffea2ae
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions cosid-mongo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
compileOnly("org.mongodb:mongodb-driver-reactivestreams")
compileOnly("io.projectreactor:reactor-core")
testImplementation(project(":cosid-test"))
testImplementation("io.projectreactor:reactor-test")
testImplementation("org.mongodb:mongodb-driver-sync")
testImplementation("io.projectreactor:reactor-core")
testImplementation("org.mongodb:mongodb-driver-reactivestreams")
Expand Down
5 changes: 4 additions & 1 deletion cosid-mongo/src/main/java/me/ahoo/cosid/mongo/Documents.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

package me.ahoo.cosid.mongo;

import me.ahoo.cosid.mongo.reactive.BlockingAdapter;

import com.mongodb.client.model.FindOneAndUpdateOptions;
import com.mongodb.client.model.ReturnDocument;

public interface Documents {
String ID_FIELD = "_id";

FindOneAndUpdateOptions UPDATE_AFTER_OPTIONS = new FindOneAndUpdateOptions()
.returnDocument(ReturnDocument.AFTER);
.returnDocument(ReturnDocument.AFTER)
.maxTime(BlockingAdapter.DEFAULT_TIME_OUT.toMillis(), java.util.concurrent.TimeUnit.MILLISECONDS);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.concurrent.TimeoutException;

public final class BlockingAdapter {
private static final Duration DEFAULT_TIME_OUT = Duration.ofSeconds(10);
public static final Duration DEFAULT_TIME_OUT = Duration.ofSeconds(10);

private BlockingAdapter() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@

package me.ahoo.cosid.mongo;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import me.ahoo.cosid.mongo.reactive.MongoReactiveIdSegmentDistributorFactory;
import me.ahoo.cosid.mongo.reactive.MongoReactiveIdSegmentInitializer;
import me.ahoo.cosid.segment.IdSegmentDistributor;
import me.ahoo.cosid.segment.IdSegmentDistributorDefinition;
import me.ahoo.cosid.segment.IdSegmentDistributorFactory;
import me.ahoo.cosid.test.MockIdGenerator;
import me.ahoo.cosid.test.segment.distributor.IdSegmentDistributorSpec;

import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.MongoDatabase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import reactor.test.StepVerifier;

class MongoReactiveIdSegmentDistributorTest extends IdSegmentDistributorSpec {
MongoDatabase mongoDatabase;
Expand Down Expand Up @@ -51,4 +60,19 @@ protected <T extends IdSegmentDistributor> void setMaxIdBack(T distributor, long
public void nextMaxIdWhenBack() {

}

@Test
public void nextMaxIdInParallel() {
var mono = Mono.fromRunnable(() -> {
String namespace = MockIdGenerator.INSTANCE.generateAsString();
IdSegmentDistributorDefinition definition = new IdSegmentDistributorDefinition(namespace, "nextMaxIdIParallel", TEST_OFFSET, TEST_STEP);
IdSegmentDistributor distributor = factory().create(definition);
long expected = TEST_OFFSET + TEST_STEP;
long actual = distributor.nextMaxId();
assertThat(actual, equalTo(expected));
long actual2 = distributor.nextMaxId();
assertThat(actual2, greaterThan(actual));
}).subscribeOn(Schedulers.parallel());
StepVerifier.create(mono).verifyComplete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
*/
public abstract class IdSegmentDistributorSpec {

static final long TEST_OFFSET = 0;
static final long TEST_STEP = 100;
protected static final long TEST_OFFSET = 0;
protected static final long TEST_STEP = 100;

protected abstract IdSegmentDistributorFactory getFactory();

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# limitations under the License.
#
group=me.ahoo.cosid
version=2.5.0
version=2.5.1
description=Universal, flexible, high-performance distributed ID generator.
website=https://github.com/Ahoo-Wang/CosId
issues=https://github.com/Ahoo-Wang/CosId/issues
Expand Down

0 comments on commit ffea2ae

Please sign in to comment.