@Override public ClusterTierActiveEntity createActiveEntity(ServiceRegistry registry, byte[] configuration) throws ConfigurationException { ClusterTierEntityConfiguration clusterTierEntityConfiguration = configCodec.decodeClusteredStoreConfiguration(configuration); return new ClusterTierActiveEntity(registry, clusterTierEntityConfiguration, DEFAULT_MAPPER); }
@Test(expected = ConfigurationException.class) public void testConfigNull() throws Exception { new ClusterTierActiveEntity(mock(ServiceRegistry.class), null, DEFAULT_MAPPER); }
@Test public void testCreateDedicatedServerStoreExisting() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); ClusterTierActiveEntity otherEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); try { otherEntity.createNew(); fail("Duplicate creation should fail with an exception"); } catch (ConfigurationException e) { assertThat(e.getMessage(), containsString("already exists")); } }
@Test public void testLoadExistingRegistersEvictionListener() throws Exception { EhcacheStateService stateService = mock(EhcacheStateService.class); ServerSideServerStore store = mock(ServerSideServerStore.class); when(stateService.loadStore(eq(defaultStoreName), any())).thenReturn(store); IEntityMessenger<EhcacheEntityMessage, EhcacheEntityResponse> entityMessenger = mock(IEntityMessenger.class); ServiceRegistry registry = getCustomMockedServiceRegistry(stateService, null, entityMessenger, null, null); ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(registry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.loadExisting(); verify(store).setEvictionListener(any(ServerStoreEvictionListener.class)); }
@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 testLoadExistingRecoversInflightInvalidationsForEventualCache() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); EhcacheStateServiceImpl ehcacheStateService = defaultRegistry.getStoreManagerService(); ehcacheStateService.createStore(defaultStoreName, defaultStoreConfiguration, false); //Passive would have done this before failover InvalidationTracker invalidationTracker = ehcacheStateService.getInvalidationTracker(defaultStoreName); Random random = new Random(); random.ints(0, 100).limit(10).forEach(invalidationTracker::trackHashInvalidation); activeEntity.loadExisting(); assertThat(activeEntity.getInflightInvalidations().isEmpty(), is(false)); }
@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 testDestroyServerStore() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); activeEntity.destroy(); assertThat(defaultRegistry.getResource(defaultResource).getUsed(), is(0L)); assertThat(defaultRegistry.getStoreManagerService().getStores(), empty()); assertThat(defaultRegistry.getStoreManagerService().getDedicatedResourcePoolIds(), empty()); }
@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)); }
/** * 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)); }
@Test public void testInvalidMessageThrowsError() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); try { activeEntity.invokeActive(context, new InvalidMessage()); fail("Invalid message should result in AssertionError"); } catch (AssertionError e) { assertThat(e.getMessage(), containsString("Unsupported")); } }
/** * Ensures shared pool and store (cache) name spaces are independent. * The cache alias is used as the name for a {@code ServerStore} instance; this name can be * the same as, but is independent of, the shared pool name. The */ @Test public void testSharedPoolCacheNameCollision() throws Exception { defaultRegistry.addSharedPool(defaultStoreName, MemoryUnit.MEGABYTES.toBytes(2), defaultResource); ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); assertThat(defaultRegistry.getStoreManagerService().getSharedResourcePoolIds(), contains(defaultStoreName)); assertThat(defaultRegistry.getStoreManagerService().getDedicatedResourcePoolIds(), contains(defaultStoreName)); assertThat(defaultRegistry.getStoreManagerService().getStores(), containsInAnyOrder(defaultStoreName)); }
@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 testCreateSharedServerStore() throws Exception { defaultRegistry.addSharedPool(defaultSharedPool, MemoryUnit.MEGABYTES.toBytes(2), defaultResource); ServerStoreConfiguration storeConfiguration = new ServerStoreConfigBuilder() .shared(defaultSharedPool) .build(); ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, new ClusterTierEntityConfiguration(identifier, defaultStoreName, storeConfiguration), DEFAULT_MAPPER); activeEntity.createNew(); assertThat(defaultRegistry.getStoreManagerService().getStores(), containsInAnyOrder(defaultStoreName)); assertThat(defaultRegistry.getStoreManagerService().getSharedResourcePoolIds(), containsInAnyOrder(defaultSharedPool)); assertThat(defaultRegistry.getStoreManagerService().getDedicatedResourcePoolIds(), empty()); assertThat(defaultRegistry.getResource(defaultResource).getUsed(), is(MemoryUnit.MEGABYTES.toBytes(2L))); }
@Test public void testSyncToPassiveNoData() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); assertSuccess(activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration))); @SuppressWarnings("unchecked") PassiveSynchronizationChannel<EhcacheEntityMessage> syncChannel = mock(PassiveSynchronizationChannel.class); activeEntity.synchronizeKeyToPassive(syncChannel, 3); verifyZeroInteractions(syncChannel); }
@Test public void testCreateNonExistentSharedPool() throws Exception { ServerStoreConfiguration storeConfiguration = new ServerStoreConfigBuilder() .shared(defaultSharedPool) .build(); ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, new ClusterTierEntityConfiguration(identifier, defaultStoreName, storeConfiguration), DEFAULT_MAPPER); try { activeEntity.createNew(); fail("Creation with non-existent shared pool should have failed"); } catch (ConfigurationException e) { assertThat(e.getMessage(), containsString("undefined")); } }
@Test public void testCreateUnknownServerResource() throws Exception { ServerStoreConfiguration storeConfiguration = new ServerStoreConfigBuilder() .dedicated("unknown", 2, MemoryUnit.MEGABYTES) .build(); ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, new ClusterTierEntityConfiguration(identifier, defaultStoreName, storeConfiguration), DEFAULT_MAPPER); try { activeEntity.createNew(); fail("Creation with non-existent shared pool should have failed"); } catch (ConfigurationException e) { assertThat(e.getMessage(), containsString("Non-existent server side resource")); } }
@Test public void testValidateUnknown() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); assertSuccess(activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, new ServerStoreConfigBuilder().unknown().build()))); }
@Test public void testValidateDedicatedServerStoreBad() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); assertFailure(activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, new ServerStoreConfigBuilder() .dedicated(defaultResource, 8, MemoryUnit.MEGABYTES) .build())), InvalidServerStoreConfigurationException.class); }
@Test public void testValidateSharedServerStore() throws Exception { defaultRegistry.addSharedPool(defaultSharedPool, MemoryUnit.MEGABYTES.toBytes(2), defaultResource); ServerStoreConfiguration storeConfiguration = new ServerStoreConfigBuilder() .shared(defaultSharedPool) .build(); ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, new ClusterTierEntityConfiguration(identifier, defaultStoreName, storeConfiguration), DEFAULT_MAPPER); activeEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); assertSuccess(activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, storeConfiguration))); assertThat(activeEntity.getConnectedClients(), contains(context.getClientDescriptor())); }