use of zmq.SocketBase in project jeromq by zeromq.
the class DealerSpecTest method fairQueueIn.
private void fairQueueIn(Ctx ctx, String address, int bindType, int connectType) throws IOException, InterruptedException {
// Server socket will accept connections
SocketBase receiver = ZMQ.socket(ctx, bindType);
assertThat(receiver, notNullValue());
int timeout = 250;
boolean rc = ZMQ.setSocketOption(receiver, ZMQ.ZMQ_RCVTIMEO, timeout);
assertThat(rc, is(true));
rc = ZMQ.bind(receiver, address);
assertThat(rc, is(true));
int services = 5;
List<SocketBase> senders = new ArrayList<>();
for (int peer = 0; peer < services; ++peer) {
SocketBase sender = ZMQ.socket(ctx, connectType);
assertThat(sender, notNullValue());
senders.add(sender);
rc = ZMQ.setSocketOption(sender, ZMQ.ZMQ_RCVTIMEO, timeout);
assertThat(rc, is(true));
String host = (String) ZMQ.getSocketOptionExt(receiver, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
rc = ZMQ.connect(sender, host);
assertThat(rc, is(true));
}
rc = sendSeq(senders.get(0), "A");
assertThat(rc, is(true));
recvSeq(receiver, "A");
rc = sendSeq(senders.get(0), "A");
assertThat(rc, is(true));
recvSeq(receiver, "A");
// send N requests
for (int peer = 0; peer < services; ++peer) {
sendSeq(senders.get(peer), "B");
}
// Wait for data.
ZMQ.msleep(50);
// handle N requests
for (int peer = 0; peer < services; ++peer) {
recvSeq(receiver, "B");
}
ZMQ.closeZeroLinger(receiver);
for (SocketBase sender : senders) {
ZMQ.closeZeroLinger(sender);
}
// Wait for disconnects.
ZMQ.msleep(100);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class DealerSpecTest method blockOnSendNoPeers.
private void blockOnSendNoPeers(Ctx ctx, String address, int bindType) throws IOException, InterruptedException {
SocketBase dealer = ZMQ.socket(ctx, bindType);
int timeout = 250;
boolean rc = ZMQ.setSocketOption(dealer, ZMQ.ZMQ_SNDTIMEO, timeout);
assertThat(rc, is(true));
int ret = ZMQ.send(dealer, "", ZMQ.ZMQ_DONTWAIT);
assertThat(ret, is(-1));
assertThat(dealer.errno(), is(ZError.EAGAIN));
ret = ZMQ.send(dealer, "", 0);
assertThat(ret, is(-1));
assertThat(dealer.errno(), is(ZError.EAGAIN));
rc = ZMQ.bind(dealer, address);
assertThat(rc, is(true));
ret = ZMQ.send(dealer, "", ZMQ.ZMQ_DONTWAIT);
assertThat(ret, is(-1));
assertThat(dealer.errno(), is(ZError.EAGAIN));
ret = ZMQ.send(dealer, "", 0);
assertThat(ret, is(-1));
assertThat(dealer.errno(), is(ZError.EAGAIN));
ZMQ.close(dealer);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class ReqSpecTest method onlyListensToCurrentPeer.
private void onlyListensToCurrentPeer(Ctx ctx, String bindAddress, int bindType, int connectType) {
SocketBase socket = ZMQ.socket(ctx, bindType);
boolean rc = ZMQ.setSocketOption(socket, ZMQ.ZMQ_IDENTITY, "A");
assertThat(rc, is(true));
rc = ZMQ.bind(socket, bindAddress);
assertThat(rc, is(true));
int timeout = 250;
int services = 3;
List<SocketBase> senders = new ArrayList<>();
for (int peer = 0; peer < services; ++peer) {
SocketBase connect = ZMQ.socket(ctx, connectType);
assertThat(connect, notNullValue());
senders.add(connect);
rc = ZMQ.setSocketOption(connect, ZMQ.ZMQ_RCVTIMEO, timeout);
assertThat(rc, is(true));
rc = ZMQ.setSocketOption(connect, ZMQ.ZMQ_ROUTER_MANDATORY, true);
assertThat(rc, is(true));
String host = (String) ZMQ.getSocketOptionExt(socket, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
rc = ZMQ.connect(connect, host);
assertThat(rc, is(true));
}
ZMQ.msleep(100);
for (int peer = 0; peer < services; ++peer) {
// There still is a race condition when a stale peer's message
// arrives at the REQ just after a request was sent to that peer.
// To avoid that happening in the test, sleep for a bit.
ZMQ.msleep(10);
sendSeq(socket, "ABC");
// Receive on router i
recvSeq(senders.get(peer), "A", null, "ABC");
// Send back replies on all routers
for (int j = 0; j < services; ++j) {
List<String> replies = Arrays.asList("WRONG", "GOOD");
String reply = peer == j ? replies.get(1) : replies.get(0);
sendSeq(senders.get(j), "A", null, reply);
}
// Receive only the good reply
recvSeq(socket, "GOOD");
}
ZMQ.closeZeroLinger(socket);
for (SocketBase sender : senders) {
ZMQ.closeZeroLinger(sender);
}
// Wait for disconnects.
ZMQ.msleep(100);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class ReqSpecTest method blockOnSendNoPeers.
private void blockOnSendNoPeers(Ctx ctx, String address, int bindType) throws IOException, InterruptedException {
SocketBase socket = ZMQ.socket(ctx, bindType);
int timeout = 250;
boolean rc = ZMQ.setSocketOption(socket, ZMQ.ZMQ_SNDTIMEO, timeout);
assertThat(rc, is(true));
int ret = ZMQ.send(socket, "", ZMQ.ZMQ_DONTWAIT);
assertThat(ret, is(-1));
assertThat(socket.errno(), is(ZError.EAGAIN));
ret = ZMQ.send(socket, "", 0);
assertThat(ret, is(-1));
assertThat(socket.errno(), is(ZError.EAGAIN));
rc = ZMQ.bind(socket, address);
assertThat(rc, is(true));
ret = ZMQ.send(socket, "", ZMQ.ZMQ_DONTWAIT);
assertThat(ret, is(-1));
assertThat(socket.errno(), is(ZError.EAGAIN));
ret = ZMQ.send(socket, "", 0);
assertThat(ret, is(-1));
assertThat(socket.errno(), is(ZError.EAGAIN));
ZMQ.close(socket);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class ReqSpecTest method messageFormat.
private void messageFormat(Ctx ctx, String address, int bindType, int connectType) throws IOException, InterruptedException {
// Server socket will accept connections
SocketBase req = ZMQ.socket(ctx, bindType);
assertThat(req, notNullValue());
boolean rc = ZMQ.bind(req, address);
assertThat(rc, is(true));
SocketBase router = ZMQ.socket(ctx, connectType);
assertThat(router, notNullValue());
String host = (String) ZMQ.getSocketOptionExt(req, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
rc = ZMQ.connect(router, host);
assertThat(rc, is(true));
// Send a multi-part request.
sendSeq(req, "ABC", "DEF");
// Receive peer identity
Msg msg = ZMQ.recv(router, 0);
assertThat(msg, notNullValue());
assertThat(msg.size() > 0, is(true));
Msg peerId = msg;
int more = ZMQ.getSocketOption(router, ZMQ.ZMQ_RCVMORE);
assertThat(more, is(1));
// Receive the rest.
recvSeq(router, null, "ABC", "DEF");
// Send back a single-part reply.
int ret = ZMQ.send(router, peerId, ZMQ.ZMQ_SNDMORE);
assertThat(ret, is(peerId.size()));
sendSeq(router, null, "GHI");
// Receive reply.
recvSeq(req, "GHI");
ZMQ.closeZeroLinger(req);
ZMQ.closeZeroLinger(router);
// Wait for disconnects.
ZMQ.msleep(100);
}
Aggregations