Search in sources :

Example 1 with Amount

use of com.twitter.common.quantity.Amount in project commons by twitter.

the class DeadlineCallerTest method makeDeadline.

private DeadlineCaller makeDeadline(final boolean shouldTimeOut) {
    final CountDownLatch cancelled = new CountDownLatch(1);
    if (shouldTimeOut) {
        addTearDown(new TearDown() {

            @Override
            public void tearDown() throws Exception {
                // This will block forever if cancellation does not occur and interrupt the ~indefinite
                // sleep.
                cancelled.await();
            }
        });
    }
    Caller sleepyCaller = new CallerDecorator(caller, false) {

        @Override
        public Object call(Method method, Object[] args, @Nullable AsyncMethodCallback callback, @Nullable Amount<Long, Time> connectTimeoutOverride) throws Throwable {
            if (shouldTimeOut) {
                try {
                    Thread.sleep(Long.MAX_VALUE);
                    fail("Expected late work to be cancelled and interrupted");
                } catch (InterruptedException e) {
                    cancelled.countDown();
                }
            }
            return caller.call(method, args, callback, connectTimeoutOverride);
        }
    };
    return new DeadlineCaller(sleepyCaller, false, executorService, DEADLINE);
}
Also used : TearDown(com.google.common.testing.TearDown) AsyncMethodCallback(org.apache.thrift.async.AsyncMethodCallback) Amount(com.twitter.common.quantity.Amount) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) TTimeoutException(com.twitter.common.thrift.TTimeoutException) Nullable(javax.annotation.Nullable)

Example 2 with Amount

use of com.twitter.common.quantity.Amount in project commons by twitter.

the class ConnectionPoolTest method testCreating.

@Test
public void testCreating() throws Exception {
    Amount<Long, Time> timeout = Amount.of(1L, Time.SECONDS);
    Executor executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());
    expect(connectionFactory.mightCreate()).andReturn(true);
    Capture<Amount<Long, Time>> timeout1 = new Capture<Amount<Long, Time>>();
    @SuppressWarnings("unchecked") Connection<String, Integer> connection1 = control.createMock(Connection.class);
    expect(connectionFactory.create(capture(timeout1))).andReturn(connection1);
    Capture<Amount<Long, Time>> timeout2 = new Capture<Amount<Long, Time>>();
    @SuppressWarnings("unchecked") Connection<String, Integer> connection2 = control.createMock(Connection.class);
    expect(connectionFactory.create(capture(timeout2))).andReturn(connection2);
    control.replay();
    ConnectionPool<Connection<String, Integer>> connectionPool = createConnectionPool(executor);
    assertSame(connection1, connectionPool.get(timeout));
    assertTrue(timeout1.hasCaptured());
    Long timeout1Millis = timeout1.getValue().as(Time.MILLISECONDS);
    assertTrue(timeout1Millis > 0 && timeout1Millis <= timeout.as(Time.MILLISECONDS));
    assertSame(connection2, connectionPool.get(timeout));
    assertTrue(timeout2.hasCaptured());
    Long timeout2Millis = timeout1.getValue().as(Time.MILLISECONDS);
    assertTrue(timeout2Millis > 0 && timeout2Millis <= timeout.as(Time.MILLISECONDS));
    control.verify();
}
Also used : Amount(com.twitter.common.quantity.Amount) Time(com.twitter.common.quantity.Time) Capture(org.easymock.Capture) Executor(java.util.concurrent.Executor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test)

Example 3 with Amount

use of com.twitter.common.quantity.Amount in project commons by twitter.

the class ApproximateHistogramTest method testMemConstraint.

@Test
public void testMemConstraint() {
    ImmutableList.Builder<Amount<Long, Data>> builder = ImmutableList.builder();
    builder.add(Amount.of(1L, Data.KB));
    builder.add(Amount.of(4L, Data.KB));
    builder.add(Amount.of(8L, Data.KB));
    builder.add(Amount.of(16L, Data.KB));
    builder.add(Amount.of(32L, Data.KB));
    builder.add(Amount.of(64L, Data.KB));
    builder.add(Amount.of(256L, Data.KB));
    builder.add(Amount.of(1L, Data.MB));
    builder.add(Amount.of(16L, Data.MB));
    builder.add(Amount.of(32L, Data.MB));
    List<Amount<Long, Data>> sizes = builder.build();
    for (Amount<Long, Data> maxSize : sizes) {
        ApproximateHistogram hist = new ApproximateHistogram(maxSize);
        for (long i = 0; i < 1000 * 1000; i++) {
            hist.add(i);
        }
        long size = ObjectSizeCalculator.getObjectSize(hist);
        assertTrue(size < maxSize.as(Data.BYTES));
    }
}
Also used : ApproximateHistogram(com.twitter.common.stats.ApproximateHistogram) ImmutableList(com.google.common.collect.ImmutableList) Amount(com.twitter.common.quantity.Amount) Data(com.twitter.common.quantity.Data) Test(org.junit.Test)

Aggregations

Amount (com.twitter.common.quantity.Amount)3 Test (org.junit.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 TearDown (com.google.common.testing.TearDown)1 Data (com.twitter.common.quantity.Data)1 Time (com.twitter.common.quantity.Time)1 ApproximateHistogram (com.twitter.common.stats.ApproximateHistogram)1 TTimeoutException (com.twitter.common.thrift.TTimeoutException)1 Method (java.lang.reflect.Method)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executor (java.util.concurrent.Executor)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 Nullable (javax.annotation.Nullable)1 AsyncMethodCallback (org.apache.thrift.async.AsyncMethodCallback)1 Capture (org.easymock.Capture)1