Java Code Examples for reactor.core.publisher.Mono#fromDirect()

The following examples show how to use reactor.core.publisher.Mono#fromDirect() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleLifoPoolTest.java    From reactor-pool with Apache License 2.0 6 votes vote down vote up
@Test
void consistentThreadDeliveringWhenNoElementsButNotFull() throws InterruptedException {
    Scheduler deliveryScheduler = Schedulers.newSingle("delivery");
    AtomicReference<String> threadName = new AtomicReference<>();
    Scheduler acquireScheduler = Schedulers.newSingle("acquire");
    PoolConfig<PoolableTest> testConfig = poolableTestConfig(0, 1,
            Mono.fromCallable(PoolableTest::new)
                .subscribeOn(Schedulers.newParallel("poolable test allocator")),
            deliveryScheduler);
    SimpleLifoPool<PoolableTest> pool = new SimpleLifoPool<>(testConfig);

    //the pool is started with no elements, and has capacity for 1
    //we prepare to acquire, which would allocate the element
    Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just));
    CountDownLatch latch = new CountDownLatch(1);

    //we actually request the acquire from a separate thread, but the allocation also happens in a dedicated thread
    //we look at which thread the element was delivered from
    acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown));
    latch.await(1, TimeUnit.SECONDS);

    assertThat(threadName.get())
            .startsWith("delivery-");
}
 
Example 2
Source File: SimpleLifoPoolTest.java    From reactor-pool with Apache License 2.0 6 votes vote down vote up
@Test
void consistentThreadDeliveringWhenHasElements() throws InterruptedException {
    Scheduler deliveryScheduler = Schedulers.newSingle("delivery");
    AtomicReference<String> threadName = new AtomicReference<>();
    Scheduler acquireScheduler = Schedulers.newSingle("acquire");
    PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1,
            Mono.fromCallable(PoolableTest::new)
                .subscribeOn(Schedulers.newParallel("poolable test allocator")),
            deliveryScheduler);
    SimpleLifoPool<PoolableTest> pool = new SimpleLifoPool<>(testConfig);

    //the pool is started with one available element
    //we prepare to acquire it
    Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just));
    CountDownLatch latch = new CountDownLatch(1);

    //we actually request the acquire from a separate thread and see from which thread the element was delivered
    acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown));
    latch.await(1, TimeUnit.SECONDS);

    assertThat(threadName.get())
            .startsWith("delivery-");
}
 
Example 3
Source File: SimpleFifoPoolTest.java    From reactor-pool with Apache License 2.0 6 votes vote down vote up
@Test
void consistentThreadDeliveringWhenHasElements() throws InterruptedException {
    Scheduler deliveryScheduler = Schedulers.newSingle("delivery");
    AtomicReference<String> threadName = new AtomicReference<>();
    Scheduler acquireScheduler = Schedulers.newSingle("acquire");
    PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1,
            Mono.fromCallable(PoolableTest::new)
                .subscribeOn(Schedulers.newParallel("poolable test allocator")),
            deliveryScheduler);
    SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig);

    //the pool is started with one available element
    //we prepare to acquire it
    Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just));
    CountDownLatch latch = new CountDownLatch(1);

    //we actually request the acquire from a separate thread and see from which thread the element was delivered
    acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown));
    latch.await(1, TimeUnit.SECONDS);

    assertThat(threadName.get())
            .startsWith("delivery-");
}
 
Example 4
Source File: SimpleFifoPoolTest.java    From reactor-pool with Apache License 2.0 6 votes vote down vote up
@Test
void consistentThreadDeliveringWhenNoElementsButNotFull() throws InterruptedException {
    Scheduler deliveryScheduler = Schedulers.newSingle("delivery");
    AtomicReference<String> threadName = new AtomicReference<>();
    Scheduler acquireScheduler = Schedulers.newSingle("acquire");
    PoolConfig<PoolableTest> testConfig = poolableTestConfig(0, 1,
            Mono.fromCallable(PoolableTest::new)
                .subscribeOn(Schedulers.newParallel("poolable test allocator")),
            deliveryScheduler);
    SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig);

    //the pool is started with no elements, and has capacity for 1
    //we prepare to acquire, which would allocate the element
    Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just));
    CountDownLatch latch = new CountDownLatch(1);

    //we actually request the acquire from a separate thread, but the allocation also happens in a dedicated thread
    //we look at which thread the element was delivered from
    acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown));
    latch.await(1, TimeUnit.SECONDS);

    assertThat(threadName.get())
            .startsWith("delivery-");
}
 
