/* * @see DATAREDIS-415 */ @Test public void interruptAtStart() throws Exception { final Thread main = Thread.currentThread(); // interrupt thread once Executor.execute is called doAnswer( new Answer<Object>() { @Override public Object answer(final InvocationOnMock invocationOnMock) throws Throwable { main.interrupt(); return null; } }) .when(executorMock) .execute(any(Runnable.class)); container.addMessageListener(adapter, new ChannelTopic("a")); container.start(); // reset the interrupted flag to not destroy the teardown assertThat(Thread.interrupted(), is(true)); assertThat(container.isRunning(), is(false)); }
@Override public Subscription onSubscribe(final Observer<? super String> observer) { System.out.println("TestObservable subscribed to ..."); t = new Thread( new Runnable() { @Override public void run() { try { System.out.println("running TestObservable thread"); for (String s : values) { if ("fail".equals(s)) throw new RuntimeException("Forced Failure"); System.out.println("TestObservable onNext: " + s); observer.onNext(s); } System.out.println("TestObservable onCompleted"); observer.onCompleted(); } catch (Throwable e) { System.out.println("TestObservable onError: " + e); observer.onError(e); } } }); System.out.println("starting TestObservable thread"); t.start(); System.out.println("done starting TestObservable thread"); return s; }
@Override public Subscription subscribe(final Observer<String> observer) { System.out.println("TestObservable subscribed to ..."); t = new Thread( new Runnable() { @Override public void run() { try { System.out.println("running TestObservable thread"); for (String s : values) { System.out.println("TestObservable onNext: " + s); observer.onNext(s); } observer.onCompleted(); } catch (Exception e) { throw new RuntimeException(e); } } }); System.out.println("starting TestObservable thread"); t.start(); System.out.println("done starting TestObservable thread"); return s; }
@Override public void subscribe(final Subscriber<? super String> observer) { observer.onSubscribe(EmptySubscription.INSTANCE); t = new Thread( new Runnable() { @Override public void run() { for (String s : valuesToReturn) { if (s == null) { System.out.println("throwing exception"); try { Thread.sleep(100); } catch (Throwable e) { } observer.onError(new NullPointerException()); return; } else { observer.onNext(s); } } System.out.println("subscription complete"); observer.onComplete(); } }); t.start(); }
@Test public void testCreateLDAPConnectionFactoryWithCustomClassLoader() throws Exception { // test no exception is thrown, which means transport provider is correctly loaded LDAPOptions options = new LDAPOptions().setProviderClassLoader(Thread.currentThread().getContextClassLoader()); InetSocketAddress socketAddress = findFreeSocketAddress(); LDAPConnectionFactory factory = new LDAPConnectionFactory(socketAddress.getHostName(), socketAddress.getPort(), options); factory.close(); }
@Override public void subscribe(final Subscriber<? super String> observer) { observer.onSubscribe(EmptySubscription.INSTANCE); t = new Thread( new Runnable() { @Override public void run() { observer.onNext("hello"); observer.onComplete(); } }); t.start(); }
@Override public void subscribe(final Subscriber<? super String> observer) { observer.onSubscribe(EmptySubscription.INSTANCE); t = new Thread( new Runnable() { @Override public void run() { try { Thread.sleep(100); } catch (InterruptedException e) { observer.onError(e); } observer.onNext("hello"); observer.onComplete(); } }); t.start(); }
@Override public Subscription subscribe(final Observer<String> observer) { t = new Thread( new Runnable() { @Override public void run() { onNextBeingSent.countDown(); observer.onNext("hello"); // I can't use a countDownLatch to prove we are actually sending 'onNext' // since it will block if synchronized and I'll deadlock observer.onCompleted(); } }); t.start(); return new Subscription() { @Override public void unsubscribe() {} }; }
@Test public void testSynchronizationOfMultipleSequences() throws Exception { final TestASynchronousObservable o1 = new TestASynchronousObservable(); final TestASynchronousObservable o2 = new TestASynchronousObservable(); // use this latch to cause onNext to wait until we're ready to let it go final CountDownLatch endLatch = new CountDownLatch(1); final AtomicInteger concurrentCounter = new AtomicInteger(); final AtomicInteger totalCounter = new AtomicInteger(); @SuppressWarnings("unchecked") Observable<String> m = Observable.create(merge(o1, o2)); m.subscribe( new Observer<String>() { @Override public void onCompleted() {} @Override public void onError(Exception e) { throw new RuntimeException("failed", e); } @Override public void onNext(String v) { totalCounter.incrementAndGet(); concurrentCounter.incrementAndGet(); try { // wait here until we're done asserting endLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); throw new RuntimeException("failed", e); } finally { concurrentCounter.decrementAndGet(); } } }); // wait for both observables to send (one should be blocked) o1.onNextBeingSent.await(); o2.onNextBeingSent.await(); // I can't think of a way to know for sure that both threads have or are trying to send onNext // since I can't use a CountDownLatch for "after" onNext since I want to catch during it // but I can't know for sure onNext is invoked // so I'm unfortunately reverting to using a Thread.sleep to allow the process scheduler time // to make sure after o1.onNextBeingSent and o2.onNextBeingSent are hit that the following // onNext is invoked. Thread.sleep(300); try { // in try/finally so threads are released via latch countDown even if assertion fails assertEquals(1, concurrentCounter.get()); } finally { // release so it can finish endLatch.countDown(); } try { o1.t.join(); o2.t.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } assertEquals(2, totalCounter.get()); assertEquals(0, concurrentCounter.get()); }