Search in sources :

Example 6 with Promise

use of services.moleculer.Promise in project moleculer-java by moleculer-java.

the class CircuitBreakerTest method testRoundRobin2.

@Test
public void testRoundRobin2() throws Exception {
    // Node0 -> fail
    currentID = 1;
    for (int i = 0; i < 30; i++) {
        Promise p = br.call("test.test", (Tree) null);
        String nodeID = getCurrentID();
        boolean success = !"node0".equals(nodeID);
        createResponse(success);
        boolean ok = false;
        try {
            p.waitFor();
            ok = true;
        } catch (Exception e) {
            ok = false;
        }
        assertEquals(success, ok);
    }
    // Check ErrorCounter
    long now = System.currentTimeMillis();
    ErrorCounter ec = cb.errorCounters.get(new EndpointKey("node0", "test.test"));
    assertFalse(ec.isAvailable(now));
    // Do not invoke node0
    for (int i = 0; i < 20; i++) {
        Promise p = br.call("test.test", (Tree) null);
        String nodeID = createResponse(true);
        assertFalse("node0".equals(nodeID));
        boolean ok = false;
        try {
            p.waitFor();
            ok = true;
        } catch (Exception e) {
            ok = false;
        }
        assertTrue(ok);
    }
    // Invoke node1 directly
    Promise p = br.call("test.test", (Tree) null, CallOptions.nodeID("node1"));
    createResponse(true);
    boolean ok = false;
    try {
        p.waitFor();
        ok = true;
    } catch (Exception e) {
        ok = false;
    }
    assertTrue(ok);
    assertFalse(ec.isAvailable(now));
    // Invoke node0 directly
    p = br.call("test.test", (Tree) null, CallOptions.nodeID("node0"));
    createResponse(true);
    try {
        p.waitFor();
        ok = true;
    } catch (Exception e) {
        ok = false;
    }
    assertTrue(ok);
    assertTrue(ec.isAvailable(now));
}
Also used : Promise(services.moleculer.Promise) Tree(io.datatree.Tree) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 7 with Promise

use of services.moleculer.Promise in project moleculer-java by moleculer-java.

the class CircuitBreakerTest method testRetryWithError.

@Test
public void testRetryWithError() throws Exception {
    for (int i = 0; i < 10; i++) {
        Promise p = br.call("test.test", (Tree) null, CallOptions.retryCount(1));
        String n1 = createResponse(false);
        String n2 = createResponse(false);
        assertFalse(n1.equals(n2));
        boolean ok = false;
        try {
            p.waitFor();
            ok = true;
        } catch (Exception e) {
            ok = false;
        }
        assertFalse(ok);
    }
}
Also used : Promise(services.moleculer.Promise) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 8 with Promise

use of services.moleculer.Promise in project moleculer-java by moleculer-java.

the class RedisGetSetClient method clean.

/**
 * Deletes a group of items. Removes every key by a match string.
 *
 * @param match
 */
public final Promise clean(String match) {
    ScanArgs args = new ScanArgs();
    args.limit(100);
    boolean singleStar = match.indexOf('*') > -1;
    boolean doubleStar = match.contains("**");
    if (doubleStar) {
        args.match(match.replace("**", "*"));
    } else {
        args.match(match);
    }
    if (!singleStar || doubleStar) {
        match = null;
    }
    if (client != null) {
        return new Promise(clean(client.scan(args), args, match));
    }
    if (clusteredClient != null) {
        return new Promise(clean(clusteredClient.scan(args), args, match));
    }
    return Promise.resolve();
}
Also used : Promise(services.moleculer.Promise) ScanArgs(com.lambdaworks.redis.ScanArgs)

Example 9 with Promise

use of services.moleculer.Promise in project moleculer-java by moleculer-java.

the class CircuitBreakerTest method testRetry.

@Test
public void testRetry() throws Exception {
    for (int i = 0; i < 10; i++) {
        Promise p = br.call("test.test", (Tree) null, CallOptions.retryCount(1));
        String n1 = createResponse(false);
        String n2 = createResponse(true);
        assertFalse(n1.equals(n2));
        boolean ok = false;
        try {
            p.waitFor();
            ok = true;
        } catch (Exception e) {
            ok = false;
        }
        assertTrue(ok);
    }
}
Also used : Promise(services.moleculer.Promise) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 10 with Promise

use of services.moleculer.Promise in project moleculer-java by moleculer-java.

the class MqttTransporter method subscribe.

@Override
public Promise subscribe(String channel) {
    Promise promise = new Promise();
    if (client != null) {
        try {
            client.subscribe(new Subscription[] { new Subscription(channel, qos) });
            subscriptions.put(channel, promise);
        } catch (Exception cause) {
            promise.complete(cause);
        }
    } else {
        promise.complete(new Throwable("Not connected!"));
    }
    return promise;
}
Also used : Promise(services.moleculer.Promise) Subscription(net.sf.xenqtt.client.Subscription)

Aggregations

Promise (services.moleculer.Promise)14 Tree (io.datatree.Tree)6 TimeoutException (java.util.concurrent.TimeoutException)5 ExecutionException (java.util.concurrent.ExecutionException)4 LinkedList (java.util.LinkedList)3 Test (org.junit.Test)3 Context (services.moleculer.context.Context)3 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 Objects (java.util.Objects)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ServiceBroker (services.moleculer.ServiceBroker)2 ServiceBrokerConfig (services.moleculer.config.ServiceBrokerConfig)2 CallOptions (services.moleculer.context.CallOptions)2 ContextFactory (services.moleculer.context.ContextFactory)2 ScanArgs (com.lambdaworks.redis.ScanArgs)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Annotation (java.lang.annotation.Annotation)1