Example 5
Source File: SimpleLifoPoolTest.java    From reactor-pool with Apache License 2.0 6 votes vote down vote up
@Test
void defaultThreadDeliveringWhenNoElementsButNotFull() throws InterruptedException {
    AtomicReference<String> threadName = new AtomicReference<>();
    Scheduler acquireScheduler = Schedulers.newSingle("acquire");
    PoolConfig<PoolableTest> testConfig = poolableTestConfig(0, 1,
            Mono.fromCallable(PoolableTest::new)
                .subscribeOn(Schedulers.newParallel("poolable test allocator")));
    SimpleLifoPool<PoolableTest> pool = new SimpleLifoPool<>(testConfig);

    //the pool is started with no elements, and has capacity for 1
    //we prepare to acquire, which would allocate the element
    Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just));
    CountDownLatch latch = new CountDownLatch(1);

    //we actually request the acquire from a separate thread, but the allocation also happens in a dedicated thread
    //we look at which thread the element was delivered from
    acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown));
    latch.await(1, TimeUnit.SECONDS);

    assertThat(threadName.get())
            .startsWith("poolable test allocator-");
}
 
Example 6
Source File: SimpleLifoPoolTest.java    From reactor-pool with Apache License 2.0 6 votes vote down vote up
@Test
void defaultThreadDeliveringWhenHasElements() throws InterruptedException {
    AtomicReference<String> threadName = new AtomicReference<>();
    Scheduler acquireScheduler = Schedulers.newSingle("acquire");
    PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1,
            Mono.fromCallable(PoolableTest::new)
                .subscribeOn(Schedulers.newParallel("poolable test allocator")));
    SimpleLifoPool<PoolableTest> pool = new SimpleLifoPool<>(testConfig);
    pool.warmup().block();

    //the pool is started and warmed up with one available element
    //we prepare to acquire it
    Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just));
    CountDownLatch latch = new CountDownLatch(1);

    //we actually request the acquire from a separate thread and see from which thread the element was delivered
    acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()), e -> latch.countDown(), latch::countDown));
    latch.await(1, TimeUnit.SECONDS);

    assertThat(threadName.get())
            .startsWith("acquire-");
}
 
Example 7
Source File: MonoTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void monoFromDirectPublisherCallsAssemblyHook() {
	final Publisher<Integer> source = TestPublisher.create();
	Assertions.assertThat(source).isNotInstanceOf(Flux.class); //smoke test this is a Publisher

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Mono.fromDirect(source);
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 8
Source File: DefaultTestPublisher.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<T> mono() {
	if (violations.isEmpty()) {
		return Mono.from(this);
	}
	else {
		return Mono.fromDirect(this);
	}
}
 
Example 9
Source File: MonoTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void monoFromDirectFluxCallsAssemblyHook() {
	final Flux<Integer> source = Flux.just(1).hide();

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Mono.fromDirect(source);
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 10
Source File: MonoTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void monoFromDirectFluxWrappingMonoDoesntCallAssemblyHook() {
	final Flux<Integer> source = Flux.from(Mono.just(1).hide());

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Mono.fromDirect(source);
	Assertions.assertThat(wrappedCount).hasValue(0);
}
 
Example 11
Source File: SimpleLifoPoolTest.java    From reactor-pool with Apache License 2.0 5 votes vote down vote up
@Test
void defaultThreadDeliveringWhenNoElementsAndFull() throws InterruptedException {
    AtomicReference<String> threadName = new AtomicReference<>();
    Scheduler acquireScheduler = Schedulers.newSingle("acquire");
    Scheduler releaseScheduler = Schedulers.fromExecutorService(
            Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"release"))));
    PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1,
            Mono.fromCallable(PoolableTest::new)
                .subscribeOn(Schedulers.newParallel("poolable test allocator")));
    SimpleLifoPool<PoolableTest> pool = new SimpleLifoPool<>(testConfig);

    //the pool is started with one elements, and has capacity for 1.
    //we actually first acquire that element so that next acquire will wait for a release
    PooledRef<PoolableTest> uniqueSlot = pool.acquire().block();
    assertThat(uniqueSlot).isNotNull();

    //we prepare next acquire
    Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just));
    CountDownLatch latch = new CountDownLatch(1);

    //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered
    acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()),
            e -> latch.countDown(), latch::countDown));
    //after a short while, we release the acquired unique element from a third thread
    releaseScheduler.schedule(uniqueSlot.release()::block, 500, TimeUnit.MILLISECONDS);
    latch.await(1, TimeUnit.SECONDS);

    assertThat(threadName.get())
            .isEqualTo("release");
}
 
