/** * Tests that an exception is thrown if the timestamps overflow and the method * terminate before 5 seconds. */ @Test public void TimestampOverflow() { Callable<Resource> task = () -> { TestPool pool = new TestPool(DynamicResourcePool.Options.defaultOptions().setMaxCapacity(1)); pool.acquire(Long.MAX_VALUE, TimeUnit.MILLISECONDS); return pool.acquire(Long.MAX_VALUE, TimeUnit.MILLISECONDS); }; ExecutorService executor = Executors.newFixedThreadPool(1); Future<Resource> future = executor.submit(task); boolean timeout = false; try { future.get(5000, TimeUnit.MILLISECONDS); } catch (Exception ex) { timeout = true; } Assert.assertTrue(timeout); Assert.assertFalse(future.isDone()); future.cancel(true); } }
/** * Acquire without capacity. */ @Test public void acquireWithoutCapacity() throws Exception { TestPool pool = new TestPool(DynamicResourcePool.Options.defaultOptions().setMaxCapacity(1)); List<Resource> resourceList = new ArrayList<>(); boolean timeout = false; try { Resource resource = pool.acquire(); resourceList.add(resource); Assert.assertEquals(0, resource.mInteger.intValue()); resource = pool.acquire(1, TimeUnit.SECONDS); resourceList.add(resource); } catch (TimeoutException e) { timeout = true; } Assert.assertEquals(1, resourceList.size()); Assert.assertTrue(timeout); }
@Test public void multiClients() throws Exception { TestPool pool = new TestPool(DynamicResourcePool.Options.defaultOptions().setMaxCapacity(1)); final Resource resource1 = pool.acquire(); Assert.assertEquals(0, resource1.mInteger.intValue()); class ReleaseThread extends Thread { private TestPool mPool; private Resource mResource; ReleaseThread(TestPool pool, Resource resource) { mPool = pool; mResource = resource; } @Override public void run() { try { // Sleep sometime to test wait logic. Thread.sleep(1000); } catch (InterruptedException e) { return; } mPool.release(mResource); } } ReleaseThread releaseThread = new ReleaseThread(pool, resource1); releaseThread.start(); Resource resource2 = pool.acquire(2, TimeUnit.SECONDS); Assert.assertEquals(0, resource2.mInteger.intValue()); }
/** * Creates a netty channel pool instance with a minimum capacity of 1. * * @param bootstrap the netty bootstrap used to create netty channel * @param maxCapacity the maximum capacity of the pool * @param gcThresholdMs when a channel is older than this threshold and the pool's capacity * is above the minimum capacity(1), it is closed and removed from the pool. */ public NettyChannelPool(Bootstrap bootstrap, int maxCapacity, long gcThresholdMs) { super(Options.defaultOptions().setMaxCapacity(maxCapacity).setGcExecutor(GC_EXECUTOR)); mBootstrap = bootstrap; mGcThresholdMs = gcThresholdMs; }
/** * Creates a new block master client pool. * * @param subject the parent subject * @param address address of the worker * @param maxCapacity the maximum capacity of the pool * @param alluxioConf Alluxio configuration * is above the minimum capacity(1), it is closed and removed from the pool. */ public BlockWorkerClientPool(Subject subject, SocketAddress address, int maxCapacity, AlluxioConfiguration alluxioConf) { super(Options.defaultOptions().setMaxCapacity(maxCapacity).setGcExecutor(GC_EXECUTOR)); mSubject = subject; mAddress = address; mConf = alluxioConf; }