package fi.jyu.zmq;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Socket;
import static org.testng.Assert.*;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class ZmqAssociationTest {
ZMQ.Context context;
private Collection<Socket> sockets;
@BeforeMethod
public void setUp() {
sockets = new ArrayList<Socket>();
context = ZMQ.context(1);
}
@AfterMethod
public void tearDown() {
closeSockets();
terminateContext();
}
@Test
public void bindAndConnectAssociationsExists() throws URISyntaxException {
ZmqAssociation b = ZmqAssociation.fromURI("bind:inproc://1");
ZmqAssociation c = ZmqAssociation.fromURI("connect:inproc://1");
Socket reqSocket = createSocket(ZMQ.REQ);
Socket repSocket = createSocket(ZMQ.REP);
b.associate(reqSocket);
c.associate(repSocket);
byte[] msg = new byte[]{1};
reqSocket.send(msg, 0);
byte[] recv = repSocket.recv(0);
assertEquals(msg, recv);
}
@Test
public void bindReversing() throws URISyntaxException {
ZmqAssociation bindAssoc = ZmqAssociation.fromURI("bind:inproc://2");
Socket reqSocket = createSocket(ZMQ.REQ);
Socket repSocket = createSocket(ZMQ.REP);
bindAssoc.associate(reqSocket);
bindAssoc.reverse().associate(repSocket);
byte[] msg = new byte[]{1};
reqSocket.send(msg, 0);
byte[] recv = repSocket.recv(0);
assertEquals(msg, recv);
}
@Test
public void connectReversing() throws URISyntaxException {
ZmqAssociation connectAssoc = ZmqAssociation.fromURI("connect:inproc://3");
Socket reqSocket = createSocket(ZMQ.REQ);
Socket repSocket = createSocket(ZMQ.REP);
connectAssoc.reverse().associate(repSocket);
connectAssoc.associate(reqSocket);
byte[] msg = new byte[]{1};
reqSocket.send(msg, 0);
byte[] recv = repSocket.recv(0);
assertEquals(msg, recv);
}
@Test
public void bindReversingAllInterfacesToLocalhost() throws URISyntaxException {
ZmqAssociation assoc = ZmqAssociation.fromURI("bind:tcp://*:1234");
assertEquals(assoc.reverse().toString(), "connect:tcp://localhost:1234");
}
@Test
public void bindReversingInProcDoesNotReplaceStar() throws URISyntaxException {
ZmqAssociation assoc = ZmqAssociation.fromURI("bind:inproc://*:1234");
assertEquals(assoc.reverse().toString(), "connect:inproc://*:1234");
}
@Test
public void bindReversingIpcDoesNotReplaceStar() throws URISyntaxException {
ZmqAssociation assoc = ZmqAssociation.fromURI("bind:ipc://*:1234");
assertEquals(assoc.reverse().toString(), "connect:ipc://*:1234");
}
@Test(expectedExceptions=UnknownAssociationTypeException.class)
public void unknowAssociationTypeThrowsError() throws URISyntaxException {
ZmqAssociation.fromURI("bind2:ipc://*:1234");
}
@Test
public void multipleAddressesCanBeDefined() throws URISyntaxException {
ZmqAssociation assoc = ZmqAssociation.fromURI("connect:inproc://place1;connect:inproc://place2");
Socket pushSocket1 = createSocket(ZMQ.PUSH);
Socket pushSocket2 = createSocket(ZMQ.PUSH);
Socket pullSocket = createSocket(ZMQ.PULL);
pushSocket1.bind("inproc://place1");
pushSocket2.bind("inproc://place2");
assoc.associate(pullSocket);
byte[] msg1 = new byte[]{1};
byte[] msg2 = new byte[]{2, 2};
pushSocket1.send(msg1, 0);
byte[] recv1 = pullSocket.recv(0);
assertEquals(recv1, msg1);
pushSocket2.send(msg2, 0);
byte[] recv2 = pullSocket.recv(0);
assertEquals(recv2, msg2);
}
protected void terminateContext() {
context.term();
}
private void closeSockets() {
for (Socket socket : sockets) {
socket.close();
}
}
protected Socket createSocket(int socketType) {
final Socket socket = context.socket(socketType);
sockets.add(socket);
return socket;
}
}