Example 12
Source File: SimpleFifoPoolTest.java    From reactor-pool with Apache License 2.0 5 votes vote down vote up
@Test
void consistentThreadDeliveringWhenNoElementsAndFull() throws InterruptedException {
    Scheduler deliveryScheduler = Schedulers.newSingle("delivery");
    AtomicReference<String> threadName = new AtomicReference<>();
    Scheduler acquireScheduler = Schedulers.newSingle("acquire");
    Scheduler releaseScheduler = Schedulers.fromExecutorService(
            Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"release"))));
    PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1,
            Mono.fromCallable(PoolableTest::new)
                .subscribeOn(Schedulers.newParallel("poolable test allocator")),
            deliveryScheduler);
    SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig);

    //the pool is started with one elements, and has capacity for 1.
    //we actually first acquire that element so that next acquire will wait for a release
    PooledRef<PoolableTest> uniqueSlot = pool.acquire().block();
    assertThat(uniqueSlot).isNotNull();

    //we prepare next acquire
    Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just));
    CountDownLatch latch = new CountDownLatch(1);

    //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered
    acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()),
            e -> latch.countDown(), latch::countDown));
    //after a short while, we release the acquired unique element from a third thread
    releaseScheduler.schedule(uniqueSlot.release()::block, 500, TimeUnit.MILLISECONDS);
    latch.await(1, TimeUnit.SECONDS);

    assertThat(threadName.get())
            .startsWith("delivery-");
}
 
Example 13
Source File: SimpleFifoPoolTest.java    From reactor-pool with Apache License 2.0 5 votes vote down vote up
@Test
void defaultThreadDeliveringWhenNoElementsAndFull() throws InterruptedException {
    AtomicReference<String> threadName = new AtomicReference<>();
    Scheduler acquireScheduler = Schedulers.newSingle("acquire");
    Scheduler releaseScheduler = Schedulers.fromExecutorService(
            Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"release"))));
    PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1,
            Mono.fromCallable(PoolableTest::new)
                .subscribeOn(Schedulers.newParallel("poolable test allocator")));
    SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig);

    //the pool is started with one elements, and has capacity for 1.
    //we actually first acquire that element so that next acquire will wait for a release
    PooledRef<PoolableTest> uniqueSlot = pool.acquire().block();
    assertThat(uniqueSlot).isNotNull();

    //we prepare next acquire
    Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just));
    CountDownLatch latch = new CountDownLatch(1);

    //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered
    acquireScheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName()),
            e -> latch.countDown(), latch::countDown));
    //after a short while, we release the acquired unique element from a third thread
    releaseScheduler.schedule(uniqueSlot.release()::block, 500, TimeUnit.MILLISECONDS);
    latch.await(1, TimeUnit.SECONDS);

    assertThat(threadName.get())
            .isEqualTo("release");
}
 
