@Test
public void testMultipleBucketsBothGetStored() {

    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-H");
    stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    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() == 2) {
                assertBetween(55, 90, (int) distribution.getMean());
            }
            if (distribution.getTotalCount() == 5) {
                assertEquals(60, 90, (long) distribution.getMean());
            }
        }
    });
    Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 10);
    Command cmd2 = Command.from(groupKey, key, HystrixEventType.FAILURE, 100);
    cmd1.observe();
    cmd2.observe();
    try {
        Thread.sleep(500);
    } catch (InterruptedException ie) {
        fail("Interrupted ex");
    }
    Command cmd3 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 60);
    Command cmd4 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 60);
    Command cmd5 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 70);
    cmd3.observe();
    cmd4.observe();
    cmd5.observe();
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertBetween(55, 90, stream.getLatestMean());
    assertBetween(10, 50, stream.getLatestPercentile(0.0));
    assertBetween(100, 150, stream.getLatestPercentile(100.0));
}