public static ClientDescriptor create() { return new TestClientDescriptor(); }
@Test public void testCreateLockStateAfterFailover() { LockManagerImpl lockManager = new LockManagerImpl(); ClientDescriptor clientDescriptor = new TestClientDescriptor(); Set<Long> locks = new HashSet<>(); locks.add(1L); locks.add(100L); locks.add(1000L); lockManager.createLockStateAfterFailover(clientDescriptor, locks); ClientDescriptor clientDescriptor1 = new TestClientDescriptor(); assertThat(lockManager.lock(100L, clientDescriptor1), is(false)); assertThat(lockManager.lock(1000L, clientDescriptor1), is(false)); assertThat(lockManager.lock(1L, clientDescriptor1), is(false)); }
@Test public void testDisconnectedNotConnected() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); ClientDescriptor client1 = new TestClientDescriptor(); activeEntity.disconnected(client1); // Not expected to fail ... }
@Test public void testConnectedSecond() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); ClientDescriptor client1 = new TestClientDescriptor(); activeEntity.connected(client1); ClientDescriptor client2 = new TestClientDescriptor(); activeEntity.connected(client2); Set<ClientDescriptor> connectedClients = activeEntity.getConnectedClients(); assertThat(connectedClients, hasSize(2)); assertThat(connectedClients, hasItems(client1, client2)); }
@Test public void testConnected() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); ClientDescriptor client = new TestClientDescriptor(); activeEntity.connected(client); Set<ClientDescriptor> connectedClients = activeEntity.getConnectedClients(); assertThat(connectedClients, hasSize(1)); assertThat(connectedClients, hasItem(client)); }
@Test public void testUnlock() { LockManagerImpl lockManager = new LockManagerImpl(); ClientDescriptor clientDescriptor = new TestClientDescriptor(); assertThat(lockManager.lock(1L, clientDescriptor), is(true)); lockManager.unlock(1L); assertThat(lockManager.lock(1L, clientDescriptor), is(true)); }
@Test public void testLock() { LockManagerImpl lockManager = new LockManagerImpl(); ClientDescriptor clientDescriptor = new TestClientDescriptor(); assertThat(lockManager.lock(1L, clientDescriptor), is(true)); assertThat(lockManager.lock(1L, clientDescriptor), is(false)); assertThat(lockManager.lock(2L, clientDescriptor), is(true)); }
/** * Ensures the disconnect of a connected client is properly tracked and does not affect others. */ @Test public void testDisconnectedSecond() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); ClientDescriptor client1 = new TestClientDescriptor(); activeEntity.connected(client1); ClientDescriptor client2 = new TestClientDescriptor(); activeEntity.connected(client2); assertThat(activeEntity.getConnectedClients(), hasSize(2)); activeEntity.disconnected(client1); Set<ClientDescriptor> connectedClients = activeEntity.getConnectedClients(); assertThat(connectedClients, hasSize(1)); assertThat(connectedClients, hasItem(client2)); }
@Test public void testConnectedAgain() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); ClientDescriptor client = new TestClientDescriptor(); activeEntity.connected(client); activeEntity.connected(client); Set<ClientDescriptor> connectedClients = activeEntity.getConnectedClients(); assertThat(connectedClients, hasSize(1)); assertThat(connectedClients, hasItem(client)); }
@Test @SuppressWarnings("unchecked") public void testSweepLocksForClient() { LockManagerImpl lockManager = new LockManagerImpl(); ClientDescriptor clientDescriptor1 = new TestClientDescriptor(); ClientDescriptor clientDescriptor2 = new TestClientDescriptor(); assertThat(lockManager.lock(1L, clientDescriptor1), is(true)); assertThat(lockManager.lock(2L, clientDescriptor1), is(true)); assertThat(lockManager.lock(3L, clientDescriptor1), is(true)); assertThat(lockManager.lock(4L, clientDescriptor1), is(true)); assertThat(lockManager.lock(5L, clientDescriptor2), is(true)); assertThat(lockManager.lock(6L, clientDescriptor2), is(true)); AtomicInteger counter = new AtomicInteger(); Consumer<List<Long>> consumer = mock(Consumer.class); ArgumentCaptor<List<Long>> argumentCaptor = ArgumentCaptor.forClass(List.class); doAnswer(invocation -> counter.incrementAndGet()).when(consumer).accept(argumentCaptor.capture()); lockManager.sweepLocksForClient(clientDescriptor2, consumer); assertThat(counter.get(), is(1)); assertThat(argumentCaptor.getValue().size(), is(2)); assertThat(argumentCaptor.getValue(), containsInAnyOrder(5L, 6L)); assertThat(lockManager.lock(5L, clientDescriptor2), is(true)); assertThat(lockManager.lock(6L, clientDescriptor2), is(true)); assertThat(lockManager.lock(1L, clientDescriptor1), is(false)); assertThat(lockManager.lock(2L, clientDescriptor1), is(false)); assertThat(lockManager.lock(3L, clientDescriptor1), is(false)); assertThat(lockManager.lock(4L, clientDescriptor1), is(false)); }
/** * Ensures the disconnect of a connected client is properly tracked. */ @Test public void testDisconnected() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); ClientDescriptor client1 = new TestClientDescriptor(); activeEntity.connected(client1); activeEntity.disconnected(client1); assertThat(activeEntity.getConnectedClients(), hasSize(0)); }
/** * Ensures a disconnect for a non-connected client does not throw. */ @Test public void testDisconnectedNotConnected() throws Exception { OffHeapIdentifierRegistry registry = new OffHeapIdentifierRegistry(); registry.addResource("defaultServerResource", 8, MemoryUnit.MEGABYTES); EhcacheStateService ehcacheStateService = registry.getService(new EhcacheStateServiceConfig(blankConfiguration, registry, DEFAULT_MAPPER)); final ClusterTierManagerActiveEntity activeEntity = new ClusterTierManagerActiveEntity(blankConfiguration, ehcacheStateService, management); ClientDescriptor client = new TestClientDescriptor(); activeEntity.disconnected(client); // Not expected to fail ... }
/** * Ensures basic shared resource pool configuration. */ @Test public void testConfigure() throws Exception { OffHeapIdentifierRegistry registry = new OffHeapIdentifierRegistry(); registry.addResource("defaultServerResource", 8, MemoryUnit.MEGABYTES); registry.addResource("serverResource1", 8, MemoryUnit.MEGABYTES); registry.addResource("serverResource2", 8, MemoryUnit.MEGABYTES); ServerSideConfiguration serverSideConfiguration = new ServerSideConfigBuilder() .defaultResource("defaultServerResource") .sharedPool("primary", "serverResource1", 4, MemoryUnit.MEGABYTES) .sharedPool("secondary", "serverResource2", 8, MemoryUnit.MEGABYTES) .build(); ClusterTierManagerConfiguration configuration = new ClusterTierManagerConfiguration("identifier", serverSideConfiguration); EhcacheStateService ehcacheStateService = registry.getService(new EhcacheStateServiceConfig(configuration, registry, DEFAULT_MAPPER)); final ClusterTierManagerActiveEntity activeEntity = new ClusterTierManagerActiveEntity(configuration, ehcacheStateService, management); ClientDescriptor client = new TestClientDescriptor(); activeEntity.connected(client); assertThat(registry.getStoreManagerService().getSharedResourcePoolIds(), containsInAnyOrder("primary", "secondary")); assertThat(registry.getResource("serverResource1").getUsed(), is(MemoryUnit.MEGABYTES.toBytes(4L))); assertThat(registry.getResource("serverResource2").getUsed(), is(MemoryUnit.MEGABYTES.toBytes(8L))); assertThat(registry.getResource("defaultServerResource").getUsed(), is(0L)); assertThat(registry.getStoreManagerService().getStores(), is(empty())); }