private void busyWait(final NameServer server, final int expected, final Set<Identifier> ids) { int count = 0; for (; ; ) { final Iterable<NameAssignment> nas = server.lookup(ids); for (@SuppressWarnings("unused") final NameAssignment na : nas) { ++count; } if (count == expected) { break; } count = 0; } }
/** * NameServer and NameClient test. * * @throws Exception */ @Test public void testNameClient() throws Exception { LOG.log(Level.FINEST, this.name.getMethodName()); final String localAddress = localAddressProvider.getLocalAddress(); final Injector injector = Tang.Factory.getTang().newInjector(); injector.bindVolatileParameter( NameServerParameters.NameServerIdentifierFactory.class, this.factory); injector.bindVolatileInstance(LocalAddressProvider.class, this.localAddressProvider); try (final NameServer server = injector.getInstance(NameServer.class)) { this.port = server.getPort(); final Map<Identifier, InetSocketAddress> idToAddrMap = new HashMap<Identifier, InetSocketAddress>(); idToAddrMap.put( this.factory.getNewInstance("task1"), new InetSocketAddress(localAddress, 7001)); idToAddrMap.put( this.factory.getNewInstance("task2"), new InetSocketAddress(localAddress, 7002)); // registration // invoke registration from the client side Configuration nameResolverConf = NameResolverConfiguration.CONF .set(NameResolverConfiguration.NAME_SERVER_HOSTNAME, localAddress) .set(NameResolverConfiguration.NAME_SERVICE_PORT, this.port) .set(NameResolverConfiguration.CACHE_TIMEOUT, this.TTL) .set(NameResolverConfiguration.RETRY_TIMEOUT, RETRY_TIMEOUT) .set(NameResolverConfiguration.RETRY_COUNT, RETRY_COUNT) .build(); try (final NameResolver client = Tang.Factory.getTang().newInjector(nameResolverConf).getInstance(NameClient.class)) { for (final Identifier id : idToAddrMap.keySet()) { client.register(id, idToAddrMap.get(id)); } // wait final Set<Identifier> ids = idToAddrMap.keySet(); busyWait(server, ids.size(), ids); // lookup final Identifier id1 = this.factory.getNewInstance("task1"); final Identifier id2 = this.factory.getNewInstance("task2"); final Map<Identifier, InetSocketAddress> respMap = new HashMap<Identifier, InetSocketAddress>(); InetSocketAddress addr1 = client.lookup(id1); respMap.put(id1, addr1); InetSocketAddress addr2 = client.lookup(id2); respMap.put(id2, addr2); for (final Identifier id : respMap.keySet()) { LOG.log(Level.FINEST, "Mapping: {0} -> {1}", new Object[] {id, respMap.get(id)}); } Assert.assertTrue(isEqual(idToAddrMap, respMap)); // un-registration for (final Identifier id : idToAddrMap.keySet()) { client.unregister(id); } // wait busyWait(server, 0, ids); final Map<Identifier, InetSocketAddress> serverMap = new HashMap<Identifier, InetSocketAddress>(); addr1 = server.lookup(id1); if (addr1 != null) { serverMap.put(id1, addr1); } addr2 = server.lookup(id1); if (addr2 != null) { serverMap.put(id2, addr2); } Assert.assertEquals(0, serverMap.size()); } } }
/** * NameServer and NameRegistryClient test. * * @throws Exception */ @Test public void testNamingRegistry() throws Exception { LOG.log(Level.FINEST, this.name.getMethodName()); final Injector injector = Tang.Factory.getTang().newInjector(); injector.bindVolatileParameter( NameServerParameters.NameServerIdentifierFactory.class, this.factory); injector.bindVolatileInstance(LocalAddressProvider.class, this.localAddressProvider); try (final NameServer server = injector.getInstance(NameServer.class)) { this.port = server.getPort(); final String localAddress = localAddressProvider.getLocalAddress(); // names to start with final Map<Identifier, InetSocketAddress> idToAddrMap = new HashMap<Identifier, InetSocketAddress>(); idToAddrMap.put( this.factory.getNewInstance("task1"), new InetSocketAddress(localAddress, 7001)); idToAddrMap.put( this.factory.getNewInstance("task2"), new InetSocketAddress(localAddress, 7002)); // registration // invoke registration from the client side try (final NameRegistryClient client = new NameRegistryClient( localAddress, this.port, this.factory, this.localAddressProvider)) { for (final Identifier id : idToAddrMap.keySet()) { client.register(id, idToAddrMap.get(id)); } // wait final Set<Identifier> ids = idToAddrMap.keySet(); busyWait(server, ids.size(), ids); // check the server side Map<Identifier, InetSocketAddress> serverMap = new HashMap<Identifier, InetSocketAddress>(); Iterable<NameAssignment> nas = server.lookup(ids); for (final NameAssignment na : nas) { LOG.log( Level.FINEST, "Mapping: {0} -> {1}", new Object[] {na.getIdentifier(), na.getAddress()}); serverMap.put(na.getIdentifier(), na.getAddress()); } Assert.assertTrue(isEqual(idToAddrMap, serverMap)); // un-registration for (final Identifier id : idToAddrMap.keySet()) { client.unregister(id); } // wait busyWait(server, 0, ids); serverMap = new HashMap<Identifier, InetSocketAddress>(); nas = server.lookup(ids); for (final NameAssignment na : nas) { serverMap.put(na.getIdentifier(), na.getAddress()); } Assert.assertEquals(0, serverMap.size()); } } }