use of io.mantisrx.runtime.source.http.impl.ServerClientContext in project mantis by Netflix.
the class ClientResumePoliciesTest method testDelayedOnError.
@Test
public void testDelayedOnError() throws Exception {
final AtomicLong start = new AtomicLong();
final AtomicLong end = new AtomicLong();
final long delay = 100;
final int repeat = 20;
final CountDownLatch done = new CountDownLatch(repeat);
ClientResumePolicy<String, String> policy = ClientResumePolicies.delayed(new Func0<Long>() {
@Override
public Long call() {
return delay;
}
}, TimeUnit.MILLISECONDS);
ServerClientContext<String, String> context = new ServerClientContext<>(new ServerInfo("localhost", 1000), client, factory, observer);
start.set(System.currentTimeMillis());
end.set(0);
for (int i = 0; i < repeat; ++i) {
Observable<HttpClientResponse<String>> resumedOnCompleted = policy.onError(context, i, new Throwable("error"));
resumedOnCompleted.subscribe(new Subscriber<HttpClientResponse<String>>() {
@Override
public void onCompleted() {
end.getAndAdd(System.currentTimeMillis() - start.get());
done.countDown();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(HttpClientResponse<String> stringHttpClientResponse) {
}
});
}
long wait = 5;
if (!done.await(5, TimeUnit.SECONDS)) {
fail("It should take far less than " + wait + " seconds to run the test. ");
}
long elapsed = end.get();
long maxDelay = delay + delay / 2;
assertTrue(String.format("The delay should be more than %d millionseconds, but no more than %d millionseconds. The actual: %d", repeat * delay, repeat * maxDelay, elapsed), elapsed >= repeat * delay && elapsed <= repeat * maxDelay);
}
use of io.mantisrx.runtime.source.http.impl.ServerClientContext in project mantis by Netflix.
the class ClientResumePoliciesTest method testMaxCombinator.
@Test
public void testMaxCombinator() throws Exception {
final AtomicLong start = new AtomicLong();
final AtomicLong end = new AtomicLong();
final long delay = 100;
final int repeat = 20;
final CountDownLatch done = new CountDownLatch(repeat);
ClientResumePolicy<String, String> delayedPolicy = ClientResumePolicies.delayed(new Func0<Long>() {
@Override
public Long call() {
return delay;
}
}, TimeUnit.MILLISECONDS);
ClientResumePolicy<String, String> policy = ClientResumePolicies.maxRepeat(delayedPolicy, repeat);
ServerClientContext<String, String> context = new ServerClientContext<>(new ServerInfo("localhost", 1000), client, factory, observer);
start.set(System.currentTimeMillis());
end.set(0);
for (int i = 0; i < repeat; ++i) {
Observable<HttpClientResponse<String>> resumedOnCompleted = policy.onCompleted(context, i);
Observable<HttpClientResponse<String>> resumedOnError = policy.onError(context, i, new Throwable("error"));
if (i <= repeat) {
assertNotNull(resumedOnCompleted);
assertNotNull(resumedOnError);
} else {
assertNull("The resumed on completion should be null as max repeat is passed", resumedOnCompleted);
assertNull("The resumed on error should be null as max repeat is passed", resumedOnError);
}
resumedOnCompleted.subscribe(new Subscriber<HttpClientResponse<String>>() {
@Override
public void onCompleted() {
end.getAndAdd(System.currentTimeMillis() - start.get());
done.countDown();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(HttpClientResponse<String> stringHttpClientResponse) {
}
});
}
long wait = 5;
if (!done.await(5, TimeUnit.SECONDS)) {
fail("It should take far less than " + wait + " seconds to run the test. ");
}
long elapsed = end.get();
long maxDelay = delay + delay / 2;
assertTrue(String.format("The delay should be more than %d milliseconds, but no more than %d milliseconds. The actual: %d", repeat * delay, repeat * maxDelay, elapsed), elapsed >= repeat * delay && elapsed <= repeat * maxDelay);
}
use of io.mantisrx.runtime.source.http.impl.ServerClientContext in project mantis by Netflix.
the class ClientResumePoliciesTest method testMaxRepeatOnCompletionAndError.
@Test
public void testMaxRepeatOnCompletionAndError() throws Exception {
int max = 10;
ClientResumePolicy<String, String> policy = ClientResumePolicies.maxRepeat(max);
ServerClientContext<String, String> context = new ServerClientContext<>(new ServerInfo("localhost", 1000), client, factory, observer);
for (int i = 0; i < 20; ++i) {
Observable<HttpClientResponse<String>> resumedOnCompleted = policy.onCompleted(context, i);
Observable<HttpClientResponse<String>> resumedOnError = policy.onError(context, i, new Throwable("error"));
if (i <= max) {
assertNotNull(resumedOnCompleted);
assertEquals(RESPONSE_CONTENT, resumedOnCompleted.toBlocking().first().getContent().toBlocking().first());
assertNotNull(resumedOnError);
assertEquals(RESPONSE_CONTENT, resumedOnError.toBlocking().first().getContent().toBlocking().first());
} else {
assertNull("The resumed on completion should be null as max repeat is passed", resumedOnCompleted);
assertNull("The resumed on error should be null as max repeat is passed", resumedOnError);
}
}
}
use of io.mantisrx.runtime.source.http.impl.ServerClientContext in project mantis by Netflix.
the class ClientResumePoliciesTest method testDelayedOnCompletion.
@Test
public void testDelayedOnCompletion() throws Exception {
final AtomicLong start = new AtomicLong();
final AtomicLong end = new AtomicLong();
final long delay = 100;
final int repeat = 20;
final CountDownLatch done = new CountDownLatch(repeat);
ClientResumePolicy<String, String> policy = ClientResumePolicies.delayed(new Func0<Long>() {
@Override
public Long call() {
return delay;
}
}, TimeUnit.MILLISECONDS);
ServerClientContext<String, String> context = new ServerClientContext<>(new ServerInfo("localhost", 1000), client, factory, observer);
start.set(System.currentTimeMillis());
end.set(0);
for (int i = 0; i < repeat; ++i) {
Observable<HttpClientResponse<String>> resumedOnCompleted = policy.onCompleted(context, i);
resumedOnCompleted.subscribe(new Subscriber<HttpClientResponse<String>>() {
@Override
public void onCompleted() {
end.getAndAdd(System.currentTimeMillis() - start.get());
done.countDown();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(HttpClientResponse<String> stringHttpClientResponse) {
}
});
}
long wait = 5;
if (!done.await(5, TimeUnit.SECONDS)) {
fail("It should take far less than " + wait + " seconds to run the test. ");
}
long elapsed = end.get();
long maxDelay = delay + delay / 2;
assertTrue(String.format("The delay should be more than %d milliseconds, but no more than %d milliseconds. The actual: %d", repeat * delay, repeat * maxDelay, elapsed), elapsed >= repeat * delay && elapsed <= repeat * maxDelay);
}
Aggregations