Search in sources :

Example 11 with TearDown

use of com.google.common.testing.TearDown in project commons by twitter.

the class BaseZooKeeperTest method setUp.

@Before
public final void setUp() throws Exception {
    final ShutdownRegistryImpl shutdownRegistry = new ShutdownRegistryImpl();
    addTearDown(new TearDown() {

        @Override
        public void tearDown() {
            shutdownRegistry.execute();
        }
    });
    zkTestServer = new ZooKeeperTestServer(0, shutdownRegistry, defaultSessionTimeout);
    zkTestServer.startNetwork();
}
Also used : TearDown(com.google.common.testing.TearDown) ShutdownRegistryImpl(com.twitter.common.application.ShutdownRegistry.ShutdownRegistryImpl) Before(org.junit.Before)

Example 12 with TearDown

use of com.google.common.testing.TearDown in project commons by twitter.

the class ThriftFactoryTest method testCreate.

@Test
public void testCreate() throws Exception {
    final AtomicReference<Socket> clientConnection = new AtomicReference<Socket>();
    final CountDownLatch connected = new CountDownLatch(1);
    final ServerSocket server = new ServerSocket(0);
    Thread service = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                clientConnection.set(server.accept());
            } catch (IOException e) {
                LOG.log(Level.WARNING, "Problem accepting a connection to thrift server", e);
            } finally {
                connected.countDown();
            }
        }
    });
    service.setDaemon(true);
    service.start();
    try {
        final Thrift<GoodService.Iface> thrift = ThriftFactory.create(GoodService.Iface.class).withMaxConnectionsPerEndpoint(1).build(ImmutableSet.of(new InetSocketAddress(server.getLocalPort())));
        addTearDown(new TearDown() {

            @Override
            public void tearDown() {
                thrift.close();
            }
        });
        GoodService.Iface client = thrift.create();
        assertEquals(GoodService.DONE, client.doWork());
    } finally {
        connected.await();
        server.close();
    }
    Socket socket = clientConnection.get();
    assertNotNull(socket);
    socket.close();
}
Also used : TearDown(com.google.common.testing.TearDown) InetSocketAddress(java.net.InetSocketAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncIface(com.twitter.common.thrift.ThriftFactoryTest.GoodService.AsyncIface) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 13 with TearDown

use of com.google.common.testing.TearDown in project commons by twitter.

the class ThriftFactoryTest method testCreateEmpty.

@Test(expected = TResourceExhaustedException.class)
public void testCreateEmpty() throws Exception {
    @SuppressWarnings("unchecked") DynamicHostSet<ServiceInstance> emptyHostSet = control.createMock(DynamicHostSet.class);
    final Thrift<GoodService.Iface> thrift = ThriftFactory.create(GoodService.Iface.class).withMaxConnectionsPerEndpoint(1).build(emptyHostSet);
    addTearDown(new TearDown() {

        @Override
        public void tearDown() {
            thrift.close();
        }
    });
    GoodService.Iface client = thrift.create();
    // This should throw a TResourceExhaustedException
    client.doWork();
}
Also used : TearDown(com.google.common.testing.TearDown) AsyncIface(com.twitter.common.thrift.ThriftFactoryTest.GoodService.AsyncIface) ServiceInstance(com.twitter.thrift.ServiceInstance) Test(org.junit.Test)

Example 14 with TearDown

use of com.google.common.testing.TearDown 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 15 with TearDown

use of com.google.common.testing.TearDown in project commons by twitter.

the class ExceptionHandlingExecutorServiceTest method setUp.

@Before
public void setUp() throws Exception {
    signallingHandler = createMock(Thread.UncaughtExceptionHandler.class);
    executorService = MoreExecutors.exceptionHandlingExecutor(Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("ExceptionHandlingExecutorServiceTest-%d").build()), signallingHandler);
    final ExecutorServiceShutdown executorServiceShutdown = new ExecutorServiceShutdown(executorService, Amount.of(3L, Time.SECONDS));
    addTearDown(new TearDown() {

        @Override
        public void tearDown() throws Exception {
            executorServiceShutdown.execute();
        }
    });
}
Also used : TearDown(com.google.common.testing.TearDown) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ExecutionException(java.util.concurrent.ExecutionException) Before(org.junit.Before)

Aggregations

TearDown (com.google.common.testing.TearDown)16 Before (org.junit.Before)6 AsyncIface (com.twitter.common.thrift.ThriftFactoryTest.GoodService.AsyncIface)3 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 InetSocketAddress (java.net.InetSocketAddress)2 ExecutionException (java.util.concurrent.ExecutionException)2 AsyncMethodCallback (org.apache.thrift.async.AsyncMethodCallback)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ShutdownRegistryImpl (com.twitter.common.application.ShutdownRegistry.ShutdownRegistryImpl)1 ExceptionalCommand (com.twitter.common.base.ExceptionalCommand)1 Temporary (com.twitter.common.io.FileUtils.Temporary)1 Amount (com.twitter.common.quantity.Amount)1 TTimeoutException (com.twitter.common.thrift.TTimeoutException)1 JoinException (com.twitter.common.zookeeper.Group.JoinException)1 Partition (com.twitter.common.zookeeper.Partitioner.Partition)1 EndpointStatus (com.twitter.common.zookeeper.ServerSet.EndpointStatus)1 LeadershipListener (com.twitter.common.zookeeper.SingletonService.LeadershipListener)1 ServiceInstance (com.twitter.thrift.ServiceInstance)1