@Test
public void testAddStorage() {

  // first add NUM_DATA_BLOCKS + NUM_PARITY_BLOCKS storages, i.e., a complete
  // group of blocks/storages
  DatanodeStorageInfo[] storageInfos = DFSTestUtil.createDatanodeStorageInfos(
      totalBlocks);
  Block[] blocks = createReportedBlocks(totalBlocks);
  int i = 0;
  for (; i < storageInfos.length; i += 2) {
    info.addStorage(storageInfos[i], blocks[i]);
    Assert.assertEquals(i/2 + 1, info.numNodes());
  }
  i /= 2;
  for (int j = 1; j < storageInfos.length; j += 2) {
    Assert.assertTrue(info.addStorage(storageInfos[j], blocks[j]));
    Assert.assertEquals(i + (j+1)/2, info.numNodes());
  }
  // check
  byte[] indices = (byte[]) Whitebox.getInternalState(info, "indices");
  Assert.assertEquals(totalBlocks, info.getCapacity());
  Assert.assertEquals(totalBlocks, indices.length);
  i = 0;
  for (DatanodeStorageInfo storage : storageInfos) {
    int index = info.findStorageInfo(storage);
    Assert.assertEquals(i++, index);
    Assert.assertEquals(index, indices[index]);
  }
  // the same block is reported from the same storage twice
  i = 0;
  for (DatanodeStorageInfo storage : storageInfos) {
    Assert.assertTrue(info.addStorage(storage, blocks[i++]));
  }
  Assert.assertEquals(totalBlocks, info.getCapacity());
  Assert.assertEquals(totalBlocks, info.numNodes());
  Assert.assertEquals(totalBlocks, indices.length);
  i = 0;
  for (DatanodeStorageInfo storage : storageInfos) {
    int index = info.findStorageInfo(storage);
    Assert.assertEquals(i++, index);
    Assert.assertEquals(index, indices[index]);
  }
  // the same block is reported from another storage
  DatanodeStorageInfo[] storageInfos2 =
      DFSTestUtil.createDatanodeStorageInfos(totalBlocks * 2);
  // only add the second half of info2
  for (i = totalBlocks; i < storageInfos2.length; i++) {
    info.addStorage(storageInfos2[i], blocks[i % totalBlocks]);
    Assert.assertEquals(i + 1, info.getCapacity());
    Assert.assertEquals(i + 1, info.numNodes());
    indices = (byte[]) Whitebox.getInternalState(info, "indices");
    Assert.assertEquals(i + 1, indices.length);
  }
  for (i = totalBlocks; i < storageInfos2.length; i++) {
    int index = info.findStorageInfo(storageInfos2[i]);
    Assert.assertEquals(i++, index);
    Assert.assertEquals(index - totalBlocks, indices[index]);
  }
}