Java Code Examples for mousio.etcd4j.promises.EtcdResponsePromise#get()

The following examples show how to use mousio.etcd4j.promises.EtcdResponsePromise#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EtcdClientRetryPolicyTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuccessWithoutRetrying() throws Exception {

    whenHttp(server)
            .match(get("/v2/keys/foo"))
            .then(SUCCESS);

    try (EtcdClient etcd = new EtcdClient(serverURI)) {

        EtcdResponsePromise<EtcdKeysResponse> promise = etcd.get("foo")
                .setRetryPolicy(new RetryNTimes(1, 10))
                .send();

        EtcdKeysResponse resp = promise.get();

        assertThat(resp.node.value).isEqualTo("bar");
        assertThat(promise.getException()).isNull();

    }

    verifyHttp(server).once(
            method(Method.GET),
            uri("/v2/keys/foo")
    );
}
 
Example 2
Source File: EtcdClientRetryPolicyTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuccessAfterRetrying() throws Exception {

    whenHttp(server)
            .match(get("/v2/keys/foo"))
            .then(sequence(FAILURE, SUCCESS));

    try (EtcdClient etcd = new EtcdClient(serverURI)) {

        EtcdResponsePromise<EtcdKeysResponse> promise = etcd.get("foo")
                .setRetryPolicy(new RetryNTimes(1, 10))
                .send();

        EtcdKeysResponse resp = promise.get();

        assertThat(resp.node.value).isEqualTo("bar");
        assertThat(promise.getException()).isNull();

    }

    verifyHttp(server).times(2,
            method(Method.GET),
            uri("/v2/keys/foo")
    );
}
 
Example 3
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * In order key tests
 */
@Test
public void testWait() throws IOException, EtcdException, EtcdAuthenticationException, InterruptedException, TimeoutException {
  EtcdResponsePromise<EtcdKeysResponse> p = etcd.get("etcd4j_test/test").waitForChange().send();

  // Ensure the change is received after the listen command is received.
  new Timer().schedule(new TimerTask() {
    @Override
    public void run() {
      try {
        etcd.put("etcd4j_test/test", "changed").send().get();
      } catch (IOException | EtcdException | EtcdAuthenticationException | TimeoutException e) {
        fail();
      }
    }
  }, 20);

  EtcdKeysResponse r = p.get();
  assertEquals("changed", r.node.value);
}
 
Example 4
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testIfCleanClose() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  etcd.setRetryHandler(new RetryWithExponentialBackOff(20, 4, 1000));

  EtcdResponsePromise<EtcdKeysResponse> p = etcd.get("etcd4j_test/test").waitForChange().send();
  etcd.close();

  try {
    p.get();
    fail();
  } catch (IOException e){
    // should be catched because connection was canceled
    if (!(e.getCause() instanceof CancellationException)) {
      fail();
    }
  }
}
 
Example 5
Source File: EtcdClientWrapper.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@Override
public KvNode set(String key, String value) {
    try {
        EtcdResponsePromise<EtcdKeysResponse> send = etcd.put(key, value).send();
        EtcdKeysResponse resp = send.get();
        log.debug("set value {} for key {}", resp.node.value, resp.node.key);
        return toNode(resp);
    } catch (Exception e) {
        throw Throwables.asRuntime(e);
    }
}
 
Example 6
Source File: EtcdFlagFieldUpdater.java    From java-flagz with MIT License 5 votes vote down vote up
private List<EtcdNode> fetchAllFlagzNodes() {
  try {
    EtcdResponsePromise<EtcdKeysResponse> promise = client
        .get(this.directoryPrefix)
        .dir()
        .send();
    EtcdKeysResponse response = promise.get();
    // NOTE: We use etcdIndex here, because we know we got latest data up to this point.
    lastKnownFlagzModificationIndex = response.etcdIndex;
    return MoreObjects.firstNonNull(response.node.nodes, ImmutableList.<EtcdNode>of());
  } catch (IOException | EtcdException
      | TimeoutException | EtcdAuthenticationException exception) {
    throw new EtcdFlagFieldUpdaterException.EtcdFetchingFailed(exception);
  }
}
 
Example 7
Source File: ITEtcdConfigurationTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private boolean publishConfig(String key, String group, String value) {
    try {
        client.putDir(group).send().get();
        EtcdResponsePromise<EtcdKeysResponse> promise = client.put(generateKey(key, group), value).send();
        promise.get();
        return true;
    } catch (Exception e) {
        return false;
    }
}
 
Example 8
Source File: EtcdCoordinator.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void registerRemote(RemoteInstance remoteInstance) throws ServiceRegisterException {

    if (needUsingInternalAddr()) {
        remoteInstance = new RemoteInstance(new Address(config.getInternalComHost(), config.getInternalComPort(), true));
    }

    this.selfAddress = remoteInstance.getAddress();
    TelemetryRelatedContext.INSTANCE.setId(selfAddress.toString());

    EtcdEndpoint endpoint = new EtcdEndpoint.Builder().serviceName(serviceName)
                                                      .host(selfAddress.getHost())
                                                      .port(selfAddress.getPort())
                                                      .build();
    try {
        client.putDir(serviceName).send();
        String key = buildKey(serviceName, selfAddress, remoteInstance);
        String json = new Gson().toJson(endpoint);
        EtcdResponsePromise<EtcdKeysResponse> promise = client.put(key, json).ttl(KEY_TTL).send();
        //check register.
        promise.get();
        renew(client, key, json);
    } catch (Exception e) {
        throw new ServiceRegisterException(e.getMessage());
    }

}
 
Example 9
Source File: EtcdClientRetryPolicyTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailureWithoutRetrying() throws Exception {

    whenHttp(server)
            .match(get("/v2/keys/foo"))
            .then(FAILURE);

    try (EtcdClient etcd = new EtcdClient(serverURI)) {

        EtcdResponsePromise<EtcdKeysResponse> promise = etcd.get("foo")
                .setRetryPolicy(new RetryNTimes(1, 0))
                .send();

        Exception err = null;

        try {
            promise.get();
        } catch (Exception e) {
            err = e;
        }

        assertThat(err).isNotNull();
        assertThat(err).isEqualTo(promise.getException());

    }

    verifyHttp(server).once(
            method(Method.GET),
            uri("/v2/keys/foo")
    );

}
 
Example 10
Source File: EtcdClientRetryPolicyTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailureAfterRetrying() throws Exception {

    whenHttp(server)
            .match(get("/v2/keys/foo"))
            .then(FAILURE);

    try (EtcdClient etcd = new EtcdClient(serverURI)) {

        EtcdResponsePromise<EtcdKeysResponse> promise = etcd.get("foo")
                .setRetryPolicy(new RetryNTimes(1, 3))
                .send();

        Exception err = null;

        try {
            promise.get();
        } catch (Exception e) {
            err = e;
        }

        assertThat(err).isNotNull();
        assertThat(err).isEqualTo(promise.getException());

    }

    verifyHttp(server).times(4,
            method(Method.GET),
            uri("/v2/keys/foo")
    );
}
 
Example 11
Source File: EtcdClientWrapper.java    From haven-platform with Apache License 2.0 4 votes vote down vote up
private EtcdKeysResponse executeRequest(EtcdKeyRequest req) throws Exception {
    EtcdResponsePromise<EtcdKeysResponse> send = req.send();
    return send.get();
}