Program: 6449
Project: 6449
Buggy source:
@Test public void testAssignment() throws InterruptedException, ExecutionException {
  CConfiguration cConf=CConfiguration.create();
  cConf.set(Constants.Zookeeper.QUORUM,zkServer.getConnectionStr());
  String serviceName="String_Node_Str";
  Injector injector=Guice.createInjector(new ConfigModule(cConf),new ZKClientModule(),new DiscoveryRuntimeModule().getDistributedModules());
  ZKClientService zkClient=injector.getInstance(ZKClientService.class);
  zkClient.startAndWait();
  DiscoveryService discoveryService=injector.getInstance(DiscoveryService.class);
  try {
    ResourceCoordinator coordinator=new ResourceCoordinator(zkClient,injector.getInstance(DiscoveryServiceClient.class),new BalancedAssignmentStrategy());
    coordinator.startAndWait();
    try {
      ResourceCoordinatorClient client=new ResourceCoordinatorClient(zkClient);
      client.startAndWait();
      try {
        ResourceRequirement requirement=ResourceRequirement.builder(serviceName).addPartitions("String_Node_Str",5,1).build();
        client.submitRequirement(requirement).get();
        Assert.assertEquals(requirement,client.fetchRequirement(requirement.getName()).get());
        final Discoverable discoverable1=createDiscoverable(serviceName,10000);
        Cancellable cancelDiscoverable1=discoveryService.register(ResolvingDiscoverable.of(discoverable1));
        final BlockingQueue<Collection<PartitionReplica>> assignmentQueue=new SynchronousQueue<>();
        final Semaphore finishSemaphore=new Semaphore(0);
        Cancellable cancelSubscribe1=subscribe(client,discoverable1,assignmentQueue,finishSemaphore);
        Collection<PartitionReplica> assigned=assignmentQueue.poll(5,TimeUnit.SECONDS);
        Assert.assertNotNull(assigned);
        Assert.assertEquals(5,assigned.size());
        cancelDiscoverable1.cancel();
        Assert.assertTrue(assignmentQueue.poll(5,TimeUnit.SECONDS).isEmpty());
        cancelDiscoverable1=discoveryService.register(ResolvingDiscoverable.of(discoverable1));
        assigned=assignmentQueue.poll(5,TimeUnit.SECONDS);
        Assert.assertNotNull(assigned);
        Assert.assertEquals(5,assigned.size());
        final Discoverable discoverable2=createDiscoverable(serviceName,10001);
        Cancellable cancelDiscoverable2=discoveryService.register(ResolvingDiscoverable.of(discoverable2));
        assigned=assignmentQueue.poll(5,TimeUnit.SECONDS);
        Assert.assertNotNull(assigned);
        Assert.assertEquals(3,assigned.size());
        cancelDiscoverable1.cancel();
        Assert.assertTrue(assignmentQueue.poll(5,TimeUnit.SECONDS).isEmpty());
        cancelSubscribe1.cancel();
        Assert.assertTrue(finishSemaphore.tryAcquire(2,TimeUnit.SECONDS));
        Cancellable cancelSubscribe2=subscribe(client,discoverable2,assignmentQueue,finishSemaphore);
        assigned=assignmentQueue.poll(5,TimeUnit.SECONDS);
        Assert.assertNotNull(assigned);
        Assert.assertEquals(5,assigned.size());
        client.submitRequirement(ResourceRequirement.builder(serviceName).build());
        Assert.assertTrue(assignmentQueue.poll(5,TimeUnit.SECONDS).isEmpty());
        client.submitRequirement(ResourceRequirement.builder(serviceName).addPartitions("String_Node_Str",1,1).build());
        assigned=assignmentQueue.poll(5,TimeUnit.SECONDS);
        Assert.assertNotNull(assigned);
        Assert.assertEquals(1,assigned.size());
        client.deleteRequirement(requirement.getName());
        Assert.assertTrue(assignmentQueue.poll(5,TimeUnit.SECONDS).isEmpty());
        cancelSubscribe2.cancel();
        Assert.assertTrue(finishSemaphore.tryAcquire(2,TimeUnit.SECONDS));
        cancelDiscoverable2.cancel();
      }
  finally {
        client.stopAndWait();
      }
    }
  finally {
      coordinator.stopAndWait();
    }
  }
  finally {
    zkClient.stopAndWait();
  }
}