Example 14
Source File: MonoTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void monoFromDirectCallableFluxCallsAssemblyHook() {
	final Flux<Integer> source = Flux.just(1);

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Mono.fromDirect(source);
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 15
Source File: DefaultUserRepository.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
@Override
public Mono<User> findMostPopular() {
    return Mono.fromDirect(mongoOperations
            .aggregate(Aggregation.newAggregation(
                    Aggregation.unwind("mentions"),
                    Aggregation.group("mentions.userId")
                            .addToSet("mentions.screenName").as("name")
                            .addToSet("mentions.screenName").as("displayName")
                            .count().as("popularity"),
                    Aggregation.sort(new Sort(new Sort.Order(Sort.Direction.DESC, "popularity"))),
                    Aggregation.limit(1),
                    Aggregation.unwind("name"),
                    Aggregation.unwind("displayName")
            ), Message.class, User.class));
}
 
Example 16
Source File: SimpleFifoPoolTest.java    From reactor-pool with Apache License 2.0 4 votes vote down vote up
void consistentThreadDeliveringWhenNoElementsAndFullAndRaceDrain(int i) throws InterruptedException {
    Scheduler deliveryScheduler = Schedulers.newSingle("delivery");
    AtomicReference<String> threadName = new AtomicReference<>();
    AtomicInteger newCount = new AtomicInteger();

    Scheduler acquire1Scheduler = Schedulers.newSingle("acquire1");
    Scheduler racerReleaseScheduler = Schedulers.fromExecutorService(
            Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"racerRelease"))));
    Scheduler racerAcquireScheduler = Schedulers.newSingle("racerAcquire");

    PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1,
            Mono.fromCallable(() -> new PoolableTest(newCount.getAndIncrement()))
                .subscribeOn(Schedulers.newParallel("poolable test allocator")),
            deliveryScheduler);
    SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig);

    //the pool is started with one elements, and has capacity for 1.
    //we actually first acquire that element so that next acquire will wait for a release
    PooledRef<PoolableTest> uniqueSlot = pool.acquire().block();
    assertThat(uniqueSlot).isNotNull();

    //we prepare next acquire
    Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just));
    CountDownLatch latch = new CountDownLatch(1);

    //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered
    acquire1Scheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName())
            , e -> latch.countDown(), latch::countDown));

    //in parallel, we'll both attempt a second acquire AND release the unique element (each on their dedicated threads
    Mono<PooledRef<PoolableTest>> otherBorrower = pool.acquire();
    racerAcquireScheduler.schedule(() -> otherBorrower.subscribe().dispose(), 100, TimeUnit.MILLISECONDS);
    racerReleaseScheduler.schedule(uniqueSlot.release()::block, 100, TimeUnit.MILLISECONDS);
    latch.await(1, TimeUnit.SECONDS);

    //we expect that, consistently, the poolable is delivered on a `delivery` thread
    assertThat(threadName.get()).as("round #" + i).startsWith("delivery-");

    //we expect that only 1 element was created
    assertThat(newCount).as("elements created in round " + i).hasValue(1);
}
 
Example 17
Source File: SimpleFifoPoolTest.java    From reactor-pool with Apache License 2.0 4 votes vote down vote up
void defaultThreadDeliveringWhenNoElementsAndFullAndRaceDrain(int round, AtomicInteger releaserWins, AtomicInteger borrowerWins) throws InterruptedException {
    AtomicReference<String> threadName = new AtomicReference<>();
    AtomicInteger newCount = new AtomicInteger();
    Scheduler acquire1Scheduler = Schedulers.newSingle("acquire1");
    Scheduler racerReleaseScheduler = Schedulers.fromExecutorService(
            Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"racerRelease"))));
    Scheduler racerAcquireScheduler = Schedulers.fromExecutorService(
            Executors.newSingleThreadScheduledExecutor((r -> new Thread(r,"racerAcquire"))));
    Scheduler allocatorScheduler = Schedulers.newParallel("poolable test allocator");

    try {
        PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1,
                Mono.fromCallable(() -> new PoolableTest(newCount.getAndIncrement()))
                    .subscribeOn(allocatorScheduler));

        SimpleFifoPool<PoolableTest> pool = new SimpleFifoPool<>(testConfig);

        //the pool is started with one elements, and has capacity for 1.
        //we actually first acquire that element so that next acquire will wait for a release
        PooledRef<PoolableTest> uniqueSlot = pool.acquire().block();
        assertThat(uniqueSlot).isNotNull();

        //we prepare next acquire
        Mono<PoolableTest> borrower = Mono.fromDirect(pool.withPoolable(Mono::just));
        CountDownLatch latch = new CountDownLatch(3);

        //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered
        acquire1Scheduler.schedule(() -> borrower.subscribe(v -> threadName.set(Thread.currentThread().getName())
                , e -> latch.countDown(), latch::countDown));

        //in parallel, we'll both attempt concurrent acquire AND release the unique element (each on their dedicated threads)
        racerAcquireScheduler.schedule(() -> {
            pool.acquire().block();
            latch.countDown();
        }, 100, TimeUnit.MILLISECONDS);
        racerReleaseScheduler.schedule(() -> {
            uniqueSlot.release().block();
            latch.countDown();
        }, 100, TimeUnit.MILLISECONDS);

        assertThat(latch.await(1, TimeUnit.SECONDS)).as("1s").isTrue();

        assertThat(newCount).as("created 1 poolable in round " + round).hasValue(1);

        //we expect that sometimes the race will let the second borrower thread drain, which would mean first borrower
        //will get the element delivered from racerAcquire thread. Yet the rest of the time it would get drained by racerRelease.
        if (threadName.get().startsWith("racerRelease")) releaserWins.incrementAndGet();
        else if (threadName.get().startsWith("racerAcquire")) borrowerWins.incrementAndGet();
        else System.out.println(threadName.get());
    }
    finally {
        acquire1Scheduler.dispose();
        racerAcquireScheduler.dispose();
        racerReleaseScheduler.dispose();
        allocatorScheduler.dispose();
    }
}
 
