/** * Tests the logic to acquire a resource when the pool is not full. */ @Test public void acquireWithCapacity() throws Exception { TestPool pool = new TestPool(DynamicResourcePool.Options.defaultOptions()); List<Resource> resourceList = new ArrayList<>(); for (int i = 0; i < 3; i++) { Resource resource = pool.acquire(); resourceList.add(resource); Assert.assertEquals(i, resource.mInteger.intValue()); } for (Resource resource : resourceList) { pool.release(resource); } Set<Integer> resources = new HashSet<>(); for (int i = 0; i < 3; i++) { Resource resource = pool.acquire(); resources.add(resource.mInteger); } // Make sure we are not creating new resources. for (int i = 0; i < 3; i++) { Assert.assertTrue(resources.contains(i)); } }
/** * 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); } }
/** * Tests the logic that the recently used resource is preferred. */ @Test public void acquireRentlyUsed() throws Exception { ManualClock manualClock = new ManualClock(); TestPool pool = new TestPool(DynamicResourcePool.Options.defaultOptions(), manualClock); List<Resource> resourceList = new ArrayList<>(); resourceList.add(pool.acquire()); resourceList.add(pool.acquire()); resourceList.add(pool.acquire()); pool.release(resourceList.get(2)); pool.release(resourceList.get(0)); manualClock.addTimeMs(1500); pool.release(resourceList.get(1)); for (int i = 0; i < 10; i++) { Resource resource = pool.acquire(); Assert.assertEquals(1, resource.mInteger.intValue()); pool.release(resource); } }
/** * 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; }
@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()); }
/** * 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 gc() throws Exception { ManualClock manualClock = new ManualClock(); TestPool pool = new TestPool( DynamicResourcePool.Options.defaultOptions().setGcIntervalMs(10).setInitialDelayMs(1), manualClock); pool.setGcThresholdInSecs(1); List<Resource> resourceList = new ArrayList<>(); resourceList.add(pool.acquire()); resourceList.add(pool.acquire()); pool.release(resourceList.get(0)); manualClock.addTimeMs(1001); // Sleep 1 second to make sure the GC has run. Thread.sleep(1000); // Resource 0 is gc-ed. Assert.assertEquals(2, pool.acquire().mInteger.intValue()); }
/** * 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; }
/** * Tests the logic that invalid resource won't be acquired. */ @Test public void UnhealthyResource() throws Exception { TestPool pool = new TestPool(DynamicResourcePool.Options.defaultOptions()); Resource resource = pool.acquire(); Assert.assertEquals(0, resource.mInteger.intValue()); resource.setInteger(Resource.INVALID_RESOURCE); pool.release(resource); resource = pool.acquire(); // The 0-th resource is not acquired because it is unhealthy. Assert.assertEquals(1, resource.mInteger.intValue()); }