@Test
public void testReacquireContainer() throws ResourceHandlerException {

  Container c0 = mockContainer(0, 2, "GEMM");
  List<FpgaDevice> assigned = new ArrayList<>();
  assigned.add(new FpgaDevice(
      vendorType, 247, 0, "acl0"));
  assigned.add(new FpgaDevice(
      vendorType, 247, 1, "acl1"));
  // Mock we've stored the c0 states
  mockStateStoreForContainer(c0, assigned);
  // NM start
  configuration.set(YarnConfiguration.NM_FPGA_ALLOWED_DEVICES, "0,1,2");
  fpgaResourceHandler.bootstrap(configuration);
  Assert.assertEquals(0,
      fpgaResourceHandler.getFpgaAllocator().getUsedFpgaCount());
  Assert.assertEquals(3,
      fpgaResourceHandler.getFpgaAllocator().getAvailableFpgaCount());
  // Case 1. try recover state for id-0 container
  fpgaResourceHandler.reacquireContainer(getContainerId(0));
  // minor number matches
  List<FpgaDevice> used =
      fpgaResourceHandler.getFpgaAllocator().
      getUsedFpga().get(getContainerId(0).toString());
  int count = 0;
  for (FpgaDevice device : used) {
    if (device.getMinor() == 0){
      count++;
    }
    if (device.getMinor() == 1) {
      count++;
    }
  }
  Assert.assertEquals("Unexpected used minor number in allocator",2, count);
  List<FpgaDevice> available =
      fpgaResourceHandler
          .getFpgaAllocator()
          .getAvailableFpga()
          .get(vendorType);
  count = 0;
  for (FpgaDevice device : available) {
    if (device.getMinor() == 2) {
      count++;
    }
  }
  Assert.assertEquals("Unexpected available minor number in allocator",
      1, count);
  // Case 2. Recover a not allowed device with minor number 5
  Container c1 = mockContainer(1, 1, "GEMM");
  assigned = new ArrayList<>();
  assigned.add(new FpgaDevice(
      vendorType, 247, 5, "acl0"));
  // Mock we've stored the c1 states
  mockStateStoreForContainer(c1, assigned);
  boolean flag = false;
  try {
    fpgaResourceHandler.reacquireContainer(getContainerId(1));
  } catch (ResourceHandlerException e) {
    flag = true;
  }
  Assert.assertTrue(flag);
  Assert.assertEquals(2,
      fpgaResourceHandler.getFpgaAllocator().getUsedFpgaCount());
  Assert.assertEquals(1,
      fpgaResourceHandler.getFpgaAllocator().getAvailableFpgaCount());
  // Case 3. recover a already used device by other container
  Container c2 = mockContainer(2, 1, "GEMM");
  assigned = new ArrayList<>();
  assigned.add(new FpgaDevice(
      vendorType, 247, 1, "acl0"));
  // Mock we've stored the c2 states
  mockStateStoreForContainer(c2, assigned);
  flag = false;
  try {
    fpgaResourceHandler.reacquireContainer(getContainerId(2));
  } catch (ResourceHandlerException e) {
    flag = true;
  }
  Assert.assertTrue(flag);
  Assert.assertEquals(2,
      fpgaResourceHandler.getFpgaAllocator().getUsedFpgaCount());
  Assert.assertEquals(1,
      fpgaResourceHandler.getFpgaAllocator().getAvailableFpgaCount());
  // Case 4. recover a normal container c3 with remaining minor device number 2
  Container c3 = mockContainer(3, 1, "GEMM");
  assigned = new ArrayList<>();
  assigned.add(new FpgaDevice(
      vendorType, 247, 2, "acl2"));
  // Mock we've stored the c2 states
  mockStateStoreForContainer(c3, assigned);
  fpgaResourceHandler.reacquireContainer(getContainerId(3));
  Assert.assertEquals(3,
      fpgaResourceHandler.getFpgaAllocator().getUsedFpgaCount());
  Assert.assertEquals(0,
      fpgaResourceHandler.getFpgaAllocator().getAvailableFpgaCount());
}