use of zmq.Msg in project jeromq by zeromq.
the class AbstractSpecTest method recvSeq.
public void recvSeq(SocketBase socket, String... data) {
for (int idx = 0; idx < data.length; ++idx) {
Msg msg = ZMQ.recv(socket, 0);
String payload = data[idx];
if (payload == null) {
assertThat(msg, notNullValue());
assertThat(msg.size(), is(0));
} else {
assertThat(msg, notNullValue());
assertThat(msg.data(), is(payload.getBytes(ZMQ.CHARSET)));
}
int rc = ZMQ.getSocketOption(socket, ZMQ.ZMQ_RCVMORE);
if (idx == data.length - 1) {
assertThat(rc, is(0));
} else {
assertThat(rc, is(1));
}
}
}
use of zmq.Msg in project jeromq by zeromq.
the class RouterProbeTest method testProbeRouter.
@Test
public void testProbeRouter() throws IOException, InterruptedException {
int port = Utils.findOpenPort();
String host = "tcp://127.0.0.1:" + port;
Ctx ctx = ZMQ.createContext();
// Server socket will accept connections
SocketBase server = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
assertThat(server, notNullValue());
boolean rc = ZMQ.bind(server, host);
assertThat(rc, is(true));
// Create client and connect to server, doing a probe
SocketBase client = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
assertThat(client, notNullValue());
rc = ZMQ.setSocketOption(client, ZMQ.ZMQ_IDENTITY, "X");
assertThat(rc, is(true));
rc = ZMQ.setSocketOption(client, ZMQ.ZMQ_PROBE_ROUTER, true);
assertThat(rc, is(true));
rc = ZMQ.connect(client, host);
assertThat(rc, is(true));
// We expect an identity=X + empty message from client
Msg msg = ZMQ.recv(server, 0);
assertThat(msg, notNullValue());
assertThat(msg.get(0), is((byte) 'X'));
msg = ZMQ.recv(server, 0);
assertThat(msg, notNullValue());
assertThat(msg.size(), is(0));
// Send a message to client now
int ret = ZMQ.send(server, "X", ZMQ.ZMQ_SNDMORE);
assertThat(ret, is(1));
ret = ZMQ.send(server, "Hello", 0);
assertThat(ret, is(5));
msg = ZMQ.recv(client, 0);
assertThat(msg, notNullValue());
assertThat(msg.size(), is(5));
// TODO DIFF V4 test should stop here, check the logic if we should receive payload in the previous message.
msg = ZMQ.recv(client, 0);
assertThat(msg, notNullValue());
assertThat(new String(msg.data(), ZMQ.CHARSET), is("Hello"));
ZMQ.closeZeroLinger(server);
ZMQ.closeZeroLinger(client);
// Shutdown
ZMQ.term(ctx);
}
use of zmq.Msg 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.Msg in project jeromq by zeromq.
the class TestReqRelaxed method bounce.
private void bounce(SocketBase socket) {
boolean more = false;
do {
Msg msg = socket.recv(0);
assertThat(msg, notNullValue());
more = ZMQ.getSocketOption(socket, ZMQ.ZMQ_RCVMORE) > 0;
socket.send(msg, more ? ZMQ.ZMQ_SNDMORE : 0);
} while (more);
}
use of zmq.Msg in project jeromq by zeromq.
the class DistTest method testAttachedWhenSendingNoMoreMessage.
@Test
public void testAttachedWhenSendingNoMoreMessage() {
Msg msg = new Msg();
dist.sendToAll(msg);
// no change in eligible/active states
assertThat(dist.eligible(), is(1));
assertThat(dist.active(), is(1));
dist.attach(second);
assertThat(dist.eligible(), is(2));
// active should have changed, as we are NOT in the middle a sending a multi-part message
assertThat(dist.active(), is(2));
dist.sendToAll(msg);
assertThat(dist.eligible(), is(2));
assertThat(dist.active(), is(2));
// no change in eligible/active states
dist.activated(second);
assertThat(dist.eligible(), is(2));
assertThat(dist.active(), is(2));
}
Aggregations