use of zmq.SocketBase in project jeromq by zeromq.
the class TestInvalidRep method testInvalidRep.
// Create REQ/ROUTER wiring.
@Test
public void testInvalidRep() {
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
SocketBase routerSocket = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
assertThat(routerSocket, notNullValue());
SocketBase reqSocket = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
assertThat(reqSocket, notNullValue());
int linger = 0;
int rc;
ZMQ.setSocketOption(routerSocket, ZMQ.ZMQ_LINGER, linger);
ZMQ.setSocketOption(reqSocket, ZMQ.ZMQ_LINGER, linger);
boolean brc = ZMQ.bind(routerSocket, "inproc://hi");
assertThat(brc, is(true));
brc = ZMQ.connect(reqSocket, "inproc://hi");
assertThat(brc, is(true));
// Initial request.
rc = ZMQ.send(reqSocket, "r", 0);
assertThat(rc, is(1));
// Receive the request.
Msg addr;
Msg bottom;
Msg body;
addr = ZMQ.recv(routerSocket, 0);
int addrSize = addr.size();
System.out.println("addrSize: " + addr.size());
assertThat(addr.size() > 0, is(true));
bottom = ZMQ.recv(routerSocket, 0);
assertThat(bottom.size(), is(0));
body = ZMQ.recv(routerSocket, 0);
assertThat(body.size(), is(1));
assertThat(body.data()[0], is((byte) 'r'));
// Send invalid reply.
rc = ZMQ.send(routerSocket, addr, 0);
assertThat(rc, is(addrSize));
// Send valid reply.
rc = ZMQ.send(routerSocket, addr, ZMQ.ZMQ_SNDMORE);
assertThat(rc, is(addrSize));
rc = ZMQ.send(routerSocket, bottom, ZMQ.ZMQ_SNDMORE);
assertThat(rc, is(0));
rc = ZMQ.send(routerSocket, "b", 0);
assertThat(rc, is(1));
// Check whether we've got the valid reply.
body = ZMQ.recv(reqSocket, 0);
assertThat(body.size(), is(1));
assertThat(body.data()[0], is((byte) 'b'));
// Tear down the wiring.
ZMQ.close(routerSocket);
ZMQ.close(reqSocket);
ZMQ.term(ctx);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class TestReqRelaxed method testReqRelaxed.
@Test
public void testReqRelaxed() throws Exception {
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
SocketBase req = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
assertThat(req, notNullValue());
boolean rc;
rc = req.setSocketOpt(ZMQ.ZMQ_REQ_CORRELATE, true);
assertThat(rc, is(true));
rc = req.setSocketOpt(ZMQ.ZMQ_REQ_RELAXED, true);
assertThat(rc, is(true));
rc = ZMQ.bind(req, "inproc://a");
assertThat(rc, is(true));
int services = 5;
List<SocketBase> reps = new ArrayList<>();
for (int idx = 0; idx < services; ++idx) {
SocketBase rep = ctx.createSocket(ZMQ.ZMQ_REP);
assertThat(rep, notNullValue());
reps.add(rep);
rc = req.setSocketOpt(ZMQ.ZMQ_RCVTIMEO, 500);
assertThat(rc, is(true));
rc = rep.connect("inproc://a");
assertThat(rc, is(true));
}
// We have to give the connects time to finish otherwise the requests
// will not properly round-robin. We could alternatively connect the
// REQ sockets to the REP sockets.
ZMQ.msleep(100);
// Case 1: Second send() before a reply arrives in a pipe.
// Send a request, ensure it arrives, don't send a reply
Helper.sendSeq(req, "A", "B");
Helper.recvSeq(reps.get(0), "A", "B");
// Send another request on the REQ socket
Helper.sendSeq(req, "C", "D");
Helper.recvSeq(reps.get(1), "C", "D");
// Send a reply to the first request - that should be discarded by the REQ
Helper.sendSeq(reps.get(0), "WRONG");
// Send the expected reply
Helper.sendSeq(reps.get(1), "OK");
Helper.recvSeq(req, "OK");
// Another standard req-rep cycle, just to check
Helper.sendSeq(req, "E");
Helper.recvSeq(reps.get(2), "E");
Helper.sendSeq(reps.get(2), "F", "G");
Helper.recvSeq(req, "F", "G");
// Case 2: Second send() after a reply is already in a pipe on the REQ.
// Send a request, ensure it arrives, send a reply
Helper.sendSeq(req, "H");
Helper.recvSeq(reps.get(3), "H");
Helper.sendSeq(reps.get(3), "BAD");
// Wait for message to be there.
ZMQ.msleep(100);
// Without receiving that reply, send another request on the REQ socket
Helper.sendSeq(req, "I");
Helper.recvSeq(reps.get(4), "I");
// Send the expected reply
Helper.sendSeq(reps.get(4), "GOOD");
Helper.recvSeq(req, "GOOD");
// Case 3: Check issue #1690. Two send() in a row should not close the
// communication pipes. For example pipe from req to rep[0] should not be
// closed after executing Case 1. So rep[0] should be the next to receive,
// not rep[1].
Helper.sendSeq(req, "J");
Helper.recvSeq(reps.get(0), "J");
ZMQ.closeZeroLinger(req);
for (SocketBase rep : reps) {
ZMQ.closeZeroLinger(rep);
}
// Wait for disconnects.
ZMQ.msleep(100);
ZMQ.term(ctx);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class TestReqRelaxed method testIssueLibzmq1965.
@Test
public void testIssueLibzmq1965() {
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
SocketBase req;
boolean rc;
// Case 4: Check issue #1695. As messages may pile up before a responder
// is available, we check that responses to messages other than the last
// sent one are correctly discarded by the REQ pipe
// Setup REQ socket as client
req = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
assertThat(req, notNullValue());
rc = req.setSocketOpt(ZMQ.ZMQ_REQ_CORRELATE, true);
assertThat(rc, is(true));
rc = req.setSocketOpt(ZMQ.ZMQ_REQ_RELAXED, true);
assertThat(rc, is(true));
rc = ZMQ.connect(req, "inproc://b");
assertThat(rc, is(true));
// Setup ROUTER socket as server but do not bind it just yet
SocketBase router = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
assertThat(router, notNullValue());
// Send two requests
Helper.sendSeq(req, "TO_BE_DISCARDED");
Helper.sendSeq(req, "TO_BE_ANSWERED");
// Bind server allowing it to receive messages
rc = ZMQ.bind(router, "inproc://b");
assertThat(rc, is(true));
// Read the two messages and send them back as is
bounce(router);
bounce(router);
Helper.recvSeq(req, "TO_BE_ANSWERED");
ZMQ.closeZeroLinger(req);
ZMQ.closeZeroLinger(router);
ctx.terminate();
}
use of zmq.SocketBase in project jeromq by zeromq.
the class PushPullSpecTest method roundRobinOut.
private void roundRobinOut(Ctx ctx, String address, int bindType, int connectType) throws IOException, InterruptedException {
SocketBase push = ZMQ.socket(ctx, bindType);
boolean rc = ZMQ.bind(push, address);
assertThat(rc, is(true));
int timeout = 250;
int services = 5;
List<SocketBase> senders = new ArrayList<>();
for (int peer = 0; peer < services; ++peer) {
SocketBase reps = ZMQ.socket(ctx, connectType);
assertThat(reps, notNullValue());
senders.add(reps);
rc = ZMQ.setSocketOption(reps, ZMQ.ZMQ_RCVTIMEO, timeout);
assertThat(rc, is(true));
String host = (String) ZMQ.getSocketOptionExt(push, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
rc = ZMQ.connect(reps, host);
assertThat(rc, is(true));
}
// Wait for connections.
ZMQ.msleep(100);
// Send 2N messages
for (int peer = 0; peer < services; ++peer) {
rc = sendSeq(push, "ABC");
assertThat(rc, is(true));
}
for (int peer = 0; peer < services; ++peer) {
rc = sendSeq(push, "DEF");
assertThat(rc, is(true));
}
// Expect every PULL got one of each
for (int peer = 0; peer < services; ++peer) {
recvSeq(senders.get(peer), "ABC");
recvSeq(senders.get(peer), "DEF");
}
ZMQ.closeZeroLinger(push);
for (SocketBase sender : senders) {
ZMQ.closeZeroLinger(sender);
}
// Wait for disconnects.
ZMQ.msleep(100);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class PushPullSpecTest method blockOnSendNoPeers.
private void blockOnSendNoPeers(Ctx ctx, String address, int bindType) throws IOException, InterruptedException {
SocketBase push = ZMQ.socket(ctx, bindType);
int timeout = 250;
boolean rc = ZMQ.setSocketOption(push, ZMQ.ZMQ_SNDTIMEO, timeout);
assertThat(rc, is(true));
int ret = ZMQ.send(push, "", ZMQ.ZMQ_DONTWAIT);
assertThat(ret, is(-1));
assertThat(push.errno(), is(ZError.EAGAIN));
ret = ZMQ.send(push, "", 0);
assertThat(ret, is(-1));
assertThat(push.errno(), is(ZError.EAGAIN));
rc = ZMQ.bind(push, address);
assertThat(rc, is(true));
ret = ZMQ.send(push, "", ZMQ.ZMQ_DONTWAIT);
assertThat(ret, is(-1));
assertThat(push.errno(), is(ZError.EAGAIN));
ret = ZMQ.send(push, "", 0);
assertThat(ret, is(-1));
assertThat(push.errno(), is(ZError.EAGAIN));
ZMQ.close(push);
}
Aggregations