Search in sources :

Example 6 with StreamConnector

use of net.sergeych.tools.StreamConnector in project universa by UniversaBlockchain.

the class BitrustedConnectorTest method connectOnAny.

@Test
public void connectOnAny() throws Exception {
    StreamConnector sca = new StreamConnector();
    StreamConnector scb = new StreamConnector();
    BitrustedConnector ca = new BitrustedConnector(TestKeys.privateKey(0), sca.getInputStream(), scb.getOutputStream());
    BitrustedConnector cb = new BitrustedConnector(TestKeys.privateKey(1), scb.getInputStream(), sca.getOutputStream());
    Future<Object> connectA = pool.submit(() -> {
        ca.connect(null);
        return null;
    });
    // LogPrinter.showDebug(true);
    cb.connect(null);
    assertTrue(cb.isConnected());
    connectA.get();
    assertTrue(ca.isConnected());
    assertEquals(ca.getMySessionKey(), cb.getRemoteSessionKey());
    assertEquals(cb.getMySessionKey(), ca.getRemoteSessionKey());
    Future<?> f1 = pool.submit(() -> {
        cb.send(Binder.fromKeysValues("hello", "world"));
        cb.send(Binder.fromKeysValues("hello", "world2"));
        Binder res = Binder.from(cb.receive());
        assertEquals("bar", res.getStringOrThrow("foo"));
        res = Binder.from(cb.receive());
        // System.out.println("------> res "+res);
        assertEquals("bar2", res.getStringOrThrow("foo"));
        res = Binder.from(cb.receive());
        // System.out.println("------> res "+res);
        assertEquals("bar", res.getStringOrThrow("foo3"));
        return null;
    });
    Future<?> f2 = pool.submit(() -> {
        ca.send(Binder.fromKeysValues("foo", "bar"));
        ca.send(Binder.fromKeysValues("foo", "bar2"));
        ca.send(Binder.fromKeysValues("foo3", "bar"));
        Binder res = Binder.from(ca.receive());
        assertEquals("world", res.getStringOrThrow("hello"));
        res = Binder.from(ca.receive());
        assertEquals("world2", res.getStringOrThrow("hello"));
        return null;
    });
    f1.get();
    f2.get();
// ca.
}
Also used : Binder(net.sergeych.tools.Binder) StreamConnector(net.sergeych.tools.StreamConnector) Test(org.junit.Test)

Example 7 with StreamConnector

use of net.sergeych.tools.StreamConnector in project universa by UniversaBlockchain.

the class BitrustedConnectorTest method asyncLoadTest.

@Test
public void asyncLoadTest() throws Exception {
    StreamConnector sca = new StreamConnector();
    StreamConnector scb = new StreamConnector();
    BitrustedConnector ca = new BitrustedConnector(TestKeys.privateKey(0), sca.getInputStream(), scb.getOutputStream());
    BitrustedConnector cb = new BitrustedConnector(TestKeys.privateKey(1), scb.getInputStream(), sca.getOutputStream());
    Future<Object> connectA = pool.submit(() -> {
        ca.connect(null);
        return null;
    });
    // LogPrinter.showDebug(true);
    cb.connect(null);
    assertTrue(cb.isConnected());
    connectA.get();
    assertTrue(ca.isConnected());
    Farcall fa = new Farcall(ca);
    Farcall fb = new Farcall(cb);
    int[] counts = new int[2];
    fa.start(command -> {
        if (command.getName().equals("fast")) {
            counts[0]++;
            return "fast done";
        } else if (command.getName().equals("slow")) {
            Thread.sleep(3);
            counts[1]++;
            return "slow done";
        }
        return null;
    });
    fb.start();
    ExecutorService es = Executors.newWorkStealingPool();
    // ExecutorService es = Executors.newSingleThreadExecutor();
    ArrayList<Long> times = new ArrayList<>();
    for (int rep = 0; rep < 7; rep++) {
        ArrayList<Future<?>> futures = new ArrayList<>();
        counts[0] = counts[1] = 0;
        long t = StopWatch.measure(() -> {
            CompletableFuture<?> cf = new CompletableFuture<>();
            for (int r = 0; r < 40; r++) {
                futures.add(es.submit(() -> {
                    for (int i = 0; i < 10; i++) assertEquals("fast done", fb.send("fast").waitSuccess());
                    return null;
                }));
                futures.add(es.submit(() -> {
                    for (int i = 0; i < 2; i++) assertEquals("slow done", fb.send("slow").waitSuccess());
                    return null;
                }));
            }
            futures.forEach(f -> {
                try {
                    f.get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            });
        });
        times.add(t);
    // System.out.println(""+t+":  "+counts[0] + ", "+counts[1]);
    }
    // the call expenses should not rise with time, and by the 3rd JIT compilation should be done
    // note this test heavily depends on jit behavior!
    long t1 = times.get(2);
    long t2 = times.get(times.size() - 1);
    long mean = (t1 + t2) / 2;
    assertThat((double) Math.abs(t2 - t1) / ((double) mean), is(lessThan(0.20)));
}
Also used : StreamConnector(net.sergeych.tools.StreamConnector) ArrayList(java.util.ArrayList) Farcall(net.sergeych.farcall.Farcall) Test(org.junit.Test)

Example 8 with StreamConnector

use of net.sergeych.tools.StreamConnector in project universa by UniversaBlockchain.

the class FarcallTest method bossConnector.

@Test(timeout = 200)
public void bossConnector() throws Exception {
    StreamConnector sa = new StreamConnector();
    StreamConnector sb = new StreamConnector();
    BossConnector connA = new BossConnector(sa.getInputStream(), sb.getOutputStream());
    BossConnector connB = new BossConnector(sb.getInputStream(), sa.getOutputStream());
    Farcall a = new Farcall(connA);
    Farcall b = new Farcall(connB);
    basicTest(null, a, b);
}
Also used : StreamConnector(net.sergeych.tools.StreamConnector) Test(org.junit.Test)

Example 9 with StreamConnector

use of net.sergeych.tools.StreamConnector in project universa by UniversaBlockchain.

the class FarcallTest method jsonConnector.

@Test(timeout = 200)
public void jsonConnector() throws Exception {
    StreamConnector sa = new StreamConnector();
    StreamConnector sb = new StreamConnector();
    JsonConnector connA = new JsonConnector(sa.getInputStream(), sb.getOutputStream());
    JsonConnector connB = new JsonConnector(sb.getInputStream(), sa.getOutputStream());
    Farcall a = new Farcall(connA);
    Farcall b = new Farcall(connB);
    basicTest(null, a, b);
}
Also used : StreamConnector(net.sergeych.tools.StreamConnector) Test(org.junit.Test)

Aggregations

StreamConnector (net.sergeych.tools.StreamConnector)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)2 Farcall (net.sergeych.farcall.Farcall)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Binder (net.sergeych.tools.Binder)1