use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class ElectionTest method testSynchronizationBarrier.
@Test
public void testSynchronizationBarrier() throws Exception {
final int threadCount = 5;
final Random random = new Random();
ByteSequence electionName = ByteSequence.from(randomString(), StandardCharsets.UTF_8);
final AtomicInteger sharedVariable = new AtomicInteger(0);
// create separate clients so they will compete for access to shared resource
List<Client> clients = new ArrayList<>(threadCount);
List<Long> leases = new ArrayList<>(threadCount);
for (int i = 0; i < threadCount; ++i) {
Client client = TestUtil.client(cluster).build();
long leaseId = client.getLeaseClient().grant(100).get().getID();
clients.add(client);
leases.add(leaseId);
}
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
List<Future<?>> futures = new ArrayList<>(threadCount);
for (int i = 0; i < threadCount; ++i) {
final int id = i;
final ByteSequence proposal = ByteSequence.from(Integer.toString(id), StandardCharsets.UTF_8);
futures.add(executor.submit(() -> {
try {
Election electionClient = clients.get(id).getElectionClient();
CampaignResponse campaignResponse = electionClient.campaign(electionName, leases.get(id), proposal).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
int localCopy = sharedVariable.get();
Thread.sleep(200 + random.nextInt(300));
sharedVariable.set(localCopy + 1);
electionClient.resign(campaignResponse.getLeader()).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
} catch (Exception e) {
fail("Unexpected error in thread {}: {}", id, e);
}
}));
}
executor.shutdown();
executor.awaitTermination(threadCount * OPERATION_TIMEOUT, TimeUnit.SECONDS);
futures.forEach(f -> assertThat(f).isDone());
assertThat(sharedVariable.get()).isEqualTo(threadCount);
GetOption getOption = GetOption.newBuilder().isPrefix(true).build();
assertThat(kvClient.get(electionName, getOption).get().getCount()).isEqualTo(0L);
for (int i = 0; i < threadCount; ++i) {
clients.get(i).getLeaseClient().revoke(leases.get(i)).get();
clients.get(i).close();
}
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class KVTest method testTxnForCmpOpNotEqual.
@Test
public void testTxnForCmpOpNotEqual() throws Exception {
ByteSequence sampleKey = bytesOf("txn_key");
ByteSequence sampleValue = bytesOf("xyz");
ByteSequence cmpValue = bytesOf("abc");
ByteSequence putValue = bytesOf("XYZ");
ByteSequence putValueNew = bytesOf("ABC");
// put the original txn key value pair
kvClient.put(sampleKey, sampleValue).get();
// construct txn operation
Txn txn = kvClient.txn();
Cmp cmp = new Cmp(sampleKey, Cmp.Op.NOT_EQUAL, CmpTarget.value(cmpValue));
CompletableFuture<io.etcd.jetcd.kv.TxnResponse> txnResp = txn.If(cmp).Then(Op.put(sampleKey, putValue, PutOption.DEFAULT)).Else(Op.put(sampleKey, putValueNew, PutOption.DEFAULT)).commit();
txnResp.get();
// get the value
GetResponse getResp = kvClient.get(sampleKey).get();
assertThat(getResp.getKvs()).hasSize(1);
assertThat(getResp.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(putValue.toString(UTF_8));
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class KVTest method testByteSequence.
@Test
public void testByteSequence() {
ByteSequence prefix = bytesOf("/test-service/");
ByteSequence subPrefix = bytesOf("uuids/");
String keyString = randomString();
ByteSequence key = bytesOf(keyString);
ByteSequence prefixedKey = prefix.concat(subPrefix).concat(key);
assertThat(prefixedKey.startsWith(prefix)).isTrue();
assertThat(prefixedKey.substring(prefix.size() + subPrefix.size()).toString(UTF_8)).isEqualTo(keyString);
assertThat(prefixedKey.substring(prefix.size(), prefix.size() + subPrefix.size())).isEqualTo(subPrefix);
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class KVTest method testNestedTxn.
@Test
public void testNestedTxn() throws Exception {
ByteSequence foo = bytesOf("txn_foo");
ByteSequence bar = bytesOf("txn_bar");
ByteSequence barz = bytesOf("txn_barz");
ByteSequence abc = bytesOf("txn_abc");
ByteSequence oneTwoThree = bytesOf("txn_123");
Txn txn = kvClient.txn();
Cmp cmp = new Cmp(foo, Cmp.Op.EQUAL, CmpTarget.version(0));
CompletableFuture<io.etcd.jetcd.kv.TxnResponse> txnResp = txn.If(cmp).Then(Op.put(foo, bar, PutOption.DEFAULT), Op.txn(null, new Op[] { Op.put(abc, oneTwoThree, PutOption.DEFAULT) }, null)).Else(Op.put(foo, barz, PutOption.DEFAULT)).commit();
txnResp.get();
GetResponse getResp = kvClient.get(foo).get();
assertThat(getResp.getKvs()).hasSize(1);
assertThat(getResp.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(bar.toString(UTF_8));
GetResponse getResp2 = kvClient.get(abc).get();
assertThat(getResp2.getKvs()).hasSize(1);
assertThat(getResp2.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(oneTwoThree.toString(UTF_8));
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class KVTest method testGetAndDeleteWithPrefix.
@Test
public void testGetAndDeleteWithPrefix() throws Exception {
String prefix = randomString();
ByteSequence key = bytesOf(prefix);
int numPrefixes = 10;
putKeysWithPrefix(prefix, numPrefixes);
// verify get withPrefix.
CompletableFuture<GetResponse> getFuture = kvClient.get(key, GetOption.newBuilder().isPrefix(true).build());
GetResponse getResp = getFuture.get();
assertThat(getResp.getCount()).isEqualTo(numPrefixes);
// verify del withPrefix.
DeleteOption deleteOpt = DeleteOption.newBuilder().isPrefix(true).build();
CompletableFuture<DeleteResponse> delFuture = kvClient.delete(key, deleteOpt);
DeleteResponse delResp = delFuture.get();
assertThat(delResp.getDeleted()).isEqualTo(numPrefixes);
}
Aggregations