use of zmq.SocketBase in project jeromq by zeromq.
the class RepSpecTest method envelope.
private void envelope(Ctx ctx, String address, int bindType, int connectType) throws IOException, InterruptedException {
SocketBase rep = ZMQ.socket(ctx, bindType);
boolean rc = ZMQ.bind(rep, address);
assertThat(rc, is(true));
SocketBase dealer = ZMQ.socket(ctx, connectType);
assertThat(dealer, notNullValue());
String host = (String) ZMQ.getSocketOptionExt(rep, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
rc = ZMQ.connect(dealer, host);
assertThat(rc, is(true));
// minimal envelope
sendSeq(dealer, null, "A");
recvSeq(rep, "A");
sendSeq(rep, "A");
recvSeq(dealer, null, "A");
// big envelope
sendSeq(dealer, "X", "Y", null, "A");
recvSeq(rep, "A");
sendSeq(rep, "A");
recvSeq(dealer, "X", "Y", null, "A");
ZMQ.closeZeroLinger(rep);
ZMQ.closeZeroLinger(dealer);
// Wait for disconnects.
ZMQ.msleep(100);
}
use of zmq.SocketBase 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.SocketBase 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.SocketBase in project jeromq by zeromq.
the class PubSubHwmTest method assertHwmSubscription.
private void assertHwmSubscription(int subscribed, String endpoint) {
int allSubscriptions = 30_000;
int subscriptionNotSent = allSubscriptions - 1;
Ctx ctx = ZMQ.createContext();
SocketBase pub = ctx.createSocket(ZMQ.ZMQ_PUB);
assertThat(pub, notNullValue());
boolean rc = ZMQ.bind(pub, endpoint);
assertThat(rc, is(true));
String host = (String) ZMQ.getSocketOptionExt(pub, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
// Set up connect socket
SocketBase sub = ctx.createSocket(ZMQ.ZMQ_SUB);
assertThat(sub, notNullValue());
rc = ZMQ.setSocketOption(sub, ZMQ.ZMQ_RCVTIMEO, 100);
assertThat(rc, is(true));
// send a lot of subscriptions, far beyond the HWM
int idx = 0;
for (; idx < allSubscriptions; ++idx) {
rc = ZMQ.setSocketOption(sub, ZMQ.ZMQ_SUBSCRIBE, Wire.putUInt32(idx));
// at some point, sending will trigger the HWM and subscription will be dropped
assertThat(rc, is(true));
}
rc = ZMQ.connect(sub, host);
assertThat(rc, is(true));
ZMQ.msleep(500);
// Send messages
int sent = ZMQ.send(pub, Wire.putUInt32(1), 0);
assertThat(sent, is(4));
sent = ZMQ.send(pub, Wire.putUInt32(subscribed), 0);
assertThat(sent, is(4));
sent = ZMQ.send(pub, Wire.putUInt32(subscriptionNotSent), 0);
assertThat(sent, is(4));
ZMQ.msleep(500);
Msg msg = ZMQ.recv(sub, 0);
assertThat(msg, notNullValue());
assertThat(msg.data(), is(Wire.putUInt32(1)));
msg = ZMQ.recv(sub, 0);
assertThat(msg, notNullValue());
assertThat(msg.data(), is(Wire.putUInt32(subscribed)));
msg = ZMQ.recv(sub, 0);
assertThat(msg, nullValue());
// Clean up
ZMQ.close(sub);
ZMQ.close(pub);
ZMQ.term(ctx);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class PubSubHwmTest method testBlocking.
private int testBlocking(int sendHwm, int msgCnt) {
Ctx ctx = ZMQ.createContext();
// Set up bind socket
SocketBase pub = ctx.createSocket(ZMQ.ZMQ_PUB);
boolean rc = ZMQ.bind(pub, "inproc://a");
assertThat(rc, is(true));
// Set up connect socket
SocketBase sub = ctx.createSocket(ZMQ.ZMQ_SUB);
rc = ZMQ.connect(sub, "inproc://a");
assertThat(rc, is(true));
// set a hwm on publisher
rc = ZMQ.setSocketOption(pub, ZMQ.ZMQ_SNDHWM, sendHwm);
assertThat(rc, is(true));
rc = ZMQ.setSocketOption(pub, ZMQ.ZMQ_XPUB_NODROP, true);
assertThat(rc, is(true));
rc = ZMQ.setSocketOption(sub, ZMQ.ZMQ_SUBSCRIBE, new byte[0]);
assertThat(rc, is(true));
// Send until we block
int sendCount = 0;
int recvCount = 0;
while (sendCount < msgCnt) {
int ret = ZMQ.send(pub, "", ZMQ.ZMQ_DONTWAIT);
if (ret == 0) {
++sendCount;
} else if (ret == -1) {
assertThat(pub.errno(), is(ZError.EAGAIN));
recvCount += receive(sub);
assertThat(sendCount, is(recvCount));
}
}
recvCount += receive(sub);
// Clean up
ZMQ.close(sub);
ZMQ.close(pub);
ZMQ.term(ctx);
return recvCount;
}
Aggregations