Fixed version (ground truth):
@Test public void testAssignment() throws InterruptedException, ExecutionException {
  CConfiguration cConf=CConfiguration.create();
  cConf.set(Constants.Zookeeper.QUORUM,zkServer.getConnectionStr());
  String serviceName="String_Node_Str";
  Injector injector=Guice.createInjector(new ConfigModule(cConf),new ZKClientModule(),new DiscoveryRuntimeModule().getDistributedModules());
  ZKClientService zkClient=injector.getInstance(ZKClientService.class);
  zkClient.startAndWait();
  DiscoveryService discoveryService=injector.getInstance(DiscoveryService.class);
  try {
    ResourceCoordinator coordinator=new ResourceCoordinator(zkClient,injector.getInstance(DiscoveryServiceClient.class),new BalancedAssignmentStrategy());
    coordinator.startAndWait();
    try {
      ResourceCoordinatorClient client=new ResourceCoordinatorClient(zkClient);
      client.startAndWait();
      try {
        ResourceRequirement requirement=ResourceRequirement.builder(serviceName).addPartitions("String_Node_Str",5,1).build();
        client.submitRequirement(requirement).get();
        Assert.assertEquals(requirement,client.fetchRequirement(requirement.getName()).get());
        final Discoverable discoverable1=createDiscoverable(serviceName,10000);
        Cancellable cancelDiscoverable1=discoveryService.register(ResolvingDiscoverable.of(discoverable1));
        final BlockingQueue<Collection<PartitionReplica>> assignmentQueue=new SynchronousQueue<>();
        final Semaphore finishSemaphore=new Semaphore(0);
        Cancellable cancelSubscribe1=subscribe(client,discoverable1,assignmentQueue,finishSemaphore);
        Collection<PartitionReplica> assigned=assignmentQueue.poll(30,TimeUnit.SECONDS);
        Assert.assertNotNull(assigned);
        Assert.assertEquals(5,assigned.size());
        cancelDiscoverable1.cancel();
        Assert.assertTrue(assignmentQueue.poll(30,TimeUnit.SECONDS).isEmpty());
        cancelDiscoverable1=discoveryService.register(ResolvingDiscoverable.of(discoverable1));
        assigned=assignmentQueue.poll(30,TimeUnit.SECONDS);
        Assert.assertNotNull(assigned);
        Assert.assertEquals(5,assigned.size());
        final Discoverable discoverable2=createDiscoverable(serviceName,10001);
        Cancellable cancelDiscoverable2=discoveryService.register(ResolvingDiscoverable.of(discoverable2));
        assigned=assignmentQueue.poll(30,TimeUnit.SECONDS);
        Assert.assertNotNull(assigned);
        Assert.assertEquals(3,assigned.size());
        cancelDiscoverable1.cancel();
        Assert.assertTrue(assignmentQueue.poll(30,TimeUnit.SECONDS).isEmpty());
        cancelSubscribe1.cancel();
        Assert.assertTrue(finishSemaphore.tryAcquire(2,TimeUnit.SECONDS));
        Cancellable cancelSubscribe2=subscribe(client,discoverable2,assignmentQueue,finishSemaphore);
        assigned=assignmentQueue.poll(30,TimeUnit.SECONDS);
        Assert.assertNotNull(assigned);
        Assert.assertEquals(5,assigned.size());
        client.submitRequirement(ResourceRequirement.builder(serviceName).build());
        Assert.assertTrue(assignmentQueue.poll(30,TimeUnit.SECONDS).isEmpty());
        client.submitRequirement(ResourceRequirement.builder(serviceName).addPartitions("String_Node_Str",1,1).build());
        assigned=assignmentQueue.poll(30,TimeUnit.SECONDS);
        Assert.assertNotNull(assigned);
        Assert.assertEquals(1,assigned.size());
        client.deleteRequirement(requirement.getName());
        Assert.assertTrue(assignmentQueue.poll(30,TimeUnit.SECONDS).isEmpty());
        cancelSubscribe2.cancel();
        Assert.assertTrue(finishSemaphore.tryAcquire(2,TimeUnit.SECONDS));
        cancelDiscoverable2.cancel();
      }
  finally {
        client.stopAndWait();
      }
    }
  finally {
      coordinator.stopAndWait();
    }
  }
  finally {
    zkClient.stopAndWait();
  }
}