예제 #1
0
  @BeforeMethod
  void setUp() throws Exception {
    System.out.print("Connecting channels: ");
    a = createChannel("A");
    disp_a = new MessageDispatcher(a, null, null);
    a.connect(RpcLockingTest.class.getSimpleName());
    lock_a = new LockService(a).getLock("lock");

    b = createChannel("B");
    disp_b = new MessageDispatcher(b, null, null);
    b.connect(RpcLockingTest.class.getSimpleName());
    lock_b = new LockService(b).getLock("lock");

    Util.waitUntilAllChannelsHaveSameSize(30000, 1000, a, b);
    System.out.println("");

    disp_a.setRequestHandler(
        new RequestHandler() {
          @Override
          public Object handle(Message arg0) throws Exception {
            System.out.println("A received a message, will now try to lock the lock");
            if (lock_a.tryLock()) {
              Assert.fail("Should not be able to lock the lock here");
              System.out.println("A aquired the lock, this shouldn't be possible");
            } else System.out.println("The lock was already locked, as it should be");
            return "Hello";
          }
        });

    disp_b.setRequestHandler(
        new RequestHandler() {
          @Override
          public Object handle(Message arg0) throws Exception {
            System.out.println("B received a message, will now try to lock the lock");
            if (lock_b.tryLock()) {
              Assert.fail("Should not be able to lock the lock here");
              System.out.println("B aquired the lock, this shouldn't be possible");
            } else System.out.println("The lock already was locked, as it should be");
            return "Hello";
          }
        });

    // Print who is the coordinator
    if (b.getView().getMembers().get(0).equals(b.getAddress()))
      System.out.println("B is the coordinator");
    else System.out.println("A is the coordinator");
    System.out.println("");
  }
예제 #2
0
  /**
   * If the coordinator of the lock locks the lock and then send a message, the receiver will wait
   * for ever in tryLock. However, castMessage will return after a while because of the default
   * settings of RequestOptions.SYNC().
   */
  public void testCoordSendFirst() throws Exception {
    System.out.println("Running testCoordSendFirst");

    // ===========================================================================
    if (lock_a.tryLock()) {
      try {
        System.out.println("A aquired the lock, about to send message to B");
        String rsp =
            disp_a.sendMessage(
                new Message(b.getAddress(), "bla"), RequestOptions.SYNC().setTimeout(60000));
        if (rsp == null) {
          System.err.println("ERROR: didn't return correctly");
          Assert.fail("Didn't return correctly");
        } else System.out.println("Returned: " + rsp);

      } finally {
        lock_a.unlock();
      }
    } else {
      Assert.fail("The lock was already locked");
      System.out.println("A failed to aquire the lock");
    }
    // ===========================================================================

    System.out.println();
  }