예제 #1
0
  /**
   * NameServer and NameLookupClient test.
   *
   * @throws Exception
   */
  @Test
  public void testNamingLookup() throws Exception {

    final String localAddress = localAddressProvider.getLocalAddress();
    LOG.log(Level.FINEST, this.name.getMethodName());

    // names
    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));

    // run a server
    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();
      for (final Identifier id : idToAddrMap.keySet()) {
        server.register(id, idToAddrMap.get(id));
      }

      // run a client
      try (final NameLookupClient client =
          new NameLookupClient(
              localAddress,
              this.port,
              10000,
              this.factory,
              RETRY_COUNT,
              RETRY_TIMEOUT,
              new NameCache(this.TTL),
              this.localAddressProvider)) {

        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));
      }
    }
  }
예제 #2
0
  /**
   * Test concurrent lookups (threads share a client).
   *
   * @throws Exception
   */
  @Test
  public void testConcurrentNamingLookup() throws Exception {

    LOG.log(Level.FINEST, this.name.getMethodName());

    final String localAddress = localAddressProvider.getLocalAddress();
    // test it 3 times to make failure likely
    for (int i = 0; i < 3; i++) {

      LOG.log(Level.FINEST, "test {0}", i);

      // names
      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));
      idToAddrMap.put(
          this.factory.getNewInstance("task3"), new InetSocketAddress(localAddress, 7003));

      // run a server
      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();
        for (final Identifier id : idToAddrMap.keySet()) {
          server.register(id, idToAddrMap.get(id));
        }

        // run a client
        try (final NameLookupClient client =
            new NameLookupClient(
                localAddress,
                this.port,
                10000,
                this.factory,
                RETRY_COUNT,
                RETRY_TIMEOUT,
                new NameCache(this.TTL),
                this.localAddressProvider)) {

          final Identifier id1 = this.factory.getNewInstance("task1");
          final Identifier id2 = this.factory.getNewInstance("task2");
          final Identifier id3 = this.factory.getNewInstance("task3");

          final ExecutorService e = Executors.newCachedThreadPool();

          final ConcurrentMap<Identifier, InetSocketAddress> respMap =
              new ConcurrentHashMap<Identifier, InetSocketAddress>();

          final Future<?> f1 =
              e.submit(
                  new Runnable() {
                    @Override
                    public void run() {
                      InetSocketAddress addr = null;
                      try {
                        addr = client.lookup(id1);
                      } catch (final Exception e) {
                        LOG.log(Level.SEVERE, "Lookup failed", e);
                        Assert.fail(e.toString());
                      }
                      respMap.put(id1, addr);
                    }
                  });
          final Future<?> f2 =
              e.submit(
                  new Runnable() {
                    @Override
                    public void run() {
                      InetSocketAddress addr = null;
                      try {
                        addr = client.lookup(id2);
                      } catch (final Exception e) {
                        LOG.log(Level.SEVERE, "Lookup failed", e);
                        Assert.fail(e.toString());
                      }
                      respMap.put(id2, addr);
                    }
                  });
          final Future<?> f3 =
              e.submit(
                  new Runnable() {
                    @Override
                    public void run() {
                      InetSocketAddress addr = null;
                      try {
                        addr = client.lookup(id3);
                      } catch (final Exception e) {
                        LOG.log(Level.SEVERE, "Lookup failed", e);
                        Assert.fail(e.toString());
                      }
                      respMap.put(id3, addr);
                    }
                  });

          f1.get();
          f2.get();
          f3.get();

          for (final Identifier id : respMap.keySet()) {
            LOG.log(Level.FINEST, "Mapping: {0} -> {1}", new Object[] {id, respMap.get(id)});
          }

          Assert.assertTrue(isEqual(idToAddrMap, respMap));
        }
      }
    }
  }