### Question:
InstanceLookupExecutor { public void run(String serviceId, Consumer<InstanceInfo> action, BiConsumer<Exception, Boolean> handleFailureConsumer) { log.debug("Started instance finder"); executorService.scheduleAtFixedRate( () -> { try { InstanceInfo instanceInfo = findEurekaInstance(serviceId); log.debug("App found {}", instanceInfo.getAppName()); action.accept(instanceInfo); executorService.shutdownNow(); } catch (InstanceNotFoundException | RetryException e) { log.debug(e.getMessage()); handleFailureConsumer.accept(e, false); } catch (Exception e) { executorService.shutdownNow(); handleFailureConsumer.accept(e, true); log.debug("Unexpected exception while retrieving '{}' service from Eureka", serviceId); } }, initialDelay, period, TimeUnit.MILLISECONDS ); } InstanceLookupExecutor(EurekaClient eurekaClient); void run(String serviceId,
                    Consumer<InstanceInfo> action,
                    BiConsumer<Exception, Boolean> handleFailureConsumer);  }### Answer:
@Test(timeout = 2000) public void testRun_whenNoApplicationRegisteredInDiscovery() throws InterruptedException { instanceLookupExecutor.run( SERVICE_ID, null, (exception, isStopped) -> { lastException = exception; latch.countDown(); } ); latch.await(); assertNotNull(lastException); assertTrue(lastException instanceof InstanceNotFoundException); assertEquals("Service '" + SERVICE_ID + "' is not registered to Discovery Service", lastException.getMessage()); }
@Test(timeout = 2000) public void testRun_whenNoInstancesExistInDiscovery() throws InterruptedException { when(eurekaClient.getApplication(SERVICE_ID)) .thenReturn(new Application(SERVICE_ID, Collections.emptyList())); instanceLookupExecutor.run( SERVICE_ID, null, (exception, isStopped) -> { lastException = exception; latch.countDown(); } ); latch.await(); assertNotNull(lastException); assertTrue(lastException instanceof InstanceNotFoundException); assertEquals("'" + SERVICE_ID + "' has no running instances registered to Discovery Service", lastException.getMessage()); }
@Test(timeout = 2000) public void testRun_whenUnexpectedExceptionHappened() throws InterruptedException { when(eurekaClient.getApplication(SERVICE_ID)) .thenThrow(new InstanceInitializationException("Unexpected Exception")); instanceLookupExecutor.run( SERVICE_ID, null, (exception, isStopped) -> { lastException = exception; latch.countDown(); } ); latch.await(); assertNotNull(lastException); assertTrue(lastException instanceof InstanceInitializationException); }
@Test(timeout = 2000) public void testRun_whenInstanceExistInDiscovery() throws InterruptedException { when(eurekaClient.getApplication(SERVICE_ID)) .thenReturn(new Application(SERVICE_ID, instances)); instanceLookupExecutor.run( SERVICE_ID, instanceInfo -> { lastInstanceInfo = instanceInfo; latch.countDown(); }, null ); latch.await(); assertNull(lastException); assertNotNull(lastInstanceInfo); assertEquals(instances.get(0), lastInstanceInfo); }