// through the entire process to create the ID.
// Then A attempts to go through the process and should discover that the
// ID has already been assigned.
uid = new UniqueId(client, table, kind, 3); // Used by client A.
HBaseClient client_b = mock(HBaseClient.class); // For client B.
final UniqueId uid_b = new UniqueId(client_b, table, kind, 3);
final byte[] id = { 0, 0, 5 };
final byte[] byte_name = { 'f', 'o', 'o' };
final ArrayList<KeyValue> kvs = new ArrayList<KeyValue>(1);
kvs.add(new KeyValue(byte_name, ID, kind_array, id));
@SuppressWarnings("unchecked")
final Deferred<ArrayList<KeyValue>> d = PowerMockito.spy(new Deferred<ArrayList<KeyValue>>());
when(client.get(anyGet()))
.thenReturn(d)
.thenReturn(Deferred.fromResult(kvs));
final Answer<byte[]> the_race = new Answer<byte[]>() {
public byte[] answer(final InvocationOnMock unused_invocation) throws Exception {
// While answering A's first Get, B doest a full getOrCreateId.
assertArrayEquals(id, uid_b.getOrCreateId("foo"));
d.callback(null);
return (byte[]) ((Deferred) d).join();
}
};
// Start the race when answering A's first Get.
try {
PowerMockito.doAnswer(the_race).when(d).joinUninterruptibly();
} catch (Exception e) {
fail("Should never happen: " + e);
}
when(client_b.get(anyGet())) // null => ID doesn't exist.
.thenReturn(Deferred.<ArrayList<KeyValue>>fromResult(null));
// Watch this! ______,^ I'm writing C++ in Java!
when(client_b.atomicIncrement(incrementForRow(MAXID)))
.thenReturn(Deferred.fromResult(5L));
when(client_b.compareAndSet(anyPut(), emptyArray()))
.thenReturn(Deferred.fromResult(true))
.thenReturn(Deferred.fromResult(true));
// Now that B is finished, A proceeds and allocates a UID that will be
// wasted, and creates the reverse mapping, but fails at creating the