Exemplo n.º 1
0
  @Test
  public void triangulate2() throws OutOfRangeException {
    DataPoint a = new DataPoint(43.074553, -75.759592, 62.583);
    DataPoint b = new DataPoint(43.134420, -75.228998, 302.2);
    Coordinate intersection = DataPoint.triangulate(a, b);

    double expectedLat = 43.211699;
    double expectedLon = -75.397193;
    double calculatedLat = intersection.getLatitude();
    double calculatedLon = intersection.getLongitude();

    double faultLat = Math.abs(expectedLat - calculatedLat);
    double faultLon = Math.abs(expectedLon - calculatedLon);

    assertTrue(faultLat < TRIANGULATE_TOLERANCE && faultLon < TRIANGULATE_TOLERANCE);
  }
  /**
   * Tests the behavior of Zookeeper upon a restart. ZK should clean up old coordinates.
   *
   * @throws Exception
   */
  @Test
  public void testZookeeperRestarts() throws Exception {
    final CountDownLatch connectedLatch1 = new CountDownLatch(1);
    final CountDownLatch connectedLatch2 = new CountDownLatch(3);
    TestCoordinateListener listener = setUpListenerEnvironment(connectedLatch1, connectedLatch2);
    assertTrue(connectedLatch1.await(20, TimeUnit.SECONDS));
    log.info("Killing zookeeper");
    forwarder.terminate();

    ezk.shutdown();
    ezk.del();
    ezk.init();
    Thread.sleep(2000);
    forwarder = new PortForwarder(forwarderPort, "127.0.0.1", zkport);

    int timeoutSecs = 30;
    while (--timeoutSecs > 0) {
      Thread.sleep(1000);
    }
    Coordinate c = Coordinate.parse("1.service.user.cell");
    cn.createCoordinate(c);

    Thread.sleep(9000);
    assertEquals(
        listener.events.get(listener.events.size() - 1), CoordinateListener.Event.COORDINATE_OK);
  }
Exemplo n.º 3
0
  @Test
  public void triangulate1() throws OutOfRangeException {
    DataPoint a = new DataPoint(51, 0.25, 108);
    DataPoint b = new DataPoint(49, 2.5, 32);
    Coordinate intersection = DataPoint.triangulate(a, b);

    double expectedLat = 50.23;
    double expectedLon = 3.7075;
    double calculatedLat = intersection.getLatitude();
    double calculatedLon = intersection.getLongitude();

    double faultLat = Math.abs(expectedLat - calculatedLat);
    double faultLon = Math.abs(expectedLon - calculatedLon);

    assertTrue(faultLat < TRIANGULATE_TOLERANCE && faultLon < TRIANGULATE_TOLERANCE);
  }
  private TestCoordinateListener setUpListenerEnvironment(
      CountDownLatch connectedLatch1, CountDownLatch connectedLatch2) throws Exception {
    forwarderPort = Net.getFreePort();
    forwarder = new PortForwarder(forwarderPort, "127.0.0.1", zkport);
    final Coordinate c = Coordinate.parse("1.service.user.cell");

    cn = makeLocalZkCloudname(forwarderPort);
    try {
      cn.createCoordinate(c);
    } catch (CoordinateException e) {
      fail(e.toString());
    }
    final TestCoordinateListener listener =
        new TestCoordinateListener(connectedLatch1, connectedLatch2);
    ServiceHandle serviceHandle = cn.claim(c);
    serviceHandle.registerCoordinateListener(listener);

    return listener;
  }
  /**
   * Tests that one process claims a coordinate, then another process tries to claim the same
   * coordinate. The first coordinate looses connection to ZooKeeper and the other process gets the
   * coordinate.
   *
   * @throws Exception
   */
  @Test
  public void testFastHardRestart() throws Exception {
    final Coordinate c = Coordinate.parse("1.service.user.cell");
    final CountDownLatch claimLatch1 = new CountDownLatch(1);
    forwarderPort = Net.getFreePort();
    forwarder = new PortForwarder(forwarderPort, "127.0.0.1", zkport);
    Cloudname cn1 =
        new ZkCloudname.Builder().setConnectString("localhost:" + forwarderPort).build().connect();
    cn1.createCoordinate(c);

    ServiceHandle handle1 = cn1.claim(c);
    handle1.registerCoordinateListener(
        new CoordinateListener() {

          @Override
          public void onCoordinateEvent(Event event, String message) {
            if (event == Event.COORDINATE_OK) {
              claimLatch1.countDown();
            }
          }
        });
    assertTrue(claimLatch1.await(5, TimeUnit.SECONDS));

    Cloudname cn2 =
        new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();

    ServiceHandle handle2 = cn2.claim(c);

    forwarder.terminate();

    assertTrue(handle2.waitForCoordinateOkSeconds(20));

    ServiceStatus status = new ServiceStatus(ServiceState.RUNNING, "updated status");
    handle2.setStatus(status);

    Cloudname cn3 =
        new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
    ServiceStatus statusRetrieved = cn3.getStatus(c);
    assertEquals("updated status", statusRetrieved.getMessage());
  }
 @Test
 public void testEquals_notEqual() {
   Coordinate c1 = new Coordinate(3, -2);
   Coordinate c2 = new Coordinate(-2, 3);
   assertFalse(c1.equals(c2));
 }
 @Test
 public void testManhattanDistance() {
   Coordinate c1 = new Coordinate(3, 5);
   Coordinate c2 = new Coordinate(-3, 2);
   assertEquals(6 + 3, (int) Coordinate.manhattanDistance(c1, c2));
 }
 @Test
 public void testEquals_equal() {
   Coordinate c1 = new Coordinate(6, -2);
   Coordinate c2 = new Coordinate(6, -2);
   assertTrue(c1.equals(c2));
 }