### Question:
NodeServiceCache { public ServiceInstance getServiceAgentInstance(ServiceNameVersion service, boolean exact, boolean localOnly, AgentImpl acting) throws AgentNotRegisteredException { ServiceInstance local = null, global = null; if (exact) { synchronized (localServices) { if (localServices.containsKey(service.getName()) && localServices.get(service.getName()).containsKey(service.getVersion())) { local = localServices.get(service.getName()).get(service.getVersion()); } } } else { synchronized (localServices) { if (localServices.containsKey(service.getName())) { for (Map.Entry<ServiceVersion, ServiceInstance> e : localServices.get(service.getName()) .entrySet()) { if (e.getKey().fits(service.getVersion())) { local = e.getValue(); break; } } } } } if (!localOnly && (local == null || runningAt.isBusy())) { if (exact) { ServiceInstance instance = getBestGlobalInstanceOfVersion(service.getName(), service.getVersion()); if (instance == null) { try { update(service, true, acting); } catch (Exception e) { if (e instanceof TimeoutException) { logger.log(Level.INFO, "Timeout while updating service cache. " + e.toString()); } else { logger.log(Level.INFO, "Could not update service cache", e); } if (local == null) { throw new AgentNotRegisteredException( "Could not retrieve service information from the network.", e); } } instance = getBestGlobalInstanceOfVersion(service.getName(), service.getVersion()); } if (instance != null) { global = instance; } } else { ServiceInstance instance = getBestGlobalInstanceFitsVersion(service.getName(), service.getVersion()); if (instance != null) { global = instance; } if (instance == null) { try { update(service, false, acting); } catch (Exception e) { if (e instanceof TimeoutException) { logger.log(Level.INFO, "Timeout while updating service cache. " + e.toString()); } else { logger.log(Level.INFO, "Could not update service cache", e); } if (local == null) { throw new AgentNotRegisteredException( "Could not retrieve service information from the network.", e); } } instance = getBestGlobalInstanceFitsVersion(service.getName(), service.getVersion()); } if (instance != null) { global = instance; } } } if (local != null && (!runningAt.isBusy() || global == null)) { return local; } else if (global != null) { return global; } throw new AgentNotRegisteredException("Could not find any agent for this service on the network!"); } NodeServiceCache(Node parent, long lifeTime, int resultCount); void setWaitForResults(int c); void setLifeTimeSeconds(int c); void setTimeoutMs(int timeoutMs); void clear(); ServiceInstance getServiceAgentInstance(ServiceNameVersion service, boolean exact, boolean localOnly,
			AgentImpl acting); void removeGlobalServiceInstance(ServiceInstance instance); void registerLocalService(ServiceAgentImpl agent); void unregisterLocalService(ServiceAgentImpl agent); ServiceAgentImpl getLocalService(ServiceNameVersion service); List<String> getLocalServiceNames(); List<ServiceVersion> getLocalServiceVersions(String serviceName);  }### Answer:
@Test public void testIntegration() { try { ServiceNameVersion serviceNameVersion = ServiceNameVersion .fromString("i5.las2peer.testServices.testPackage2.UsingService@1.0"); LocalNode serviceNode = new LocalNodeManager().newNode("export/jars/"); serviceNode.launch(); ServiceAgentImpl serviceAgent = serviceNode.startService(serviceNameVersion, "a pass"); ServiceAgentImpl localServiceAgent = serviceNode.getNodeServiceCache() .getServiceAgentInstance(serviceNameVersion, true, true, null).getServiceAgent(); assertSame(serviceAgent, localServiceAgent); serviceNode.stopService(serviceAgent); try { serviceNode.getNodeServiceCache().getServiceAgentInstance(serviceNameVersion, true, true, null); fail("AgentNotRegisteredException exptected!"); } catch (AgentNotRegisteredException e) { } } catch (Exception e) { e.printStackTrace(); Assert.fail(e.toString()); } }