public synchronized void processValues(Map<String, String> updatedValues) { updatedValues.keySet().stream() .forEach(key -> { String newValue = updatedValues.get(key); //process only new or updated values if (! StringUtils.equals(newValue, lastProperties.get(key))) { lastProperties.put(key, newValue); notifyListeners(key, newValue); //(allows command-line overrides) serviceProps.addProperty(key, newValue); } }); dataReceived.set(true); startupLatch.countDown(); }
public TestInjectionModule(String serviceName, ServiceProperties props) { this.serviceProperties = props; serviceProperties.setServiceName(serviceName); if (props.getProperty("registry") == null) { serviceProperties.addProperty("registry", "consul"); } if (props.getProperty("registryServer") == null) { serviceProperties.addProperty("registryServer", "localhost:8500"); } if (props.getProperty("kafkaServer") == null) { serviceProperties.addProperty("kafkaServer", "localhost:9092"); } }
public ServiceImpersonator(String serviceName, ServiceProperties props) throws Exception { //ServiceImpersonator needs its own injection stack so that each mock service // and the service under test get their own ecosystem this.serviceName = serviceName; TestInjectionModule testInjectionModule = new ServiceImpersonatorModule(serviceName, props); serviceProperties = testInjectionModule.getServiceProperties(); serviceProperties.setServiceName(serviceName); //has to be before getting regMgr serviceProperties.setServiceInstanceId(UUID.randomUUID().toString()); serviceProperties.addProperty("registry", "consul"); injector = Guice.createInjector(testInjectionModule, new ServiceRegistryModule(serviceProperties), new TracingModule(serviceProperties)); ServiceDiscoveryProvider provider = injector.getInstance(ServiceDiscoveryProvider.class); LoadBalancerFactory lbFactory = injector.getInstance(LoadBalancerFactory.class); lbFactory.initialize(provider); registrationManager = injector.getInstance(RegistrationManager.class); healthCheckManager = injector.getInstance(HealthCheckManager.class); rpcMethodScanner = new RpcMethodScanner(injector.getInstance(RpcClientFactory.class)); methodHandlers = injector.getInstance(MethodHandlerDictionary.class); messageHandler = injector.getInstance(MessageHandler.class); factory = injector.getInstance(KafkaPublisherFactory.class); initialize(); }
@Before public void setup() { props = new ServiceProperties(); props.addProperty("prop1", "val1"); props.addProperty("prop2", "val2"); cm = new ConfigurationManager(props); }
@Test public void integerValueInvalid() { props.addProperty("foo", "bar"); assertThat(props.getIntegerProperty("foo", 42)).isEqualTo(42); }
@Test public void integerValueHappyPath() { props.addProperty("foo", "42"); assertThat(props.getIntegerProperty("foo", 42)).isEqualTo(42); }
@Test public void getRegistrationUris() { props.addProperty(ServiceProperties.REGISTRY_SERVER_KEY, "localhost:1234"); assertThat(worker.getServiceHealthUri().toString()).isEqualTo( "http://localhost:1234/v1/health/service/foobar?stale"); worker.consulIndex = "69"; assertThat(worker.getServiceHealthUri().toString()).isEqualTo( "http://localhost:1234/v1/health/service/foobar?stale&index=69"); }
public RpcClientIntegrationTest(String featureFlag) { this.featureFlag = featureFlag; TestInjectionModule module = new TestInjectionModule(featureFlag); ServiceProperties props = new ServiceProperties(); props.addProperty(FeatureFlags.FLAG_EXPOSE_ERRORS_HTTP, featureFlag); props.addProperty(FeatureFlags.DISABLE_RPC_INSTANCE_RETRY, "true"); props.addProperty(ServiceProperties.REGISTRY_SERVER_KEY, "localhost:65432"); props.addProperty("registry", "consul"); module.setServiceProperties(props); Injector injector = Guice.createInjector(module, new ServiceRegistryModule(props), new TracingModule(props)); httpClient = (MockHttpClient)injector.getInstance(HttpClient.class); clientFactory = injector.getInstance(RpcClientFactory.class); loadBalancerFactory = injector.getInstance(LoadBalancerFactory.class); rpcClient = clientFactory.newClient(serviceName, "testing", FrameworkTest.Foobar.class).build(); loadBalancer = (LoadBalancerImpl) loadBalancerFactory.getLoadBalancer(serviceName); dependencyHealthCheck = mock(ServiceDependencyHealthCheck.class); }
@Test public void sleepAfterFailedMigration() { props.addProperty("databaseServer", "foo"); Flyway flyway = mock(Flyway.class); when(flyway.migrate()).thenThrow(new FlywayException()); migrator.flyway = flyway; migrator.flywayFailedSemaphore.release(); migrator.migrate(); } }
@Test public void doPost_NotValidJson_ErrorMessageBadRequestWithHttpExposed() throws IOException { // given final String json = "xxxx"; HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(MockHttpServletResponse.class); BufferedReader readerFromRequest = new BufferedReader(new StringReader(json)); when(request.getReader()).thenReturn(readerFromRequest); CharArrayWriter charArryWriter = new CharArrayWriter(512); PrintWriter writer = new PrintWriter(charArryWriter); when(response.getWriter()).thenReturn(writer); doCallRealMethod().when(response).setStatus(anyInt()); doCallRealMethod().when(response).getStatus(); // when ServiceProperties props = new ServiceProperties(); props.addProperty(FeatureFlags.FLAG_EXPOSE_ERRORS_HTTP, "true"); servlet = new JsonHandler(handlerDictionary, metricRegistry, handlerMetrics, props, null); servlet.doPost(request, response); // then final String responseAsString = charArryWriter.toString(); LOGGER.debug(responseAsString); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_BAD_REQUEST); }
@Test public void propertiesNotSet() { props.addProperty("kafkaServer", "test.org:52"); assertThat(props.getKafkaServer()).isEqualTo("test.org:52"); assertThat(props.getProperty("blahblah")).isNull(); }
@Test public void testRpcCallExceptionJsonRpcWithHttpExposed() throws IOException { String input = "{\"service\":\"x\",\"method\":\"jsonRpcWithException\",\"params\":[{}],\"id\":\"dead-beef\"}"; HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(MockHttpServletResponse.class); BufferedReader readerFromRequest = new BufferedReader(new StringReader(input)); when(request.getReader()).thenReturn(readerFromRequest); CharArrayWriter charArryWriter = new CharArrayWriter(512); PrintWriter writer = new PrintWriter(charArryWriter); when(response.getWriter()).thenReturn(writer); doCallRealMethod().when(response).setStatus(anyInt()); doCallRealMethod().when(response).getStatus(); ServiceProperties props = new ServiceProperties(); props.addProperty(FeatureFlags.FLAG_EXPOSE_ERRORS_HTTP, "true"); servlet = new JsonHandler(handlerDictionary, metricRegistry, handlerMetrics, props, null); servlet.doPost(request, response); String responseAsString = charArryWriter.toString(); LOGGER.debug(responseAsString); assertThat(responseAsString).contains("no p4s5!"); assertThat(responseAsString).matches(Pattern.compile(".*retriable.+true.*")); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }
@Before public void setup() throws Exception { HttpClient httpClient = mock(HttpClient.class); response = mock(ContentResponse.class); when(response.getStatus()).thenReturn(200); when(response.getContentAsString()).thenReturn(healthInfo); HttpFields headers = new HttpFields(); headers.add(CONSUL_INDEX, "42"); when(response.getHeaders()).thenReturn(headers); Request request = mock(Request.class); when(httpClient.newRequest(anyString())).thenReturn(request); when(request.send()).thenReturn(response); props = new ServiceProperties(); props.addProperty(ServiceProperties.REGISTRY_SERVER_KEY, "localhost:1234"); worker = new RegistrationMonitorWorker(httpClient, props, mock(ServiceDependencyHealthCheck.class)); worker.setServiceName("foobar"); }
@Before public void setUp() throws Exception { ServiceProperties properties = new ServiceProperties(); properties.addProperty("kafkaServer", "localhost:9092"); KafkaSubscriberFactory<String> kafkaFactory = new KafkaSubscriberFactory<>(properties, null, null); eventHandler = new ServiceTestEventHandler(kafkaFactory); eventHandler.initialize("events"); }
@Test public void throttleTest() throws InterruptedException { int messageCount = 200; CountDownLatch latch = new CountDownLatch(messageCount); DockerPort kafka = docker.containers().container("kafka").port(9092); ServiceProperties props = new ServiceProperties(); props.addProperty(KAFKA_SERVER_KEY, kafka.inFormat("$HOST:$EXTERNAL_PORT")); String topic = "throttle-test"; KafkaPublisherFactory publisherFactory = new KafkaPublisherFactory(props, null); KafkaPublisher publisher = publisherFactory.newBuilder(topic).build(); KafkaSubscriberFactory<String> subscriberFactory = new KafkaSubscriberFactory<>(props, null, null); EventReceivedCallback<String> callback = (message, topicInfo) -> { latch.countDown(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } }; subscriberFactory.newBuilder(topic, callback).withPollTime(50).withAutoCommit(true).build(); for (int i = 0; i < messageCount; i++) { publisher.publishSync("message " + i + randomData()); } latch.await(); }