@Test
public void testSemaphoreRejectedCommandDoesNotGetLatencyTracked() {

    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-F");
    stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    //10 commands with latency should occupy all semaphores.  execute those, then wait for bucket to roll
    //next command should be a semaphore rejection
    List<Command> commands = new ArrayList<Command>();
    for (int i = 0; i < 10; i++) {
        commands.add(Command.from(groupKey, key, HystrixEventType.SUCCESS, 200, HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE));
    }
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(new Subscriber<CachedValuesHistogram>() {
        @Override
        public void onCompleted() {
            latch.countDown();
        }
        @Override
        public void onError(Throwable e) {
            fail(e.getMessage());
        }
        @Override
        public void onNext(CachedValuesHistogram distribution) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
            if (distribution.getTotalCount() > 0) {
                assertBetween(200, 250, (int) distribution.getMean());
            }
        }
    });
    for (final Command cmd: commands) {
        //since these are blocking calls on the caller thread, we need a new caller thread for each command to actually get the desired concurrency
        new Thread(new HystrixContextRunnable(new Runnable() {
            @Override
            public void run() {
                cmd.observe();
            }
        })).start();
    }
    Command semaphoreRejected = Command.from(groupKey, key, HystrixEventType.SUCCESS);
    try {
        Thread.sleep(40);
        semaphoreRejected.observe();
    } catch (InterruptedException ie) {
        fail(ie.getMessage());
    }
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(10, stream.getLatest().getTotalCount());
    assertBetween(200, 250, stream.getLatestMean());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertTrue(semaphoreRejected.isResponseSemaphoreRejected());
}