Ejemplo n.º 1
0
 @Test
 public void testThatTransportConnectsOnPrefixRegistration()
     throws IOException, SecurityException {
   assertFalse(face.getTransport().getIsConnected());
   face.registerPrefix(
       new Name("/fake/prefix"),
       (OnInterestCallback) null,
       (OnRegisterFailed) null,
       (OnRegisterSuccess) null);
   assertTrue(face.getTransport().getIsConnected());
 }
Ejemplo n.º 2
0
  @Test
  public void testInterestFilters()
      throws IOException, SecurityException, EncodingException, InterruptedException {
    class State {
      boolean regFailed = false;
      boolean regSucceed = false;
    }
    final State state = new State();

    // connect transport
    face.registerPrefix(
        new Name("/fake/prefix"),
        (OnInterestCallback) null,
        new OnRegisterFailed() {
          @Override
          public void onRegisterFailed(final Name prefix) {
            state.regFailed = true;
            counter++;
          }
        },
        new OnRegisterSuccess() {
          @Override
          public void onRegisterSuccess(final Name prefix, final long registeredPrefixId) {
            state.regSucceed = true;
            counter++;
          }
        });

    // set filter
    face.setInterestFilter(
        new InterestFilter("/a/b"),
        new OnInterestCallback() {
          @Override
          public void onInterest(
              final Name prefix,
              final Interest interest,
              final Face face,
              final long interestFilterId,
              final InterestFilter filter) {
            counter++;
          }
        });

    face.receive(new Interest(new Name("/a/b")).setInterestLifetimeMilliseconds(100));

    run(10, 2);

    assertEquals(2, counter);
    assertTrue(state.regSucceed);
    assertFalse(state.regFailed);
  }
Ejemplo n.º 3
0
 private void run(final int limit, final int maxCounter)
     throws IOException, EncodingException, InterruptedException {
   // process face until a response is received
   int allowedLoops = limit;
   while (counter < maxCounter && allowedLoops > 0) {
     allowedLoops--;
     face.processEvents();
     Thread.sleep(100);
   }
 }
Ejemplo n.º 4
0
  @Test
  public void testMockWithoutMockRegistrationReply() throws Exception {
    face = new MockFace(new MockFace.Options().setEnableRegistrationReply(false));

    class State {
      boolean regFailed = false;
      boolean regSucceed = false;
    }
    final State state = new State();

    LOG.info("Register prefix: /test/with/handlers");
    face.registerPrefix(
        new Name("/test/with/handlers"),
        new OnInterestCallback() {
          @Override
          public void onInterest(
              final Name prefix,
              final Interest interest,
              final Face face,
              final long interestFilterId,
              final InterestFilter filter) {
            LOG.info("Received interest, responding: " + interest.getName().toUri());
            Data response = new Data(new Name("/test/with/handlers"));
            response.setContent(new Blob("..."));
            try {
              face.putData(response);
            } catch (IOException e) {
              exception = e;
            }
            counter++;
          }
        },
        new OnRegisterFailed() {
          @Override
          public void onRegisterFailed(final Name prefix) {
            LOG.info("Prefix registration fails: " + prefix);
            state.regFailed = true;
            counter++;
          }
        },
        new OnRegisterSuccess() {
          @Override
          public void onRegisterSuccess(final Name prefix, final long registeredPrefixId) {
            LOG.info("Prefix registration succeed: " + prefix);
            state.regSucceed = true;
            counter++;
          }
        });

    run(100, 1);
    assertFalse(state.regSucceed);
    assertTrue(state.regFailed);
  }
Ejemplo n.º 5
0
  @Test
  public void testExpressingAnInterestAfterConfiguration()
      throws IOException, EncodingException, InterruptedException {
    // add response (before face is connected)
    Data response = new Data(new Name("/test/with/responses"));
    response.setContent(new Blob("..."));
    face.receive(response);

    // make request
    expressInterest("/test/with/responses");

    run(20);

    assertNotNull(recvData);
    assertEquals(isTimeout, false);
    assertEquals(recvData.getName().toString(), "/test/with/responses");
    assertEquals(recvData.getContent().buf(), new Blob("...").buf());
  }
Ejemplo n.º 6
0
 private void expressInterest(final String name) throws IOException {
   LOG.info("Express interest: " + name);
   face.expressInterest(
       new Interest(new Name(name)).setInterestLifetimeMilliseconds(1000),
       new OnData() {
         @Override
         public void onData(final Interest interest, final Data data) {
           counter++;
           LOG.fine("Received data");
           recvData = data;
         }
       },
       new OnTimeout() {
         @Override
         public void onTimeout(final Interest interest) {
           LOG.fine("Received timeout");
           counter++;
           isTimeout = true;
         }
       });
 }
Ejemplo n.º 7
0
  @Test
  public void testPrefixRegistration()
      throws IOException, SecurityException, EncodingException, InterruptedException {
    class State {
      boolean regFailed = false;
      boolean regSucceed = false;
    }
    final State state = new State();

    LOG.info("Register prefix: /test/with/handlers");
    face.registerPrefix(
        new Name("/test/with/handlers"),
        new OnInterestCallback() {
          @Override
          public void onInterest(
              final Name prefix,
              final Interest interest,
              final Face face,
              final long interestFilterId,
              final InterestFilter filter) {
            LOG.info("Received interest, responding: " + interest.getName().toUri());
            Data response = new Data(new Name("/test/with/handlers"));
            response.setContent(new Blob("..."));
            try {
              face.putData(response);
            } catch (IOException e) {
              exception = e;
            }
            counter++;
          }
        },
        new OnRegisterFailed() {
          @Override
          public void onRegisterFailed(final Name prefix) {
            LOG.info("Prefix registration fails: " + prefix);
            state.regFailed = true;
            counter++;
          }
        },
        new OnRegisterSuccess() {
          @Override
          public void onRegisterSuccess(final Name prefix, final long registeredPrefixId) {
            LOG.info("Prefix registration succeed: " + prefix);
            state.regSucceed = true;
            counter++;
          }
        });

    run(100, 1);
    assertTrue(state.regSucceed);
    assertFalse(state.regFailed);

    // make request
    face.receive(new Interest(new Name("/test/with/handlers")));

    run(100, 2);

    assertNull(exception);

    assertEquals(face.sentData.size(), 1);
    assertFalse(isTimeout);
    assertEquals("/test/with/handlers", face.sentData.get(0).getName().toString());
    assertEquals(new Blob("...").buf(), face.sentData.get(0).getContent().buf());
  }