### Question:
ServiceAccessScope implements AutoCloseable { static void notifyExternalServiceAccessListenerIfPresent(Supplier<ServiceAccessEvent> eventSupplier) { ServiceAccessListener eventListener = listenerThreadLocal.get(); if (eventListener != null) { eventListener.accept(eventSupplier.get()); } } ServiceAccessScope(ServiceAccessListener eventListener); @Override void close();  }### Answer:
@Test public void listenerShouldBeNotifiedFromInsideServiceAccessScope() { try (ServiceAccessScope scope = new ServiceAccessScope(mockedListener)) { ServiceAccessScope.notifyExternalServiceAccessListenerIfPresent(mockedEventSupplier); } Mockito.verify(mockedEventSupplier, Mockito.times(1)).get(); Mockito.verify(mockedListener, Mockito.times(1)).accept(mockedEvent); Mockito.verifyNoMoreInteractions(mockedEventSupplier, mockedListener); Mockito.verifyZeroInteractions(mockedEvent); }
@Test public void listenerShouldNotBeNotifiedBeforeEnteringServiceAccessScope() { ServiceAccessScope.notifyExternalServiceAccessListenerIfPresent(mockedEventSupplier); try (ServiceAccessScope scope = new ServiceAccessScope(mockedListener)) { } Mockito.verifyZeroInteractions(mockedEventSupplier, mockedListener, mockedEvent); }
@Test public void listenerShouldNotBeNotifiedAfterLeavingServiceAccessScope() { try (ServiceAccessScope scope = new ServiceAccessScope(mockedListener)) { } ServiceAccessScope.notifyExternalServiceAccessListenerIfPresent(mockedEventSupplier); Mockito.verifyZeroInteractions(mockedEventSupplier, mockedListener, mockedEvent); }
@Test public void eventsShouldBeCaughtOnlyInTheSameThreadTheyWerePublishedIn() throws Exception { ThreadSafeListener threadSafeListener = new ThreadSafeListener(); ExecutorService executorService = Executors.newFixedThreadPool(4); try (ServiceAccessScope scope = new ServiceAccessScope(threadSafeListener)) { executorService.submit(() -> notifyExternalServiceAccessListenerWithoutExplicitScope(ServiceType.AIA_OCSP)); ServiceAccessScope.notifyExternalServiceAccessListenerIfPresent(new ThreadBasedEventSupplier(ServiceType.TSP)); executorService.submit(() -> notifyExternalServiceAccessListenerWithoutExplicitScope(ServiceType.AIA_OCSP)); ServiceAccessScope.notifyExternalServiceAccessListenerIfPresent(new ThreadBasedEventSupplier(ServiceType.OCSP)); executorService.submit(() -> notifyExternalServiceAccessListenerWithoutExplicitScope(ServiceType.AIA_OCSP)); Future<ThreadSafeListener> result = executorService.submit(() -> notifyExternalServiceAccessListenerInLocalScope(ServiceType.AIA_OCSP)); Assert.assertEquals(1, result.get().getReceivedEvents().size()); executorService.shutdown(); Assert.assertTrue( "Abnormal termination of " + executorService.getClass().getSimpleName(), executorService.awaitTermination(10L, TimeUnit.SECONDS) ); } finally { executorService.shutdownNow(); } List<ServiceAccessEvent> receivedEvents = threadSafeListener.getReceivedEvents(); Assert.assertEquals(2, receivedEvents.size()); assertEventFromCurrentThread(ServiceType.TSP, receivedEvents.get(0)); assertEventFromCurrentThread(ServiceType.OCSP, receivedEvents.get(1)); }