Search in sources :

Example 16 with ByteSequence

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);
        }
    }
}
Also used : Watch(io.etcd.jetcd.Watch) Watcher(io.etcd.jetcd.Watch.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) KV(io.etcd.jetcd.KV) Client(io.etcd.jetcd.Client) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 17 with ByteSequence

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();
}
Also used : KV(io.etcd.jetcd.KV) Arrays(java.util.Arrays) Awaitility.await(org.awaitility.Awaitility.await) Client(io.etcd.jetcd.Client) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EtcdClusterExtension(io.etcd.jetcd.test.EtcdClusterExtension) Watch(io.etcd.jetcd.Watch) File(java.io.File) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ByteSequence(io.etcd.jetcd.ByteSequence) Permission(io.etcd.jetcd.auth.Permission) Timeout(org.junit.jupiter.api.Timeout) ExecutorService(java.util.concurrent.ExecutorService) Client(io.etcd.jetcd.Client) File(java.io.File) ByteSequence(io.etcd.jetcd.ByteSequence)

Example 18 with ByteSequence

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();
}
Also used : NotLeaderException(io.etcd.jetcd.election.NotLeaderException) KeyValue(io.etcd.jetcd.KeyValue) GetOption(io.etcd.jetcd.options.GetOption) LeaderKey(io.etcd.jetcd.election.LeaderKey) ExecutionException(java.util.concurrent.ExecutionException) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) Test(org.junit.jupiter.api.Test)

Example 19 with ByteSequence

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CampaignResponse(io.etcd.jetcd.election.CampaignResponse) LeaderResponse(io.etcd.jetcd.election.LeaderResponse) Election(io.etcd.jetcd.Election) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) Test(org.junit.jupiter.api.Test)

Example 20 with ByteSequence

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);
    }
}
Also used : NoLeaderException(io.etcd.jetcd.election.NoLeaderException) ExecutionException(java.util.concurrent.ExecutionException) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) Test(org.junit.jupiter.api.Test)

Aggregations

ByteSequence (io.etcd.jetcd.ByteSequence)40 Test (org.junit.jupiter.api.Test)24 TestUtil.randomByteSequence (io.etcd.jetcd.impl.TestUtil.randomByteSequence)17 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 Watcher (io.etcd.jetcd.Watch.Watcher)12 Client (io.etcd.jetcd.Client)11 WatchResponse (io.etcd.jetcd.watch.WatchResponse)8 MethodSource (org.junit.jupiter.params.provider.MethodSource)8 GetResponse (io.etcd.jetcd.kv.GetResponse)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Watch (io.etcd.jetcd.Watch)6 TxnResponse (io.etcd.jetcd.kv.TxnResponse)6 Cmp (io.etcd.jetcd.op.Cmp)6 Txn (io.etcd.jetcd.Txn)5 CampaignResponse (io.etcd.jetcd.election.CampaignResponse)5 LeaderResponse (io.etcd.jetcd.election.LeaderResponse)4 GetOption (io.etcd.jetcd.options.GetOption)4 WatchOption (io.etcd.jetcd.options.WatchOption)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Election (io.etcd.jetcd.Election)3