Search in sources :

Example 1 with FlowableSingleDeferUntilRequest

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays(java.util.Arrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UndeliverableException(io.reactivex.exceptions.UndeliverableException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Scheduler(io.reactivex.Scheduler) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) TestSubscriber(io.reactivex.subscribers.TestSubscriber) FlowableSingleDeferUntilRequest(org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) RxJavaPlugins(io.reactivex.plugins.RxJavaPlugins) Consumer(io.reactivex.functions.Consumer) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Assert.assertNull(org.junit.Assert.assertNull) SingleObserver(io.reactivex.SingleObserver) Assert.assertFalse(org.junit.Assert.assertFalse) TestScheduler(io.reactivex.schedulers.TestScheduler) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FlowableSingleDeferUntilRequest(org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 2 with FlowableSingleDeferUntilRequest

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays(java.util.Arrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UndeliverableException(io.reactivex.exceptions.UndeliverableException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Scheduler(io.reactivex.Scheduler) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) TestSubscriber(io.reactivex.subscribers.TestSubscriber) FlowableSingleDeferUntilRequest(org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) RxJavaPlugins(io.reactivex.plugins.RxJavaPlugins) Consumer(io.reactivex.functions.Consumer) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Assert.assertNull(org.junit.Assert.assertNull) SingleObserver(io.reactivex.SingleObserver) Assert.assertFalse(org.junit.Assert.assertFalse) TestScheduler(io.reactivex.schedulers.TestScheduler) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(io.reactivex.subscribers.TestSubscriber) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 3 with FlowableSingleDeferUntilRequest

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays(java.util.Arrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UndeliverableException(io.reactivex.exceptions.UndeliverableException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Scheduler(io.reactivex.Scheduler) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) TestSubscriber(io.reactivex.subscribers.TestSubscriber) FlowableSingleDeferUntilRequest(org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) RxJavaPlugins(io.reactivex.plugins.RxJavaPlugins) Consumer(io.reactivex.functions.Consumer) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Assert.assertNull(org.junit.Assert.assertNull) SingleObserver(io.reactivex.SingleObserver) Assert.assertFalse(org.junit.Assert.assertFalse) TestScheduler(io.reactivex.schedulers.TestScheduler) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 4 with FlowableSingleDeferUntilRequest

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());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays(java.util.Arrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UndeliverableException(io.reactivex.exceptions.UndeliverableException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Scheduler(io.reactivex.Scheduler) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) TestSubscriber(io.reactivex.subscribers.TestSubscriber) FlowableSingleDeferUntilRequest(org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) RxJavaPlugins(io.reactivex.plugins.RxJavaPlugins) Consumer(io.reactivex.functions.Consumer) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Assert.assertNull(org.junit.Assert.assertNull) SingleObserver(io.reactivex.SingleObserver) Assert.assertFalse(org.junit.Assert.assertFalse) TestScheduler(io.reactivex.schedulers.TestScheduler) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(io.reactivex.subscribers.TestSubscriber) FlowableSingleDeferUntilRequest(org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 5 with FlowableSingleDeferUntilRequest

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));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays(java.util.Arrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UndeliverableException(io.reactivex.exceptions.UndeliverableException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Scheduler(io.reactivex.Scheduler) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) TestSubscriber(io.reactivex.subscribers.TestSubscriber) FlowableSingleDeferUntilRequest(org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) RxJavaPlugins(io.reactivex.plugins.RxJavaPlugins) Consumer(io.reactivex.functions.Consumer) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Assert.assertNull(org.junit.Assert.assertNull) SingleObserver(io.reactivex.SingleObserver) Assert.assertFalse(org.junit.Assert.assertFalse) TestScheduler(io.reactivex.schedulers.TestScheduler) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FlowableSingleDeferUntilRequest(org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Aggregations

TestScheduler (io.reactivex.schedulers.TestScheduler)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 FlowableSingleDeferUntilRequest (org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest)6 Test (org.junit.Test)6 Flowable (io.reactivex.Flowable)5 Scheduler (io.reactivex.Scheduler)5 SingleObserver (io.reactivex.SingleObserver)5 Disposable (io.reactivex.disposables.Disposable)5 UndeliverableException (io.reactivex.exceptions.UndeliverableException)5 Consumer (io.reactivex.functions.Consumer)5 TestObserver (io.reactivex.observers.TestObserver)5 RxJavaPlugins (io.reactivex.plugins.RxJavaPlugins)5 Schedulers (io.reactivex.schedulers.Schedulers)5 TestSubscriber (io.reactivex.subscribers.TestSubscriber)5 SQLException (java.sql.SQLException)5 Arrays (java.util.Arrays)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Executors (java.util.concurrent.Executors)5