Search in sources :

Example 1 with AsyncCallbackFuture

use of io.fabric8.dosgi.api.AsyncCallbackFuture in project fabric8 by jboss-fuse.

the class InvocationTest method testOverflowAsync.

@Test(timeout = 30 * 1000)
public void testOverflowAsync() throws Exception {
    DispatchQueue queue = Dispatch.createQueue();
    HashMap<String, SerializationStrategy> map = new HashMap<String, SerializationStrategy>();
    map.put("protobuf", new ProtobufSerializationStrategy());
    ServerInvokerImpl server = new ServerInvokerImpl("tcp://localhost:0", queue, map);
    server.start();
    ClientInvokerImpl client = new ClientInvokerImpl(queue, map);
    client.start();
    try {
        server.registerService("service-id", new ServerInvoker.ServiceFactory() {

            public Object get() {
                return new HelloImpl();
            }

            public void unget() {
            }
        }, HelloImpl.class.getClassLoader());
        InvocationHandler handler = client.getProxy(server.getConnectAddress(), "service-id", HelloImpl.class.getClassLoader());
        Hello hello = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), new Class[] { Hello.class }, handler);
        char[] chars = new char[65 * 1024];
        String payload = new String(chars);
        final List<AsyncCallbackFuture<String>> futures = new ArrayList<AsyncCallbackFuture<String>>();
        for (int i = 0; i < 100; i++) {
            AsyncCallbackFuture<String> future = new AsyncCallbackFuture<String>();
            hello.hello(payload, future);
            futures.add(future);
        }
        for (Future<String> f : futures) {
            f.get(3, TimeUnit.SECONDS);
        }
    // future2.get(2, TimeUnit.SECONDS);
    // assertEquals("Hello Hiram!", future1.get(2, TimeUnit.SECONDS));
    // assertEquals("Hello Hiram!", hello.protobuf(stringValue(payload)).getValue());
    } finally {
        server.stop();
        client.stop();
    }
}
Also used : ServerInvoker(io.fabric8.dosgi.io.ServerInvoker) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InvocationHandler(java.lang.reflect.InvocationHandler) ClientInvokerImpl(io.fabric8.dosgi.tcp.ClientInvokerImpl) ServerInvokerImpl(io.fabric8.dosgi.tcp.ServerInvokerImpl) DispatchQueue(org.fusesource.hawtdispatch.DispatchQueue) Test(org.junit.Test)

Example 2 with AsyncCallbackFuture

use of io.fabric8.dosgi.api.AsyncCallbackFuture in project fabric8 by jboss-fuse.

the class InvocationTest method testInvoke.

