use of zmq.Msg in project jeromq by zeromq.
the class RepSpecTest method fairQueueIn.
private void fairQueueIn(Ctx ctx, String address, int bindType, int connectType) throws IOException, InterruptedException {
// Server socket will accept connections
SocketBase rep = ZMQ.socket(ctx, bindType);
assertThat(rep, notNullValue());
int timeout = 250;
boolean rc = ZMQ.setSocketOption(rep, ZMQ.ZMQ_RCVTIMEO, timeout);
assertThat(rc, is(true));
rc = ZMQ.bind(rep, address);
assertThat(rc, is(true));
int services = 5;
List<SocketBase> reqs = new ArrayList<>();
for (int peer = 0; peer < services; ++peer) {
SocketBase sender = ZMQ.socket(ctx, connectType);
assertThat(sender, notNullValue());
reqs.add(sender);
rc = ZMQ.setSocketOption(sender, ZMQ.ZMQ_RCVTIMEO, timeout);
assertThat(rc, is(true));
String host = (String) ZMQ.getSocketOptionExt(rep, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
rc = ZMQ.connect(sender, host);
assertThat(rc, is(true));
}
rc = sendSeq(reqs.get(0), "A");
assertThat(rc, is(true));
recvSeq(rep, "A");
rc = sendSeq(rep, "A");
assertThat(rc, is(true));
recvSeq(reqs.get(0), "A");
rc = sendSeq(reqs.get(0), "A");
assertThat(rc, is(true));
recvSeq(rep, "A");
rc = sendSeq(rep, "A");
assertThat(rc, is(true));
recvSeq(reqs.get(0), "A");
boolean someoneFixThis = false;
// TODO V4 review this test (breaking in libzmq): there is no guarantee about the order of the replies.
if (someoneFixThis) {
// send N requests
for (int peer = 0; peer < services; ++peer) {
sendSeq(reqs.get(peer), "B" + peer);
}
Set<String> replies = new HashSet<>();
// handle N requests
for (int peer = 0; peer < services; ++peer) {
Msg msg = ZMQ.recv(rep, 0);
assertThat(msg, notNullValue());
String reply = new String(msg.data(), ZMQ.CHARSET);
replies.add(reply);
sendSeq(rep, reply);
}
for (int peer = 0; peer < services; ++peer) {
Msg msg = ZMQ.recv(reqs.get(peer), 0);
assertThat(msg, notNullValue());
String reply = new String(msg.data(), ZMQ.CHARSET);
replies.remove(reply);
}
assertThat(replies.size(), is(0));
}
ZMQ.closeZeroLinger(rep);
for (SocketBase sender : reqs) {
ZMQ.closeZeroLinger(sender);
}
// Wait for disconnects.
ZMQ.msleep(100);
}
use of zmq.Msg in project jeromq by zeromq.
the class PushPullSpecTest method fairQueueIn.
private void fairQueueIn(Ctx ctx, String address, int bindType, int connectType) throws IOException, InterruptedException {
// Server socket will accept connections
SocketBase pull = ZMQ.socket(ctx, bindType);
assertThat(pull, notNullValue());
boolean rc = ZMQ.bind(pull, 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);
String host = (String) ZMQ.getSocketOptionExt(pull, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
rc = ZMQ.connect(sender, host);
assertThat(rc, is(true));
}
// Wait for connections.
ZMQ.msleep(100);
Set<String> firstHalf = new HashSet<>();
Set<String> secondHalf = new HashSet<>();
// Send 2N messages
for (int peer = 0; peer < services; ++peer) {
sendSeq(senders.get(peer), "A" + peer);
firstHalf.add("A" + peer);
sendSeq(senders.get(peer), "B" + peer);
secondHalf.add("B" + peer);
}
// Wait for data.
ZMQ.msleep(100);
// Expect to pull one from each first
for (int peer = 0; peer < services; ++peer) {
Msg msg = ZMQ.recv(pull, 0);
assertThat(msg, notNullValue());
assertThat(msg.size(), is(2));
firstHalf.remove(new String(msg.data(), ZMQ.CHARSET));
}
assertThat(firstHalf.size(), is(0));
// And then get the second batch
for (int peer = 0; peer < services; ++peer) {
Msg msg = ZMQ.recv(pull, 0);
assertThat(msg, notNullValue());
assertThat(msg.size(), is(2));
secondHalf.remove(new String(msg.data(), ZMQ.CHARSET));
}
assertThat(secondHalf.size(), is(0));
ZMQ.closeZeroLinger(pull);
for (SocketBase sender : senders) {
ZMQ.closeZeroLinger(sender);
}
// Wait for disconnects.
ZMQ.msleep(100);
}
use of zmq.Msg in project jeromq by zeromq.
the class DistTest method testMatch.
@Test
public void testMatch() {
dist.attach(second);
assertThat(dist.matching(), is(0));
// simulate HWM
second.write(new Msg());
// mark the pipe as not active and not eligible
dist.sendToAll(new Msg());
dist.match(first);
assertThat(dist.matching(), is(1));
dist.match(second);
// second pipe is not a matching one
assertThat(dist.matching(), is(1));
}
use of zmq.Msg in project jeromq by zeromq.
the class DistTest method testActivatedAfterReachingHWM.
@Test
public void testActivatedAfterReachingHWM() {
dist.attach(second);
assertThat(dist.eligible(), is(2));
assertThat(dist.active(), is(2));
// simulate HWM
second.write(new Msg());
// mark the pipe as not active and not eligible
dist.sendToAll(new Msg());
// now, second pipe has been put out of the active and eligible ones
assertThat(dist.eligible(), is(1));
assertThat(dist.active(), is(1));
// reactivate pipe having reached HWM
dist.activated(second);
assertThat(dist.eligible(), is(2));
assertThat(dist.active(), is(2));
}
use of zmq.Msg in project jeromq by zeromq.
the class DistTest method testAttachedWhenSendingMultipartMessage.
@Test
public void testAttachedWhenSendingMultipartMessage() {
Msg msg = new Msg();
msg.setFlags(Msg.MORE);
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 not have changed, as we are in the middle a sending a multi-part message
assertThat(dist.active(), is(1));
// no change in eligible/active states
dist.activated(second);
assertThat(dist.eligible(), is(2));
assertThat(dist.active(), is(1));
dist.sendToAll(new Msg());
// end of multi-part message
assertThat(dist.eligible(), is(2));
assertThat(dist.active(), is(2));
}
Aggregations