@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 serviceRegsistrySupportsMultipleProvidersOfSameService() 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, "default", "server-1");
    server1serviceRegistryClient.register(
        Ping.class,
        DirectComponent.registerAndGetProperties(Ping.class, new PingImpl("1")),
        Integer.MAX_VALUE);

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

    Ping ping1 = clientContext.getBean(Ping.class);
    ServiceRegistryClient serviceRegistryClient =
        clientContext.getBean(ServiceRegistryClient.class);
    List<ServiceProperties> providers =
        serviceRegistryClient.list(AstrixBeanKey.create(Ping.class));
    assertEquals(2, providers.size());
    assertNotNull(ping1.ping());
  }
  @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());
  }
  @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);
  }
 @Before
 public void setup() {
   astrixConfigurer.registerApiProvider(AstrixServiceRegistryLibraryProvider.class);
   astrixConfigurer.registerApiProvider(AstrixServiceRegistryServiceProvider.class);
 }