@Test(timeout = 30 * 1000)
public void testInvoke() throws Exception {
    DispatchQueue queue = Dispatch.createQueue();
    HashMap<String, SerializationStrategy> map = new HashMap<String, SerializationStrategy>();
    map.put("protobuf", new ProtobufSerializationStrategy());
    ServerInvokerImpl server = new ServerInvokerImpl("tcp://localhost:0", queue, map);
    server.start();
    ClientInvokerImpl client = new ClientInvokerImpl(queue, map);
    client.start();
    try {
        server.registerService("service-id", new ServerInvoker.ServiceFactory() {

            public Object get() {
                return new HelloImpl();
            }

            public void unget() {
            }
        }, HelloImpl.class.getClassLoader());
        InvocationHandler handler = client.getProxy(server.getConnectAddress(), "service-id", HelloImpl.class.getClassLoader());
        Hello hello = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), new Class[] { Hello.class }, handler);
        assertEquals("Hello Fabric!", hello.hello("Fabric"));
        assertEquals("Hello World!", hello.helloworld());
        // Verification the we can pick the right overloaded method even if using a mixure
        // of primitives / objects and array dimensions.
        assertEquals('a', hello.mix(0));
        assertEquals('b', hello.mix(new int[] { 0 }));
        assertEquals('c', hello.mix(new Integer(0)));
        assertEquals('d', hello.mix(new Integer[] { new Integer(0) }));
        assertEquals('e', hello.mix(new int[0][0]));
        assertEquals('f', hello.mix(new Integer[0][0]));
        AsyncCallbackFuture<String> future1 = new AsyncCallbackFuture<String>();
        hello.hello("Hiram", future1);
        assertEquals("Hello Hiram!", future1.get(2, TimeUnit.SECONDS));
        assertEquals("Hello Hiram!", hello.protobuf(stringValue("Hiram")).getValue());
        AsyncCallbackFuture<StringValue.Getter> future2 = new AsyncCallbackFuture<StringValue.Getter>();
        hello.protobuf(stringValue("Hiram Async"), future2);
        assertEquals("Hello Hiram Async!", future2.get(2, TimeUnit.SECONDS).getValue());
    } finally {
        server.stop();
        client.stop();
    }
}
Also used : ServerInvoker(io.fabric8.dosgi.io.ServerInvoker) HashMap(java.util.HashMap) InvocationHandler(java.lang.reflect.InvocationHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientInvokerImpl(io.fabric8.dosgi.tcp.ClientInvokerImpl) ServerInvokerImpl(io.fabric8.dosgi.tcp.ServerInvokerImpl) DispatchQueue(org.fusesource.hawtdispatch.DispatchQueue) Test(org.junit.Test)

Example 3 with AsyncCallbackFuture

use of io.fabric8.dosgi.api.AsyncCallbackFuture in project fabric8 by jboss-fuse.

the class TransportFailureTest method testInvoke.

@Test
public void testInvoke() throws Exception {
    DispatchQueue queue = Dispatch.createQueue();
    HashMap<String, SerializationStrategy> map = new HashMap<String, SerializationStrategy>();
    map.put("protobuf", new ProtobufSerializationStrategy());
    ServerInvokerImpl server = new ServerInvokerImpl("tcp://localhost:0", queue, map);
    server.start();
    ClientInvokerImpl client = new ClientInvokerImpl(queue, map);
    client.start();
    try {
        server.registerService("service-id", new ServerInvoker.ServiceFactory() {

            public Object get() {
                return new HelloImpl();
            }

            public void unget() {
            }
        }, HelloImpl.class.getClassLoader());
        InvocationHandler handler = client.getProxy(server.getConnectAddress(), "service-id", HelloImpl.class.getClassLoader());
        Hello hello = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), new Class[] { Hello.class }, handler);
        AsyncCallbackFuture<String> future1 = new AsyncCallbackFuture<String>();
        hello.hello("Guillaume", future1);
        long t0 = System.currentTimeMillis();
        try {
            assertEquals("Hello Guillaume!", future1.get(MAX_DELAY, TimeUnit.MILLISECONDS));
            fail("Should have thrown an exception");
        } catch (Exception e) {
            // Expected
            long t1 = System.currentTimeMillis();
            assertTrue(t1 - t0 > SLEEP_TIME / 2);
            assertTrue(t1 - t0 < MAX_DELAY / 2);
        }
    } finally {
        server.stop();
        client.stop();
    }
}
Also used : ServerInvoker(io.fabric8.dosgi.io.ServerInvoker) HashMap(java.util.HashMap) InvocationHandler(java.lang.reflect.InvocationHandler) AsyncCallbackFuture(io.fabric8.dosgi.api.AsyncCallbackFuture) ClientInvokerImpl(io.fabric8.dosgi.tcp.ClientInvokerImpl) ProtobufSerializationStrategy(io.fabric8.dosgi.api.ProtobufSerializationStrategy) SerializationStrategy(io.fabric8.dosgi.api.SerializationStrategy) ServerInvokerImpl(io.fabric8.dosgi.tcp.ServerInvokerImpl) ProtobufSerializationStrategy(io.fabric8.dosgi.api.ProtobufSerializationStrategy) DispatchQueue(org.fusesource.hawtdispatch.DispatchQueue) Test(org.junit.Test)

Aggregations

ServerInvoker (io.fabric8.dosgi.io.ServerInvoker)3 ClientInvokerImpl (io.fabric8.dosgi.tcp.ClientInvokerImpl)3 ServerInvokerImpl (io.fabric8.dosgi.tcp.ServerInvokerImpl)3 InvocationHandler (java.lang.reflect.InvocationHandler)3 HashMap (java.util.HashMap)3 DispatchQueue (org.fusesource.hawtdispatch.DispatchQueue)3 Test (org.junit.Test)3 AsyncCallbackFuture (io.fabric8.dosgi.api.AsyncCallbackFuture)1 ProtobufSerializationStrategy (io.fabric8.dosgi.api.ProtobufSerializationStrategy)1 SerializationStrategy (io.fabric8.dosgi.api.SerializationStrategy)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1