use of net.sergeych.tools.StreamConnector in project universa by UniversaBlockchain.
the class BossConnectorTest method directLoadTest.
@Test
public void directLoadTest() throws Exception {
StreamConnector sa = new StreamConnector();
StreamConnector sb = new StreamConnector();
Farcall fa = new Farcall(new BossConnector(sa.getInputStream(), sb.getOutputStream()));
Farcall fb = new Farcall(new BossConnector(sb.getInputStream(), sa.getOutputStream()));
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();
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), CoreMatchers.is(lessThan(0.16)));
}
use of net.sergeych.tools.StreamConnector in project universa by UniversaBlockchain.
the class BossConnectorTest method send.
@Test
public void send() throws Exception {
StreamConnector sa = new StreamConnector();
BossConnector bsc = new BossConnector(sa.getInputStream(), sa.getOutputStream());
bsc.send(Do.map("hello", "мыльня"));
Map<String, Object> res = bsc.receive();
assertEquals(1, res.size());
assertEquals("мыльня", res.get("hello"));
}
use of net.sergeych.tools.StreamConnector in project universa by UniversaBlockchain.
the class JsonConnectorTest method send.
@Test(timeout = 100)
public void send() throws Exception {
StreamConnector sa = new StreamConnector();
JsonConnector jsc = new JsonConnector(sa.getInputStream(), sa.getOutputStream());
jsc.send(Do.map("hello", "мыльня"));
Map<String, Object> res = jsc.receive();
assertEquals(1, res.size());
assertEquals("мыльня", res.get("hello"));
}
use of net.sergeych.tools.StreamConnector in project universa by UniversaBlockchain.
the class BitrustedConnectorTest method sampleSync.
@Test
public void sampleSync() 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";
} else if (command.getName().equals("stop")) {
sca.close();
System.out.println("closed!");
}
return null;
});
fb.start();
Future<Object> f1 = pool.submit(() -> fb.send("fast").waitSuccess());
Future<Object> f2 = pool.submit(() -> fb.send("fast").waitSuccess());
Future<Object> f3 = pool.submit(() -> fb.send("fast").waitSuccess());
Future<Object> f4 = pool.submit(() -> fb.send("slow").waitSuccess());
Future<Object> f5 = pool.submit(() -> fb.send("slow").waitSuccess());
assertEquals("fast done", f1.get());
assertEquals("fast done", f2.get());
assertEquals("fast done", f3.get());
assertEquals("slow done", f4.get());
assertEquals("slow done", f5.get());
assertEquals(3, counts[0]);
assertEquals(2, counts[1]);
}
use of net.sergeych.tools.StreamConnector in project universa by UniversaBlockchain.
the class BitrustedConnectorTest method timoutOnConnection.
@Test
public void timoutOnConnection() throws Exception {
StreamConnector sca = new StreamConnector();
BitrustedConnector connector = new BitrustedConnector(TestKeys.privateKey(0), sca.getInputStream(), new ByteArrayOutputStream());
connector.setHandshakeTimeoutMillis(10);
try {
connector.connect(null);
fail("must throw TimeoutException");
} catch (TimeoutException x) {
// all ok
}
}
Aggregations