use of org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest in project rxjava2-jdbc by davidmoten.
the class NonBlockingPoolTest method testMaxIdleTime.
@Test
public void testMaxIdleTime() throws InterruptedException {
TestScheduler s = new TestScheduler();
AtomicInteger count = new AtomicInteger();
AtomicInteger disposed = new AtomicInteger();
Pool<Integer> pool = //
NonBlockingPool.factory(//
() -> count.incrementAndGet()).healthCheck(//
n -> true).maxSize(//
3).maxIdleTime(1, //
TimeUnit.MINUTES).disposer(//
n -> disposed.incrementAndGet()).scheduler(//
s).build();
TestSubscriber<Member<Integer>> ts = new //
FlowableSingleDeferUntilRequest<>(//
pool.member()).doOnNext(//
m -> m.checkin()).doOnNext(//
System.out::println).doOnRequest(//
t -> System.out.println("test request=" + t)).test(1);
s.triggerActions();
ts.assertValueCount(1);
assertEquals(0, disposed.get());
s.advanceTimeBy(1, TimeUnit.MINUTES);
s.triggerActions();
assertEquals(1, disposed.get());
}
use of org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest in project rxjava2-jdbc by davidmoten.
the class NonBlockingPoolTest method testReleasedMemberIsRecreated.
@Test
public void testReleasedMemberIsRecreated() throws Exception {
TestScheduler s = new TestScheduler();
AtomicInteger count = new AtomicInteger();
AtomicInteger disposed = new AtomicInteger();
Pool<Integer> pool = //
NonBlockingPool.factory(//
() -> count.incrementAndGet()).healthCheck(//
n -> true).maxSize(//
1).maxIdleTime(1, //
TimeUnit.MINUTES).disposer(//
n -> disposed.incrementAndGet()).scheduler(//
s).build();
{
TestSubscriber<Member<Integer>> ts = new FlowableSingleDeferUntilRequest<>(//
pool.member()).doOnNext(//
m -> m.checkin()).doOnNext(//
System.out::println).doOnRequest(//
t -> System.out.println("test request=" + t)).test(1);
s.triggerActions();
ts.assertValueCount(1);
assertEquals(0, disposed.get());
s.advanceTimeBy(1, TimeUnit.MINUTES);
s.triggerActions();
assertEquals(1, disposed.get());
ts.cancel();
assertEquals(1, disposed.get());
}
{
TestSubscriber<Member<Integer>> ts = //
pool.member().repeat().doOnNext(//
m -> m.checkin()).doOnNext(//
System.out::println).doOnRequest(//
t -> System.out.println("test request=" + t)).test(1);
s.triggerActions();
ts.assertValueCount(1);
assertEquals(1, disposed.get());
s.advanceTimeBy(1, TimeUnit.MINUTES);
s.triggerActions();
assertEquals(2, disposed.get());
}
// check Pool.close() disposes value
{
TestSubscriber<Member<Integer>> ts = //
pool.member().repeat().doOnNext(//
m -> m.checkin()).doOnNext(//
System.out::println).doOnRequest(//
t -> System.out.println("test request=" + t)).test(1);
s.triggerActions();
ts.assertValueCount(1);
assertEquals(2, disposed.get());
}
pool.close();
assertEquals(3, disposed.get());
}
use of org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest in project rxjava2-jdbc by davidmoten.
the class NonBlockingPoolTest method testSubscribeWhenPoolClosedEmitsError.
@Test
public void testSubscribeWhenPoolClosedEmitsError() throws Exception {
TestScheduler s = new TestScheduler();
AtomicInteger count = new AtomicInteger();
AtomicInteger disposed = new AtomicInteger();
Pool<Integer> pool = //
NonBlockingPool.factory(//
() -> count.incrementAndGet()).healthCheck(//
n -> true).maxSize(//
3).maxIdleTime(1, //
TimeUnit.MINUTES).disposer(//
n -> disposed.incrementAndGet()).scheduler(//
s).build();
pool.close();
new //
FlowableSingleDeferUntilRequest<>(//
pool.member()).test(//
1).assertError(//
PoolClosedException.class).assertNoValues();
}
use of org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest in project rxjava2-jdbc by davidmoten.
the class NonBlockingPoolTest method testHealthCheckWhenFails.
@Test
public void testHealthCheckWhenFails() throws Exception {
TestScheduler s = new TestScheduler();
AtomicInteger count = new AtomicInteger();
AtomicInteger disposed = new AtomicInteger();
AtomicInteger healthChecks = new AtomicInteger();
Pool<Integer> pool = //
NonBlockingPool.factory(//
() -> count.incrementAndGet()).healthCheck(n -> {
healthChecks.incrementAndGet();
return false;
}).createRetryInterval(10, //
TimeUnit.MINUTES).idleTimeBeforeHealthCheck(1, //
TimeUnit.MILLISECONDS).maxSize(//
1).maxIdleTime(1, //
TimeUnit.HOURS).disposer(//
n -> disposed.incrementAndGet()).scheduler(//
s).build();
{
TestSubscriber<Member<Integer>> ts = //
new FlowableSingleDeferUntilRequest<>(pool.member()).repeat().doOnNext(//
System.out::println).doOnNext(//
m -> m.checkin()).doOnRequest(//
t -> System.out.println("test request=" + t)).test(1);
s.triggerActions();
// health check doesn't get run on create
ts.assertValueCount(1);
assertEquals(0, disposed.get());
assertEquals(0, healthChecks.get());
// next request is immediate so health check does not run
System.out.println("health check should not run because immediate");
ts.request(1);
s.triggerActions();
ts.assertValueCount(2);
assertEquals(0, disposed.get());
assertEquals(0, healthChecks.get());
// now try to trigger health check
s.advanceTimeBy(1, TimeUnit.MILLISECONDS);
s.triggerActions();
System.out.println("trying to trigger health check");
ts.request(1);
s.triggerActions();
ts.assertValueCount(2);
assertEquals(1, disposed.get());
assertEquals(1, healthChecks.get());
// checkout retry should happen after interval
s.advanceTimeBy(10, TimeUnit.MINUTES);
ts.assertValueCount(3);
// failing health check causes recreate to be scheduled
ts.cancel();
// already disposed so cancel has no effect
assertEquals(1, disposed.get());
}
}
use of org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest in project rxjava2-jdbc by davidmoten.
the class NonBlockingPoolTest method testConnectionPoolRecylesAlternating.
@Test
public void testConnectionPoolRecylesAlternating() {
TestScheduler s = new TestScheduler();
AtomicInteger count = new AtomicInteger();
Pool<Integer> pool = //
NonBlockingPool.factory(//
() -> count.incrementAndGet()).healthCheck(//
n -> true).maxSize(//
2).maxIdleTime(1, //
TimeUnit.MINUTES).scheduler(//
s).build();
TestSubscriber<Integer> ts = //
new FlowableSingleDeferUntilRequest<>(pool.member()).repeat().doOnNext(//
m -> m.checkin()).map(//
m -> m.value()).test(//
4);
s.triggerActions();
//
ts.assertValueCount(4).assertNotTerminated();
List<Object> list = ts.getEvents().get(0);
// all 4 connections released were the same
assertTrue(list.get(0) == list.get(1));
assertTrue(list.get(1) == list.get(2));
assertTrue(list.get(2) == list.get(3));
}
Aggregations