use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class WatchResumeTest method testWatchOnPut.
@Test
public void testWatchOnPut() throws Exception {
try (Client client = TestUtil.client(cluster).build()) {
Watch watchClient = client.getWatchClient();
KV kvClient = client.getKVClient();
final ByteSequence key = TestUtil.randomByteSequence();
final ByteSequence value = TestUtil.randomByteSequence();
final AtomicReference<WatchResponse> ref = new AtomicReference<>();
try (Watcher watcher = watchClient.watch(key, ref::set)) {
cluster.restart();
kvClient.put(key, value).get(1, TimeUnit.SECONDS);
await().atMost(30, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
assertThat(ref.get().getEvents().size()).isEqualTo(1);
assertThat(ref.get().getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
assertThat(ref.get().getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
}
}
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class WatchTokenExpireTest method setUpEnvironment.
private void setUpEnvironment() throws Exception {
final File caFile = new File(Objects.requireNonNull(getClass().getResource("/ssl/cert/ca.pem")).toURI());
Client client = TestUtil.client(cluster).authority("etcd0").sslContext(b -> b.trustManager(caFile)).build();
// enable authentication to enforce usage of access token
ByteSequence role = TestUtil.bytesOf("root");
client.getAuthClient().roleAdd(role).get();
client.getAuthClient().userAdd(user, password).get();
// grant access only to given key
client.getAuthClient().roleGrantPermission(role, key, key, Permission.Type.READWRITE).get();
client.getAuthClient().userGrantRole(user, role).get();
client.getAuthClient().authEnable().get();
client.close();
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class ElectionTest method testProposeValueNotBeingLeader.
@Test
public void testProposeValueNotBeingLeader() throws Exception {
ByteSequence electionName = ByteSequence.from(randomString(), StandardCharsets.UTF_8);
LeaderKey leaderKey = new LeaderKey(electionName, randomByteSequence(), 1, 1);
ByteSequence proposal = ByteSequence.from("proposal", StandardCharsets.UTF_8);
try {
electionClient.proclaim(leaderKey, proposal).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
fail("Cannot proclaim proposal not being a leader");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(NotLeaderException.class);
}
GetOption getOption = GetOption.newBuilder().isPrefix(true).build();
List<KeyValue> keys = kvClient.get(electionName, getOption).get().getKvs();
assertThat(keys).isEmpty();
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class ElectionTest method testObserveElections.
@Test
public void testObserveElections() throws Exception {
int electionCount = 3;
final AtomicInteger electionsSeen = new AtomicInteger(0);
ByteSequence electionName = ByteSequence.from(randomString(), StandardCharsets.UTF_8);
electionClient.observe(electionName, new Election.Listener() {
@Override
public void onNext(LeaderResponse response) {
electionsSeen.incrementAndGet();
}
@Override
public void onError(Throwable error) {
}
@Override
public void onCompleted() {
}
});
long leaseId = leaseClient.grant(10).get().getID();
for (int i = 0; i < electionCount; ++i) {
ByteSequence proposal = ByteSequence.from(randomString(), StandardCharsets.UTF_8);
CampaignResponse campaignResponse = electionClient.campaign(electionName, leaseId, proposal).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
Thread.sleep(100);
electionClient.resign(campaignResponse.getLeader()).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
}
TestUtil.waitForCondition(() -> electionsSeen.get() == electionCount, OPERATION_TIMEOUT * 1000, "Observer did not receive expected notifications, got: " + electionsSeen.get());
leaseClient.revoke(leaseId).get();
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class ElectionTest method testEmptyElection.
@Test
public void testEmptyElection() throws Exception {
ByteSequence electionName = ByteSequence.from(randomString(), StandardCharsets.UTF_8);
try {
electionClient.leader(electionName).get(OPERATION_TIMEOUT, TimeUnit.SECONDS);
fail("etcd communicates missing leader with error");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(NoLeaderException.class);
}
}
Aggregations