Exemplo n.º 1
0
  public void testReadWriteZero() throws IOException {
    try {
      // Create a BookKeeper client and a ledger
      bkc = new BookKeeper("127.0.0.1");
      lh = bkc.createLedger(ledgerPassword);
      bkc.initMessageDigest("SHA1");
      ledgerId = lh.getId();
      LOG.info("Ledger ID: " + lh.getId());
      for (int i = 0; i < numEntriesToWrite; i++) {
        bkc.addEntry(lh, new byte[0]);
      }

      /*
       * Write a non-zero entry
       */
      ByteBuffer entry = ByteBuffer.allocate(4);
      entry.putInt(rng.nextInt(maxInt));
      entry.position(0);
      entries.add(entry.array());
      bkc.addEntry(lh, entry.array());

      bkc.closeLedger(lh);
      lh = bkc.openLedger(ledgerId, ledgerPassword);
      LOG.debug("Number of entries written: " + lh.getLast());
      assertTrue("Verifying number of entries written", lh.getLast() == (numEntriesToWrite + 1));

      ls = bkc.readEntries(lh, 0, numEntriesToWrite - 1);
      int i = 0;
      while (ls.hasMoreElements()) {
        ByteBuffer result = ByteBuffer.wrap(ls.nextElement().getEntry());
        LOG.debug("Length of result: " + result.capacity());

        assertTrue("Checking if entry " + i + " has zero bytes", result.capacity() == 0);
      }
      bkc.closeLedger(lh);
    } catch (KeeperException e) {
      e.printStackTrace();
    } catch (BKException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 2
0
  public void testReadWriteSyncSingleClient() throws IOException {
    try {
      // Create a BookKeeper client and a ledger
      bkc = new BookKeeper("127.0.0.1");
      lh = bkc.createLedger(ledgerPassword);
      bkc.initMessageDigest("SHA1");
      ledgerId = lh.getId();
      LOG.info("Ledger ID: " + lh.getId());
      for (int i = 0; i < numEntriesToWrite; i++) {
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);
        entries.add(entry.array());
        bkc.addEntry(lh, entry.array());
      }
      bkc.closeLedger(lh);
      lh = bkc.openLedger(ledgerId, ledgerPassword);
      LOG.debug("Number of entries written: " + lh.getLast());
      assertTrue("Verifying number of entries written", lh.getLast() == numEntriesToWrite);

      ls = bkc.readEntries(lh, 0, numEntriesToWrite - 1);
      int i = 0;
      while (ls.hasMoreElements()) {
        ByteBuffer origbb = ByteBuffer.wrap(entries.get(i++));
        Integer origEntry = origbb.getInt();
        ByteBuffer result = ByteBuffer.wrap(ls.nextElement().getEntry());
        LOG.debug("Length of result: " + result.capacity());
        LOG.debug("Original entry: " + origEntry);

        Integer retrEntry = result.getInt();
        LOG.debug("Retrieved entry: " + retrEntry);
        assertTrue("Checking entry " + i + " for equality", origEntry.equals(retrEntry));
      }
      bkc.closeLedger(lh);
    } catch (KeeperException e) {
      e.printStackTrace();
    } catch (BKException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 3
0
  @Test
  public void testSyncReadAsyncWriteStringsSingleClient() throws IOException {
    LOG.info("TEST READ WRITE STRINGS MIXED SINGLE CLIENT");
    String charset = "utf-8";
    LOG.debug("Default charset: " + Charset.defaultCharset());
    try {
      // Create a BookKeeper client and a ledger
      bkc = new BookKeeper("127.0.0.1");
      lh = bkc.createLedger(ledgerPassword);
      bkc.initMessageDigest("SHA1");
      ledgerId = lh.getId();
      LOG.info("Ledger ID: " + lh.getId());
      for (int i = 0; i < numEntriesToWrite; i++) {
        int randomInt = rng.nextInt(maxInt);
        byte[] entry = new String(Integer.toString(randomInt)).getBytes(charset);
        entries.add(entry);
        bkc.asyncAddEntry(lh, entry, this, sync);
      }

      // wait for all entries to be acknowledged
      synchronized (sync) {
        if (sync.counter < numEntriesToWrite) {
          LOG.debug("Entries counter = " + sync.counter);
          sync.wait();
        }
      }

      LOG.debug("*** ASYNC WRITE COMPLETED ***");
      // close ledger
      bkc.closeLedger(lh);

      // *** WRITING PART COMPLETED // READ PART BEGINS ***

      // open ledger
      lh = bkc.openLedger(ledgerId, ledgerPassword);
      LOG.debug("Number of entries written: " + lh.getLast());
      assertTrue("Verifying number of entries written", lh.getLast() == numEntriesToWrite);

      // read entries
      ls = bkc.readEntries(lh, 0, numEntriesToWrite - 1);

      assertTrue("Checking number of read entries", ls.size() == numEntriesToWrite);

      LOG.debug("*** SYNC READ COMPLETED ***");

      // at this point, LedgerSequence ls is filled with the returned values
      int i = 0;
      while (ls.hasMoreElements()) {
        byte[] origEntryBytes = entries.get(i++);
        byte[] retrEntryBytes = ls.nextElement().getEntry();

        LOG.debug("Original byte entry size: " + origEntryBytes.length);
        LOG.debug("Saved byte entry size: " + retrEntryBytes.length);

        String origEntry = new String(origEntryBytes, charset);
        String retrEntry = new String(retrEntryBytes, charset);

        LOG.debug("Original entry: " + origEntry);
        LOG.debug("Retrieved entry: " + retrEntry);

        assertTrue("Checking entry " + i + " for equality", origEntry.equals(retrEntry));
      }
      bkc.closeLedger(lh);
    } catch (KeeperException e) {
      e.printStackTrace();
    } catch (BKException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 4
0
  @Test
  public void testReadWriteAsyncSingleClient() throws IOException {
    try {
      // Create a BookKeeper client and a ledger
      bkc = new BookKeeper("127.0.0.1");
      lh = bkc.createLedger(ledgerPassword);
      bkc.initMessageDigest("SHA1");
      ledgerId = lh.getId();
      LOG.info("Ledger ID: " + lh.getId());
      for (int i = 0; i < numEntriesToWrite; i++) {
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);

        entries.add(entry.array());
        entriesSize.add(entry.array().length);
        bkc.asyncAddEntry(lh, entry.array(), this, sync);
      }

      // wait for all entries to be acknowledged
      synchronized (sync) {
        if (sync.counter < numEntriesToWrite) {
          LOG.debug("Entries counter = " + sync.counter);
          sync.wait();
        }
      }

      LOG.debug("*** WRITE COMPLETED ***");
      // close ledger
      bkc.closeLedger(lh);

      // *** WRITING PART COMPLETED // READ PART BEGINS ***

      // open ledger
      lh = bkc.openLedger(ledgerId, ledgerPassword);
      LOG.debug("Number of entries written: " + lh.getLast());
      assertTrue("Verifying number of entries written", lh.getLast() == numEntriesToWrite);

      // read entries
      bkc.asyncReadEntries(lh, 0, numEntriesToWrite - 1, this, (Object) sync);

      synchronized (sync) {
        while (sync.value == false) {
          sync.wait();
        }
      }

      assertTrue("Checking number of read entries", ls.size() == numEntriesToWrite);

      LOG.debug("*** READ COMPLETED ***");

      // at this point, LedgerSequence ls is filled with the returned values
      int i = 0;
      while (ls.hasMoreElements()) {
        ByteBuffer origbb = ByteBuffer.wrap(entries.get(i));
        Integer origEntry = origbb.getInt();
        byte[] entry = ls.nextElement().getEntry();
        ByteBuffer result = ByteBuffer.wrap(entry);
        LOG.debug("Length of result: " + result.capacity());
        LOG.debug("Original entry: " + origEntry);

        Integer retrEntry = result.getInt();
        LOG.debug("Retrieved entry: " + retrEntry);
        assertTrue("Checking entry " + i + " for equality", origEntry.equals(retrEntry));
        assertTrue(
            "Checking entry " + i + " for size", entry.length == entriesSize.get(i).intValue());
        i++;
      }
      bkc.closeLedger(lh);
    } catch (KeeperException e) {
      e.printStackTrace();
    } catch (BKException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
  }