public Observable<Artist> search(String searchedName, String searchedNationality, int maxResults) { return getSavedArtists() // <1> .filter(name -> name.contains(searchedName)) // <2> .flatMap(this::lookupArtist) // <3> .filter(artist -> artist.getNationality() // <4> .contains(searchedNationality)) .take(maxResults); // <5> } // END search
@Test public void noEvents() throws InterruptedException { CountDownLatch commandLatch = new CountDownLatch(1); CountDownLatch threadPoolLatch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> commandSubscriber = getLatchedSubscriber(commandLatch); readCommandStream.observe().take(1).subscribe(commandSubscriber); Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch); readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber); //no writes assertFalse(commandLatch.await(1000, TimeUnit.MILLISECONDS)); assertFalse(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS)); }
@Test public void noEvents() throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> subscriber = getLatchedSubscriber(latch); commandStream.observe().take(1).subscribe(subscriber); //no writes assertFalse(latch.await(1000, TimeUnit.MILLISECONDS)); }
@Test public void testSemaphoreIsolatedSuccess() throws Exception { CountDownLatch commandLatch = new CountDownLatch(1); CountDownLatch threadPoolLatch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> commandSubscriber = getLatchedSubscriber(commandLatch); readCommandStream.observe().take(1).subscribe(commandSubscriber); Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch); readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber); ExecutionResult result = ExecutionResult.from(HystrixEventType.SUCCESS); writeToStream.executionDone(result, commandKey, threadPoolKey); assertTrue(commandLatch.await(1000, TimeUnit.MILLISECONDS)); assertFalse(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS)); }
@Test public void testSemaphoreIsolatedTimeout() throws Exception { CountDownLatch commandLatch = new CountDownLatch(1); CountDownLatch threadPoolLatch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> commandSubscriber = getLatchedSubscriber(commandLatch); readCommandStream.observe().take(1).subscribe(commandSubscriber); Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch); readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber); ExecutionResult result = ExecutionResult.from(HystrixEventType.TIMEOUT); writeToStream.executionDone(result, commandKey, threadPoolKey); assertTrue(commandLatch.await(1000, TimeUnit.MILLISECONDS)); assertFalse(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS)); }
@Test public void testSemaphoreRejectedCommand() throws Exception { CountDownLatch commandLatch = new CountDownLatch(1); CountDownLatch threadPoolLatch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> commandSubscriber = getLatchedSubscriber(commandLatch); readCommandStream.observe().take(1).subscribe(commandSubscriber); Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch); readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber); ExecutionResult result = ExecutionResult.from(HystrixEventType.SEMAPHORE_REJECTED); writeToStream.executionDone(result, commandKey, threadPoolKey); assertTrue(commandLatch.await(1000, TimeUnit.MILLISECONDS)); assertFalse(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS)); }
@Test public void testThreadRejectedCommand() throws Exception { CountDownLatch commandLatch = new CountDownLatch(1); CountDownLatch threadPoolLatch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> commandSubscriber = getLatchedSubscriber(commandLatch); readCommandStream.observe().take(1).subscribe(commandSubscriber); Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch); readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber); ExecutionResult result = ExecutionResult.from(HystrixEventType.THREAD_POOL_REJECTED); writeToStream.executionDone(result, commandKey, threadPoolKey); assertTrue(commandLatch.await(1000, TimeUnit.MILLISECONDS)); assertTrue(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS)); }
@Test public void testSemaphoreIsolatedFailure() throws Exception { CountDownLatch commandLatch = new CountDownLatch(1); CountDownLatch threadPoolLatch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> commandSubscriber = getLatchedSubscriber(commandLatch); readCommandStream.observe().take(1).subscribe(commandSubscriber); Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch); readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber); ExecutionResult result = ExecutionResult.from(HystrixEventType.FAILURE); writeToStream.executionDone(result, commandKey, threadPoolKey); assertTrue(commandLatch.await(1000, TimeUnit.MILLISECONDS)); assertFalse(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS)); }
@Test public void testSemaphoreIsolatedBadRequest() throws Exception { CountDownLatch commandLatch = new CountDownLatch(1); CountDownLatch threadPoolLatch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> commandSubscriber = getLatchedSubscriber(commandLatch); readCommandStream.observe().take(1).subscribe(commandSubscriber); Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch); readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber); ExecutionResult result = ExecutionResult.from(HystrixEventType.BAD_REQUEST); writeToStream.executionDone(result, commandKey, threadPoolKey); assertTrue(commandLatch.await(1000, TimeUnit.MILLISECONDS)); assertFalse(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS)); }
@Test public void testThreadIsolatedSuccess() throws InterruptedException { CountDownLatch commandLatch = new CountDownLatch(1); CountDownLatch threadPoolLatch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> commandSubscriber = getLatchedSubscriber(commandLatch); readCommandStream.observe().take(1).subscribe(commandSubscriber); Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch); readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber); ExecutionResult result = ExecutionResult.from(HystrixEventType.SUCCESS).setExecutedInThread(); writeToStream.executionDone(result, commandKey, threadPoolKey); assertTrue(commandLatch.await(1000, TimeUnit.MILLISECONDS)); assertTrue(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS)); }
@Test public void testShortCircuit() throws Exception { CountDownLatch commandLatch = new CountDownLatch(1); CountDownLatch threadPoolLatch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> commandSubscriber = getLatchedSubscriber(commandLatch); readCommandStream.observe().take(1).subscribe(commandSubscriber); Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch); readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber); ExecutionResult result = ExecutionResult.from(HystrixEventType.SHORT_CIRCUITED); writeToStream.executionDone(result, commandKey, threadPoolKey); assertTrue(commandLatch.await(1000, TimeUnit.MILLISECONDS)); assertFalse(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS)); } }
@Test public void testThreadIsolatedFailure() throws Exception { CountDownLatch commandLatch = new CountDownLatch(1); CountDownLatch threadPoolLatch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> commandSubscriber = getLatchedSubscriber(commandLatch); readCommandStream.observe().take(1).subscribe(commandSubscriber); Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch); readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber); ExecutionResult result = ExecutionResult.from(HystrixEventType.FAILURE).setExecutedInThread(); writeToStream.executionDone(result, commandKey, threadPoolKey); assertTrue(commandLatch.await(1000, TimeUnit.MILLISECONDS)); assertTrue(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS)); }
@Test public void testThreadIsolatedTimeout() throws Exception { CountDownLatch commandLatch = new CountDownLatch(1); CountDownLatch threadPoolLatch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> commandSubscriber = getLatchedSubscriber(commandLatch); readCommandStream.observe().take(1).subscribe(commandSubscriber); Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch); readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber); ExecutionResult result = ExecutionResult.from(HystrixEventType.TIMEOUT).setExecutedInThread(); writeToStream.executionDone(result, commandKey, threadPoolKey); assertTrue(commandLatch.await(1000, TimeUnit.MILLISECONDS)); assertTrue(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS)); }
@Test public void testThreadIsolatedBadRequest() throws Exception { CountDownLatch commandLatch = new CountDownLatch(1); CountDownLatch threadPoolLatch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> commandSubscriber = getLatchedSubscriber(commandLatch); readCommandStream.observe().take(1).subscribe(commandSubscriber); Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch); readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber); ExecutionResult result = ExecutionResult.from(HystrixEventType.BAD_REQUEST).setExecutedInThread(); writeToStream.executionDone(result, commandKey, threadPoolKey); assertTrue(commandLatch.await(1000, TimeUnit.MILLISECONDS)); assertTrue(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS)); }
@Test public void testSingleWriteMultipleSubscribers() throws InterruptedException { CountDownLatch latch1 = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> subscriber1 = getLatchedSubscriber(latch1); CountDownLatch latch2 = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> subscriber2 = getLatchedSubscriber(latch2); commandStream.observe().take(1).subscribe(subscriber1); commandStream.observe().take(1).subscribe(subscriber2); ExecutionResult result = ExecutionResult.from(HystrixEventType.SUCCESS).setExecutedInThread(); HystrixCommandCompletion event = HystrixCommandCompletion.from(result, commandKey, threadPoolKey); commandStream.write(event); assertTrue(latch1.await(1000, TimeUnit.MILLISECONDS)); assertTrue(latch2.await(10, TimeUnit.MILLISECONDS)); } }
@Test public void testSingleWriteSingleSubscriber() throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); Subscriber<HystrixCommandCompletion> subscriber = getLatchedSubscriber(latch); commandStream.observe().take(1).subscribe(subscriber); ExecutionResult result = ExecutionResult.from(HystrixEventType.SUCCESS).setExecutedInThread(); HystrixCommandCompletion event = HystrixCommandCompletion.from(result, commandKey, threadPoolKey); commandStream.write(event); assertTrue(latch.await(1000, TimeUnit.MILLISECONDS)); }
@Test public void testEmptyStreamProducesZeros() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Concurrency-A"); stream = RollingCommandMaxConcurrencyStream.getInstance(key, 10, 500); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(5).subscribe(getSubscriber(latch)); //no writes try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(0, stream.getLatestRollingMax()); }
@Test public void testStartsAndEndsInSameBucketProduceValue() throws InterruptedException { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Concurrency-B"); stream = RollingCommandMaxConcurrencyStream.getInstance(key, 10, 500); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(5).subscribe(getSubscriber(latch)); Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 100); Command cmd2 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 100); cmd1.observe(); Thread.sleep(1); cmd2.observe(); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); assertEquals(2, stream.getLatestRollingMax()); }
@Test public void testStartsAndEndsInSameBucketProduceValue() throws InterruptedException { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-Concurrency-B"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-Concurrency-B"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingConcurrency-B"); stream = RollingThreadPoolMaxConcurrencyStream.getInstance(threadPoolKey, 10, 100); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(10).subscribe(getSubscriber(latch)); Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 50); Command cmd2 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 40); cmd1.observe(); Thread.sleep(1); cmd2.observe(); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); assertEquals(2, stream.getLatestRollingMax()); }
@Test public void testSingleFailure() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Health-C"); stream = HealthCountsStream.getInstance(key, 10, 100); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(10).subscribe(getSubscriber(latch)); CommandStreamTest.Command cmd = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.FAILURE, 20); cmd.observe(); try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertEquals(1L, stream.getLatest().getErrorCount()); assertEquals(1L, stream.getLatest().getTotalRequests()); }