Search in sources :

Example 1 with EtcdClient

use of mousio.etcd4j.EtcdClient in project camel by apache.

the class EtcdRoutePolicy method doStart.

@Override
protected void doStart() throws Exception {
    ObjectHelper.notNull(camelContext, "camelContext");
    ObjectHelper.notNull(clientUris, "clientUris");
    if (client == null) {
        client = new EtcdClient(EtcdHelper.resolveURIs(camelContext, clientUris));
        managedClient = true;
    }
    setLeader(tryTakeLeadership());
    watch();
    super.doStart();
}
Also used : EtcdClient(mousio.etcd4j.EtcdClient)

Example 2 with EtcdClient

use of mousio.etcd4j.EtcdClient in project camel by apache.

the class EtcdKeysTest method testKeys.

@Test(expected = EtcdException.class)
public void testKeys() throws Exception {
    final String path = "/camel/" + UUID.randomUUID().toString();
    final String value = UUID.randomUUID().toString();
    final EtcdClient client = getClient();
    final Map<String, Object> headers = new HashMap<>();
    // *******************************************
    // SET
    // *******************************************
    headers.clear();
    headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_SET);
    headers.put(EtcdConstants.ETCD_PATH, path);
    sendBody("direct:keys-set", value, headers);
    MockEndpoint mockSet = getMockEndpoint("mock:result-set");
    mockSet.expectedMinimumMessageCount(1);
    mockSet.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.keys.name());
    mockSet.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);
    mockSet.assertIsSatisfied();
    // *******************************************
    // GET
    // *******************************************
    headers.clear();
    headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_GET);
    headers.put(EtcdConstants.ETCD_PATH, path);
    sendBody("direct:keys-get", value, headers);
    MockEndpoint mockGet = getMockEndpoint("mock:result-get");
    mockGet.expectedMinimumMessageCount(1);
    mockSet.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.keys.name());
    mockGet.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);
    mockGet.expectedMessagesMatches(new Predicate() {

        @Override
        public boolean matches(Exchange exchange) {
            EtcdKeysResponse keysResponse = exchange.getIn().getBody(EtcdKeysResponse.class);
            assertNotNull(keysResponse);
            assertNotNull(keysResponse.node);
            assertNotNull(keysResponse.node.value);
            return keysResponse.node.value.equals(value);
        }
    });
    mockGet.assertIsSatisfied();
    // *******************************************
    // DELETE
    // *******************************************
    headers.clear();
    headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_DELETE);
    headers.put(EtcdConstants.ETCD_PATH, path);
    sendBody("direct:keys-del", "value", headers);
    MockEndpoint mockDel = getMockEndpoint("mock:result-del");
    mockDel.expectedMinimumMessageCount(1);
    mockSet.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.keys.name());
    mockDel.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);
    mockDel.assertIsSatisfied();
    // *******************************************
    // VALIDATION
    // *******************************************
    client.get(path).send().get();
    fail("EtcdException should have been thrown");
}
Also used : Exchange(org.apache.camel.Exchange) EtcdClient(mousio.etcd4j.EtcdClient) HashMap(java.util.HashMap) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) EtcdKeysResponse(mousio.etcd4j.responses.EtcdKeysResponse) Predicate(org.apache.camel.Predicate) Test(org.junit.Test)

Example 3 with EtcdClient

use of mousio.etcd4j.EtcdClient in project camel by apache.

the class EtcdWatchTest method testWatch.

private void testWatch(String mockEndpoint, final String key, int updates) throws Exception {
    final String[] values = new String[updates];
    for (int i = 0; i < updates; i++) {
        values[i] = key + "=myValue-" + i;
    }
    MockEndpoint mock = getMockEndpoint(mockEndpoint);
    mock.expectedMessageCount(2);
    mock.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.watch.name());
    mock.expectedHeaderReceived(EtcdConstants.ETCD_PATH, key);
    mock.expectedBodiesReceived(values);
    final EtcdClient client = getClient();
    for (int i = 0; i < updates; i++) {
        client.put(key, "myValue-" + i).send().get();
    }
    mock.assertIsSatisfied();
}
Also used : EtcdClient(mousio.etcd4j.EtcdClient) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 4 with EtcdClient

use of mousio.etcd4j.EtcdClient in project camel by apache.

the class EtcdWatchTest method testWatchRecovery.

@Test
public void testWatchRecovery() throws Exception {
    final String key = "/myKeyRecovery";
    final EtcdClient client = getClient();
    try {
        // Delete the key if present
        client.delete(key).send().get();
    } catch (EtcdException e) {
        if (!e.isErrorCode(EtcdErrorCode.KeyNotFound)) {
            throw e;
        }
    }
    // Fill the vent backlog ( > 1000)
    for (int i = 0; i < 2000; i++) {
        client.put(key, "v" + i).send().get();
    }
    context().startRoute("watchRecovery");
    testWatch("mock:watch-recovery", key, 10);
}
Also used : EtcdClient(mousio.etcd4j.EtcdClient) EtcdException(mousio.etcd4j.responses.EtcdException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Aggregations

EtcdClient (mousio.etcd4j.EtcdClient)4 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 EtcdException (mousio.etcd4j.responses.EtcdException)1 EtcdKeysResponse (mousio.etcd4j.responses.EtcdKeysResponse)1 Exchange (org.apache.camel.Exchange)1 Predicate (org.apache.camel.Predicate)1