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));
}
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);
}
}
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();
}
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);
}
}
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;
}
Aggregations