Skip to content

Commit

Permalink
Support promise handle and callback handle. #763
Browse files Browse the repository at this point in the history
  • Loading branch information
MammatusPlatypus committed Sep 7, 2016
1 parent 95df4fe commit 32e6982
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

import io.advantageous.qbit.service.*;
import io.advantageous.qbit.time.Duration;
import io.advantageous.reakt.CallbackHandle;
import io.advantageous.reakt.promise.Promise;
import io.advantageous.reakt.promise.PromiseHandle;
import io.advantageous.reakt.promise.Promises;
import org.junit.After;
import org.junit.Before;
Expand All @@ -13,6 +15,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -161,6 +164,39 @@ private void testFail(ServiceDiscovery serviceDiscovery) {
}


@Test
public void testServiceWithReturnPromiseHandleFail() {
testFailHandle(serviceDiscovery);
testFailHandle(serviceDiscoveryStrongTyped);
testFailHandle(serviceDiscoveryServiceBundle);
}

private void testFailHandle(ServiceDiscovery serviceDiscovery) {
serviceDiscovery.lookupServiceByPromiseHandle(null).then(this::handleSuccess)
.catchError(this::handleError).invoke();

await();
assertNull("We do not have a return", returnValue.get());
assertNotNull("There were errors", errorRef.get());
}


@Test
public void testServiceWithReturnPromiseHandleSuccess() {
testSuccessHandle(serviceDiscovery);
testSuccessHandle(serviceDiscoveryStrongTyped);
testSuccessHandle(serviceDiscoveryServiceBundle);
}

private void testSuccessHandle(ServiceDiscovery serviceDiscovery) {
serviceDiscovery.lookupServiceByPromiseHandle(URI.create("http://localhost/foo")).then(this::handleSuccess)
.catchError(this::handleError).invoke();

await();
assertNotNull("We do not have a return", returnValue.get());
assertNull("There were errors", errorRef.get());
}

@Test(expected = IllegalStateException.class)
public void testServiceWithReturnPromiseSuccessInvokeTwice() {
final Promise<URI> promise = serviceDiscovery.lookupService(empURI).then(this::handleSuccess)
Expand Down Expand Up @@ -196,6 +232,8 @@ interface ServiceDiscovery {

Promise<Integer> five();


PromiseHandle<URI> lookupServiceByPromiseHandle(final URI uri);
}

public class ServiceDiscoveryImpl {
Expand All @@ -208,6 +246,16 @@ public void lookupService(final io.advantageous.qbit.reactive.Callback<URI> call
}
}

public PromiseHandle<URI> lookupServiceByPromiseHandle(final URI uri) {
return Promises.deferCall(callback -> {
if (uri == null) {
callback.reject("uri can't be null");
} else {
callback.resolve(successResult);
}
});
}

public void ok(final io.advantageous.qbit.reactive.Callback<Boolean> callback) {
callback.resolve(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.advantageous.qbit.reactive.Callback;
import io.advantageous.qbit.service.rest.endpoint.tests.model.Employee;
import io.advantageous.reakt.promise.Promise;
import io.advantageous.reakt.promise.PromiseHandle;
import io.advantageous.reakt.promise.Promises;

import java.util.List;
Expand Down Expand Up @@ -160,4 +161,9 @@ public Promise<Map<String, Employee>> returnMapByPromise() {
return Promises.invokablePromise(promise -> promise.resolve(Maps.map("1", new Employee(1, "Rick"), "2", new Employee(2, "Diana"))));
}

@RequestMapping("/returnMapByPromiseHandle")
public PromiseHandle<Map<String, Employee>> returnMapByPromiseHandle() {
return Promises.deferCall(promise -> promise.resolve(Maps.map("1", new Employee(1, "Rick"), "2", new Employee(2, "Diana"))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@ public void testReturnMapByPromise() {

}

@Test
public void testReturnMapByPromiseHandle() {

final HttpTextResponse httpResponse = httpServerSimulator.get("/es/returnMapByPromiseHandle");

assertEquals(200, httpResponse.code());

Map<String, Employee> employeeMap = new BoonJsonMapper().fromJsonMap(httpResponse.body(), String.class, Employee.class);

assertEquals(2, employeeMap.size());

System.out.println(employeeMap);

}


@Test
public void testSendEmployeesWithCallback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import io.advantageous.qbit.server.ServiceEndpointServer;
import io.advantageous.qbit.util.PortUtils;
import io.advantageous.reakt.promise.Promise;
import io.advantageous.reakt.promise.PromiseHandle;
import io.advantageous.reakt.promise.Promises;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.net.URI;
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -107,6 +109,16 @@ public void testServiceWithReturnPromiseSuccessFromImpl() {

}

@Test
public void testServiceWithReturnPromiseSuccessFromImplHandle() {

final URI uri = serviceDiscoveryWebSocket.lookupServiceByPromiseHandle(empURI).blockingGet(Duration.ofSeconds(30));

assertEquals(successResult, uri);

}




@Test
Expand All @@ -124,6 +136,12 @@ private void testFail(ServiceDiscovery serviceDiscovery) {
}


@Test (expected = Exception.class)
public void testServiceWithReturnPromiseHandleFail() {
final URI uri = serviceDiscoveryWebSocket.lookupServiceByPromiseHandle(null).blockingGet(Duration.ofSeconds(10));

}


private void handleError(Throwable error) {
errorRef.set(error);
Expand Down Expand Up @@ -166,6 +184,9 @@ interface ServiceDiscovery {

Promise<URI> lookupServiceByPromise(URI uri);


PromiseHandle<URI> lookupServiceByPromiseHandle(URI uri);

Promise<Boolean> ok();

Promise<Integer> five();
Expand All @@ -184,16 +205,23 @@ public void lookupService(final io.advantageous.qbit.reactive.Callback<URI> call


public Promise<URI> lookupServiceByPromise(final URI uri) {
return Promises.invokablePromise(new Consumer<Promise<URI>>() {
@Override
public void accept(Promise<URI> uriPromise) {
return Promises.invokablePromise(uriPromise-> {
if (uri == null) {
uriPromise.reject("uri can't be null");
} else {
uriPromise.resolve(successResult);
}
});
}

public PromiseHandle<URI> lookupServiceByPromiseHandle(final URI uri) {
return Promises.deferCall(uriPromise-> {
if (uri == null) {
uriPromise.reject("uri can't be null");
} else {
uriPromise.resolve(successResult);
}
}
});
});
}

public void ok(final io.advantageous.qbit.reactive.Callback<Boolean> callback) {
Expand Down

0 comments on commit 32e6982

Please sign in to comment.