use of zmq.Ctx in project jeromq by zeromq.
the class TestReqrepInproc method testReqrepInproc.
// Create REQ/ROUTER wiring.
@Test
public void testReqrepInproc() {
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
SocketBase sb = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
assertThat(sb, notNullValue());
boolean brc = ZMQ.bind(sb, "inproc://a");
assertThat(brc, is(true));
SocketBase sc = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
assertThat(sc, notNullValue());
brc = ZMQ.connect(sc, "inproc://a");
assertThat(brc, is(true));
Helper.bounce(sb, sc);
// Tear down the wiring.
ZMQ.close(sb);
ZMQ.close(sc);
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class RouterMandatoryTest method testRouterMandatory.
@Test
public void testRouterMandatory() throws Exception {
int sent;
boolean rc;
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
SocketBase router = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
assertThat(router, notNullValue());
rc = ZMQ.bind(router, "tcp://127.0.0.1:*");
assertThat(rc, is(true));
// Sending a message to an unknown peer with the default setting
// This will not report any error
sent = ZMQ.send(router, "UNKNOWN", ZMQ.ZMQ_SNDMORE);
assertThat(sent, is(7));
sent = ZMQ.send(router, "DATA", 0);
assertThat(sent, is(4));
// Send a message to an unknown peer with mandatory routing
// This will fail
int mandatory = 1;
ZMQ.setSocketOption(router, ZMQ.ZMQ_ROUTER_MANDATORY, mandatory);
// Send a message and check that it fails
sent = ZMQ.send(router, "UNKNOWN", ZMQ.ZMQ_SNDMORE | ZMQ.ZMQ_DONTWAIT);
assertThat(sent, is(-1));
assertThat(router.errno(), is(ZError.EHOSTUNREACH));
// Create dealer called "X" and connect it to our router
SocketBase dealer = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assertThat(dealer, notNullValue());
ZMQ.setSocketOption(dealer, ZMQ.ZMQ_IDENTITY, "X");
String host = (String) ZMQ.getSocketOptionExt(router, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
rc = ZMQ.connect(dealer, host);
assertThat(rc, is(true));
// Get message from dealer to know when connection is ready
int ret = ZMQ.send(dealer, "Hello", 0);
assertThat(ret, is(5));
Msg msg = ZMQ.recv(router, 0);
assertThat(msg, notNullValue());
assertThat(msg.data()[0], is((byte) 'X'));
// Send a message to connected dealer now
// It should work
sent = ZMQ.send(router, "X", ZMQ.ZMQ_SNDMORE);
assertThat(sent, is(1));
sent = ZMQ.send(router, "Hello", 0);
assertThat(sent, is(5));
// Clean up.
ZMQ.close(router);
ZMQ.close(dealer);
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class RouterSpecTest method testDestroyQueueOnDisconnect.
@Test
@Ignore
public void testDestroyQueueOnDisconnect() throws IOException, InterruptedException {
Ctx ctx = ZMQ.createContext();
List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
for (String bindAddress : binds) {
// SHALL create a double queue when a peer connects to it. If this peer
// disconnects, the ROUTER socket SHALL destroy its double queue and SHALL
// discard any messages it contains.
// *** Test disabled until libzmq does this properly ***
// test_destroy_queue_on_disconnect (ctx);
}
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class RouterSpecTest method testFairQueueIn.
@Test
public void testFairQueueIn() throws IOException, InterruptedException {
Ctx ctx = ZMQ.createContext();
List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
for (String bindAddress : binds) {
// SHALL receive incoming messages from its peers using a fair-queuing
// strategy.
fairQueueIn(ctx, bindAddress, ZMQ.ZMQ_ROUTER, ZMQ.ZMQ_DEALER);
}
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class TestReqCorrelateRelaxed method overallSetup.
/**
* Prepares sockets and runs actual tests.
*
* Doing it this way so order is guaranteed.
*
* @throws Exception
*/
@Test
public void overallSetup() throws Exception {
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
SocketBase dealer = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assertThat(dealer, notNullValue());
boolean brc = ZMQ.bind(dealer, "inproc://a");
assertThat(brc, is(true));
SocketBase reqClient = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
assertThat(reqClient, notNullValue());
reqClient.setSocketOpt(ZMQ.ZMQ_REQ_CORRELATE, 1);
reqClient.setSocketOpt(ZMQ.ZMQ_REQ_RELAXED, 1);
reqClient.setSocketOpt(ZMQ.ZMQ_RCVTIMEO, 100);
brc = ZMQ.connect(reqClient, "inproc://a");
assertThat(brc, is(true));
// Test good path
byte[] origRequestId = testReqSentFrames(dealer, reqClient);
testReqRecvGoodRequestId(dealer, reqClient, origRequestId);
// Test what happens when a bad request ID is sent back.
origRequestId = testReqSentFrames(dealer, reqClient);
testReqRecvBadRequestId(dealer, reqClient, origRequestId);
ZMQ.close(reqClient);
ZMQ.close(dealer);
ZMQ.term(ctx);
}
Aggregations