@Test
  public void doesNotBindToNonPublishedProvidersInOtherZones() throws Exception {
    InMemoryServiceRegistry serviceRegistry = new InMemoryServiceRegistry();

    astrixConfigurer.setSubsystem("default");
    astrixConfigurer.set(AstrixSettings.SERVICE_REGISTRY_URI, serviceRegistry.getServiceUri());
    astrixConfigurer.registerApiProvider(PingApiProvider.class);
    clientContext = astrixConfigurer.configure();

    ServiceRegistryExporterClient server1serviceRegistryClient =
        new ServiceRegistryExporterClient(serviceRegistry, "my-subsystem", "server-1");
    ServiceProperties service1Properties =
        DirectComponent.registerAndGetProperties(Ping.class, new PingImpl("1"));
    service1Properties.setProperty(ServiceProperties.PUBLISHED, "false");
    service1Properties.setProperty(ServiceProperties.SERVICE_ZONE, "foo-zone");
    server1serviceRegistryClient.register(Ping.class, service1Properties, Integer.MAX_VALUE);

    ServiceRegistryClient serviceRegistryClient =
        clientContext.getBean(ServiceRegistryClient.class);
    List<ServiceProperties> providers =
        serviceRegistryClient.list(AstrixBeanKey.create(Ping.class));
    assertEquals(1, providers.size());

    Ping ping = clientContext.getBean(Ping.class);
    try {
      ping.ping();
      fail("Expected service to not be available when server is INACTIVE");
    } catch (ServiceUnavailableException e) {
      // expected
    }
  }
  @Test
  public void bindsToNonPublishedProvidersInSameZone() throws Exception {
    InMemoryServiceRegistry serviceRegistry = new InMemoryServiceRegistry();

    astrixConfigurer.setSubsystem("my-subsystem");
    astrixConfigurer.set(AstrixSettings.SERVICE_REGISTRY_URI, serviceRegistry.getServiceUri());
    astrixConfigurer.registerApiProvider(PingApiProvider.class);
    clientContext = astrixConfigurer.configure();

    ServiceRegistryExporterClient server1serviceRegistryClient =
        new ServiceRegistryExporterClient(serviceRegistry, "my-subsystem", "server-1");
    ServiceProperties service1Properties =
        DirectComponent.registerAndGetProperties(Ping.class, new PingImpl("1"));
    service1Properties.setProperty(ServiceProperties.PUBLISHED, "false");
    server1serviceRegistryClient.register(Ping.class, service1Properties, Integer.MAX_VALUE);

    ServiceRegistryClient serviceRegistryClient =
        clientContext.getBean(ServiceRegistryClient.class);
    List<ServiceProperties> providers =
        serviceRegistryClient.list(AstrixBeanKey.create(Ping.class));
    assertEquals(1, providers.size());

    Ping ping = clientContext.getBean(Ping.class);
    assertNotNull(ping.ping());
  }
 private ServiceProperties getPropertiesForAppInstance(
     String appInstanceId, List<ServiceProperties> providers) {
   for (ServiceProperties properties : providers) {
     if (appInstanceId.equals(
         properties.getProperties().get(ServiceProperties.APPLICATION_INSTANCE_ID))) {
       return properties;
     }
   }
   return null;
 }
  @Test
  public void serviceRegistration() throws Exception {
    AstrixServiceRegistry serviceRegistry = clientContext.getBean(AstrixServiceRegistry.class);
    ServiceRegistryClient serviceRegistryClient =
        clientContext.getBean(ServiceRegistryClient.class);
    ServiceRegistryExporterClient exporterClient1 =
        new ServiceRegistryExporterClient(serviceRegistry, "default", "app-instance-1");
    ServiceRegistryExporterClient exporterClient2 =
        new ServiceRegistryExporterClient(serviceRegistry, "default", "app-instance-2");

    ServiceProperties server1Props = new ServiceProperties();
    server1Props.getProperties().put("myProp", "1");
    ServiceProperties server2 = new ServiceProperties();
    server2.getProperties().put("myProp", "1");
    exporterClient1.register(SomeService.class, server1Props, 10000);
    exporterClient2.register(SomeService.class, server2, 10000);
    exporterClient2.register(AnotherService.class, new ServiceProperties(), 10000);

    List<ServiceProperties> providers =
        serviceRegistryClient.list(AstrixBeanKey.create(SomeService.class));
    assertEquals(2, providers.size());

    server1Props = new ServiceProperties();
    server1Props.getProperties().put("myProp", "3");
    exporterClient1.register(SomeService.class, server1Props, 10000);

    providers = serviceRegistryClient.list(AstrixBeanKey.create(SomeService.class));
    assertEquals(2, providers.size());
    ServiceProperties serviceProperties = getPropertiesForAppInstance("app-instance-1", providers);
    assertEquals("3", serviceProperties.getProperty("myProp"));
  }
  @Test
  public void usesRoundRobinToDistributeConsumers() throws Exception {
    InMemoryServiceRegistry serviceRegistry = new InMemoryServiceRegistry();

    astrixConfigurer.set(AstrixSettings.SERVICE_REGISTRY_URI, serviceRegistry.getServiceUri());
    astrixConfigurer.registerApiProvider(PingApiProvider.class);
    clientContext = astrixConfigurer.configure();

    ServiceRegistryExporterClient server1serviceRegistryClient =
        new ServiceRegistryExporterClient(serviceRegistry, "default", "server-1");
    ServiceProperties service1Properties =
        DirectComponent.registerAndGetProperties(Ping.class, new PingImpl("1"));
    server1serviceRegistryClient.register(Ping.class, service1Properties, Integer.MAX_VALUE);

    ServiceRegistryExporterClient server2serviceRegistryClient =
        new ServiceRegistryExporterClient(serviceRegistry, "default", "server-2");
    ServiceProperties service2Properties =
        DirectComponent.registerAndGetProperties(Ping.class, new PingImpl("1"));
    server2serviceRegistryClient.register(Ping.class, service2Properties, Integer.MAX_VALUE);

    ServiceRegistryClient serviceRegistryClient =
        clientContext.getBean(ServiceRegistryClient.class);
    int server1ConsumerCount = 0;
    int server2ConsumerCount = 0;
    for (int i = 0; i < 10; i++) {
      ServiceProperties props = serviceRegistryClient.lookup(AstrixBeanKey.create(Ping.class));
      if ("server-1".equals(props.getProperty(ServiceProperties.APPLICATION_INSTANCE_ID))) {
        server1ConsumerCount++;
      }
      if ("server-2".equals(props.getProperty(ServiceProperties.APPLICATION_INSTANCE_ID))) {
        server2ConsumerCount++;
      }
    }

    assertEquals(5, server1ConsumerCount);
    assertEquals(5, server2ConsumerCount);
  }