Example 18
Source File: DefaultRSocketClient.java    From rsocket-java with Apache License 2.0 4 votes vote down vote up
@Override
public Mono<RSocket> source() {
  return Mono.fromDirect(this);
}
 
Example 19
Source File: SimpleLifoPoolTest.java    From reactor-pool with Apache License 2.0 4 votes vote down vote up
void consistentThreadDeliveringWhenNoElementsAndFullAndRaceDrain(int i) throws InterruptedException {
    Scheduler allocatorScheduler = Schedulers.newParallel("poolable test allocator");
    Scheduler deliveryScheduler = Schedulers.newSingle("delivery");
    Scheduler acquire1Scheduler = Schedulers.newSingle("acquire1");
    Scheduler racerScheduler = Schedulers.fromExecutorService(
            Executors.newFixedThreadPool(2, (r -> new Thread(r,"racer"))));

    try {
        AtomicReference<String> threadName = new AtomicReference<>();
        AtomicInteger newCount = new AtomicInteger();


        PoolConfig<PoolableTest> testConfig = poolableTestConfig(1, 1,
                Mono.fromCallable(() -> new PoolableTest(newCount.getAndIncrement()))
                    .subscribeOn(allocatorScheduler),
                deliveryScheduler);
        SimpleLifoPool<PoolableTest> pool = new SimpleLifoPool<>(testConfig);

        //the pool is started with one elements, and has capacity for 1.
        //we actually first acquire that element so that next acquire will wait for a release
        PooledRef<PoolableTest> uniqueSlot = pool.acquire().block();
        assertThat(uniqueSlot).isNotNull();

        //we prepare next acquire
        Mono<PoolableTest> firstBorrower = Mono.fromDirect(pool.withPoolable(Mono::just));
        Mono<PoolableTest> otherBorrower = Mono.fromDirect(pool.withPoolable(Mono::just));

        CountDownLatch latch = new CountDownLatch(3);

        //we actually perform the acquire from its dedicated thread, capturing the thread on which the element will actually get delivered
        acquire1Scheduler.schedule(() -> firstBorrower.subscribe(v -> threadName.set(Thread.currentThread().getName())
                , e -> latch.countDown(), latch::countDown));

        //in parallel, we'll race a second acquire AND release the unique element (each on their dedicated threads)
        //since LIFO we expect that if the release loses, it will server acquire1
        RaceTestUtils.race(
                () -> otherBorrower.subscribe(v -> threadName.set(Thread.currentThread().getName())
                        , e -> latch.countDown(), latch::countDown),
                () -> {
                    uniqueSlot.release().block();
                    latch.countDown();
                },
                racerScheduler);
        latch.await(1, TimeUnit.SECONDS);

        //we expect that, consistently, the poolable is delivered on a `delivery` thread
        assertThat(threadName.get()).as("round #" + i).startsWith("delivery-");

        //2 elements MIGHT be created if the first acquire wins (since we're in auto-release mode)
        assertThat(newCount.get()).as("1 or 2 elements created in round " + i).isIn(1, 2);
    }
    finally {
        allocatorScheduler.dispose();
        deliveryScheduler.dispose();
        acquire1Scheduler.dispose();
        racerScheduler.dispose();
    }
}
 
Example 20
Source File: MonoOperatorTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Override
protected Mono<I> withFluxSource(Flux<I> input) {
	return Mono.fromDirect(input);
}