Search in sources :

Example 26 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class TestZMQ method testSocketDoubleClose.

@Test(timeout = 1000)
public void testSocketDoubleClose() {
    Context ctx = ZMQ.context(1);
    Socket socket = ctx.socket(ZMQ.PUSH);
    socket.close();
    socket.close();
    ctx.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 27 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class TestZPoller method testUseNull.

@Test
public void testUseNull() throws IOException {
    ZPoller poller = new ZPoller(new ZStar.VerySimpleSelectorCreator().create());
    SelectableChannel channel = null;
    // ctx.createSocket(ZMQ.SUB);
    Socket socket = null;
    boolean rc = false;
    rc = poller.register(socket, ZPoller.IN);
    Assert.assertFalse("Registering a null socket was successful", rc);
    rc = poller.register(channel, ZPoller.OUT);
    Assert.assertFalse("Registering a null channel was successful", rc);
    int events = poller.poll(10);
    Assert.assertEquals("reading event on without sockets", 0, events);
    rc = poller.isReadable(socket);
    Assert.assertFalse("checking read event on a null socket was successful", rc);
    rc = poller.writable(socket);
    Assert.assertFalse("checking write event on a null socket was successful", rc);
    rc = poller.readable(channel);
    Assert.assertFalse("checking read event on a null channel was successful", rc);
    rc = poller.isWritable(channel);
    Assert.assertFalse("checking write event on a null channel was successful", rc);
    EventsHandler global = null;
    poller.setGlobalHandler(global);
    EventsHandler handler = null;
    rc = poller.register(socket, handler, ZPoller.ERR);
    Assert.assertFalse("Register with handler on a null socket was successful", rc);
    rc = poller.register(channel, ZPoller.ERR);
    Assert.assertFalse("Register with handler on a null channel was successful", rc);
    events = poller.poll(10);
    Assert.assertEquals("reading event with events handlers without sockets", 0, events);
    poller.close();
}
Also used : SelectableChannel(java.nio.channels.SelectableChannel) EventsHandler(org.zeromq.ZPoller.EventsHandler) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 28 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class TestZThread method testFork.

@Test
public void testFork() {
    ZContext ctx = new ZContext();
    ZThread.IAttachedRunnable attached = new ZThread.IAttachedRunnable() {

        @Override
        public void run(Object[] args, ZContext ctx, Socket pipe) {
            //  Create a socket to check it'll be automatically deleted
            ctx.createSocket(ZMQ.PUSH);
            pipe.recvStr();
            pipe.send("pong");
        }
    };
    Socket pipe = ZThread.fork(ctx, attached);
    assert (pipe != null);
    pipe.send("ping");
    String pong = pipe.recvStr();
    Assert.assertEquals(pong, "pong");
    //  Everything should be cleanly closed now
    ctx.destroy();
}
Also used : Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 29 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class TestZActor method testRecreateAgent.

@Test
@Ignore
public void testRecreateAgent() {
    ZActor.Actor acting = new ZActor.SimpleActor() {

        private int counter = 0;

        @Override
        public List<Socket> createSockets(ZContext ctx, Object[] args) {
            ++counter;
            System.out.print(".Acting Ready for a hello world.");
            assert ("TEST".equals(args[0]));
            return super.createSockets(ctx, args);
        }

        @Override
        public boolean backstage(Socket pipe, ZPoller poller, int events) {
            String string = pipe.recvStr();
            if ("HELLO".equals(string)) {
                System.out.print("Hi! ");
                pipe.send("WORLD", ZMQ.SNDMORE);
                pipe.send(Integer.toString(counter));
                return false;
            }
            return true;
        }

        @Override
        public boolean destroyed(ZContext ctx, Socket pipe, ZPoller poller) {
            if (counter == 2) {
                System.out.print(".Acting Finished.");
                return false;
            }
            // recreate a new agent
            return true;
        }
    };
    ZContext context = new ZContext();
    ZActor actor = new ZActor(context, new ZAgent.VerySimpleSelectorCreator(), acting, UUID.randomUUID().toString(), Arrays.asList("TEST").toArray());
    ZAgent agent = actor.agent();
    agent = actor;
    agent = actor.agent();
    Socket pipe = agent.pipe();
    boolean rc = pipe.send("HELLO");
    assert (rc);
    ZMsg msg = actor.recv();
    String world = msg.popString();
    String counter = msg.popString();
    assert ("WORLD".equals(world));
    assert ("1".equals(counter));
    rc = actor.send("HELLO");
    assert (rc);
    msg = agent.recv();
    Assert.assertNotNull("unable to receive a message from an actor", msg);
    world = msg.popString();
    counter = msg.popString();
    assert (msg != null);
    assert ("WORLD".equals(world));
    assert ("2".equals(counter));
    msg = agent.recv();
    Assert.assertNull("Able to receive a message from a locked actor", msg);
    rc = agent.sign();
    Assert.assertFalse("Locked actor is still here", rc);
    rc = agent.send("whatever");
    Assert.assertFalse("Able to send a message to a locked actor", rc);
    try {
        rc = pipe.send("boom ?!");
        Assert.assertTrue("actor pipe was closed pretty fast", rc);
    } catch (ZMQException e) {
        int errno = e.getErrorCode();
        Assert.assertEquals("Expected exception has the wrong code", ZError.ETERM, errno);
    }
    context.close();
    System.out.println();
}
Also used : Actor(org.zeromq.ZActor.Actor) Socket(org.zeromq.ZMQ.Socket) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 30 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class TestZLoop method testZLoopEndReactorFromTimer.

@Test(timeout = 1000)
public void testZLoopEndReactorFromTimer() {
    int rc = 0;
    ZLoop loop = new ZLoop(ctx);
    assert (loop != null);
    ZLoop.IZLoopHandler timerEvent = new ZLoop.IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            ((Socket) arg).send("PING", 0);
            return 0;
        }
    };
    ZLoop.IZLoopHandler socketEvent = new ZLoop.IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            //  After 10 msecs, fire an event that ends the reactor
            ZLoop.IZLoopHandler shutdownEvent = new ZLoop.IZLoopHandler() {

                @Override
                public int handle(ZLoop loop, PollItem item, Object arg) {
                    received = ((Socket) arg).recvStr(0);
                    //  Just end the reactor
                    return -1;
                }
            };
            loop.addTimer(10, 1, shutdownEvent, arg);
            return 0;
        }
    };
    //  Fire event that sends a ping message to output
    loop.addTimer(0, 1, timerEvent, input);
    //  When we get the ping message, end the reactor
    PollItem pollInput = new PollItem(output, Poller.POLLIN);
    rc = loop.addPoller(pollInput, socketEvent, output);
    Assert.assertEquals(0, rc);
    loop.start();
    loop.removePoller(pollInput);
    Assert.assertEquals("PING", received);
}
Also used : PollItem(org.zeromq.ZMQ.PollItem) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Aggregations

Socket (org.zeromq.ZMQ.Socket)84 Context (org.zeromq.ZMQ.Context)32 ZContext (org.zeromq.ZContext)30 Test (org.junit.Test)26 Poller (org.zeromq.ZMQ.Poller)20 ZMsg (org.zeromq.ZMsg)14 Random (java.util.Random)13 ZFrame (org.zeromq.ZFrame)13 PollItem (org.zeromq.ZMQ.PollItem)6 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 SelectableChannel (java.nio.channels.SelectableChannel)3 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 Entry (java.util.Map.Entry)2 Ignore (org.junit.Ignore)2 Actor (org.zeromq.ZActor.Actor)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1