use of zmq.Ctx in project jeromq by zeromq.
the class ProxySingleSocketTest method testProxySingleSocket.
@Test
public void testProxySingleSocket() throws IOException, InterruptedException {
int port = Utils.findOpenPort();
String host = "tcp://127.0.0.1:" + port;
// The main thread simply starts several clients and a server, and then
// waits for the server to finish.
Ctx ctx = ZMQ.createContext();
SocketBase req = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
assertThat(req, notNullValue());
boolean rc = ZMQ.connect(req, host);
assertThat(rc, is(true));
// Control socket receives terminate command from main over inproc
SocketBase control = ZMQ.socket(ctx, ZMQ.ZMQ_PUB);
rc = ZMQ.bind(control, "inproc://control");
assertThat(rc, is(true));
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new ServerTask(ctx, host));
int ret = ZMQ.send(req, "msg1", 0);
assertThat(ret, is(4));
System.out.print(".");
Msg msg = ZMQ.recv(req, 0);
System.out.print(".");
assertThat(msg, notNullValue());
assertThat(new String(msg.data(), ZMQ.CHARSET), is("msg1"));
ret = ZMQ.send(req, "msg22", 0);
assertThat(ret, is(5));
System.out.print(".");
msg = ZMQ.recv(req, 0);
System.out.print(".");
assertThat(msg, notNullValue());
assertThat(new String(msg.data(), ZMQ.CHARSET), is("msg22"));
ret = ZMQ.send(control, ZMQ.PROXY_TERMINATE, 0);
assertThat(ret, is(9));
System.out.println(".");
ZMQ.close(control);
ZMQ.close(req);
executor.shutdown();
executor.awaitTermination(30, TimeUnit.SECONDS);
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class TestPairIpc method testPairIpc.
// Create REQ/ROUTER wiring.
@Test(timeout = 5000)
public void testPairIpc() {
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
SocketBase pairBind = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
assertThat(pairBind, notNullValue());
UUID random;
do {
random = UUID.randomUUID();
} while (!ZMQ.bind(pairBind, "ipc:///tmp/tester/" + random.toString()));
SocketBase pairConnect = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
assertThat(pairConnect, notNullValue());
boolean brc = ZMQ.connect(pairConnect, "ipc:///tmp/tester/" + random.toString());
assertThat(brc, is(true));
Helper.bounce(pairBind, pairConnect);
// Tear down the wiring.
ZMQ.close(pairBind);
ZMQ.close(pairConnect);
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class ConflateTest method test.
@Test
public void test() throws IOException, InterruptedException {
Ctx ctx = ZMQ.init(1);
assert (ctx != null);
SocketBase in = ZMQ.socket(ctx, ZMQ.ZMQ_PULL);
assert (in != null);
String host = "tcp://localhost:*";
int conflate = 1;
ZMQ.setSocketOption(in, ZMQ.ZMQ_CONFLATE, conflate);
boolean rc = ZMQ.bind(in, host);
assert (rc);
SocketBase out = ZMQ.socket(ctx, ZMQ.ZMQ_PUSH);
assert (out != null);
String ep = (String) ZMQ.getSocketOptionExt(in, ZMQ.ZMQ_LAST_ENDPOINT);
rc = ZMQ.connect(out, ep);
assert (rc);
int messageCount = 20;
for (int j = 0; j < messageCount; ++j) {
int count = Helper.send(out, Integer.toString(j));
assert (count > 0);
}
Thread.sleep(200);
String recvd = Helper.recv(in);
Assert.assertEquals("19", recvd);
ZMQ.close(in);
ZMQ.close(out);
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class PollerTest method catchNotification.
@Test(timeout = 1000)
public void catchNotification() throws IOException, InterruptedException {
CountDownLatch catched = new CountDownLatch(1);
AtomicReference<Selector> selectorRef = new AtomicReference<>();
Ctx ctx = new Ctx() {
@Override
public Selector createSelector() {
selectorRef.set(super.createSelector());
return selectorRef.get();
}
};
ctx.setNotificationExceptionHandler((t, e) -> {
if (e instanceof ClosedSelectorException) {
catched.countDown();
}
});
Poller poller = new Poller(ctx, "test");
poller.start();
selectorRef.get().close();
catched.await();
}
use of zmq.Ctx in project jeromq by zeromq.
the class ProxyTcpTest method testProxyTcp.
@Test
public void testProxyTcp() throws Exception {
int routerPort = Utils.findOpenPort();
int dealerPort = Utils.findOpenPort();
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
Proxy mt = new Proxy(ctx, routerPort, dealerPort);
mt.start();
new Dealer(ctx, "A", dealerPort).start();
// new Dealer(ctx, "B", dealerPort).start();
ZMQ.sleep(1);
Thread client = new Client(routerPort);
client.start();
client.join();
ZMQ.term(ctx);
}
Aggregations