TimelockNamespaces { public TimeLockServices get(String namespace) { return services.computeIfAbsent(namespace, this::createNewClient); } TimelockNamespaces(
            MetricsManager metrics,
            Function<String, TimeLockServices> factory,
            Supplier<Integer> maxNumberOfClients); TimeLockServices get(String namespace); Set<Client> getActiveClients(); int getNumberOfActiveClients(); int getMaxNumberOfClients();  }
@Test public void returnsProperServiceForEachClient() { assertThat(namespaces.get(CLIENT_A)).isEqualTo(servicesA); assertThat(namespaces.get(CLIENT_B)).isEqualTo(servicesB); }
@Test public void servicesAreOnlyCreatedOncePerClient() { namespaces.get(CLIENT_A); namespaces.get(CLIENT_A); verify(serviceFactory, times(1)).apply(any()); }
@Test public void doesNotCreateNewClientsAfterMaximumNumberHasBeenReached() { createMaximumNumberOfClients(); assertThatThrownBy(() -> namespaces.get(uniqueClient())) .isInstanceOf(IllegalStateException.class); verify(serviceFactory, times(DEFAULT_MAX_NUMBER_OF_CLIENTS)).apply(any()); verifyNoMoreInteractions(serviceFactory); }
@Test public void canDynamicallyIncreaseMaxAllowedClients() { createMaximumNumberOfClients(); when(maxNumberOfClientsSupplier.get()).thenReturn(DEFAULT_MAX_NUMBER_OF_CLIENTS + 1); namespaces.get(uniqueClient()); }
@Test public void numberOfActiveClientsUpdatesAsNewClientsCreated() { assertNumberOfActiveClientsIs(0); assertMaxClientsIs(DEFAULT_MAX_NUMBER_OF_CLIENTS); namespaces.get(uniqueClient()); assertNumberOfActiveClientsIs(1); assertMaxClientsIs(DEFAULT_MAX_NUMBER_OF_CLIENTS); namespaces.get(uniqueClient()); assertNumberOfActiveClientsIs(2); assertMaxClientsIs(DEFAULT_MAX_NUMBER_OF_CLIENTS); }
@Test public void maxNumberOfClientsRespondsToChanges() { assertNumberOfActiveClientsIs(0); assertMaxClientsIs(DEFAULT_MAX_NUMBER_OF_CLIENTS); when(maxNumberOfClientsSupplier.get()).thenReturn(1); assertNumberOfActiveClientsIs(0); assertMaxClientsIs(1); when(maxNumberOfClientsSupplier.get()).thenReturn(77); assertNumberOfActiveClientsIs(0); assertMaxClientsIs(77); }