Package com.trendmicro.mist.util

Examples of com.trendmicro.mist.util.OpenMQTestBroker


    @SuppressWarnings("unchecked")
    public void testUpdateRoute() throws SecurityException, NoSuchMethodException, MistException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InterruptedException, ClassNotFoundException, InstantiationException, IOException, JMSException, CODIException {
        /**
         * Setup open mq
         */
        OpenMQTestBroker brk = new OpenMQTestBroker("test", 9876);
        brk.start();
        brk.registerOnZk();
        for(int i = 0; i < 10; i++) {
            if(brokerFarm.getBrokerCount() == 1)
                break;
            Utils.justSleep(500);
        }
        assertEquals(1, brokerFarm.getBrokerCount());
        assertTrue(Utils.checkSocketConnectable("localhost", 9876));
       
        GateTalk.Session sessConfig = genSessionConfig("", "", "", "", "");
        GateTalk.Client clientConfig = genClientConfig("foo.out");

        ProducerSession sess = new ProducerSession(0, sessConfig);
        Method updateRoute = ProducerSession.class.getDeclaredMethod("updateRoute", new Class[] {});
        updateRoute.setAccessible(true);
        Field routeCacheMapField = ProducerSession.class.getDeclaredField("routeCacheMap");
        routeCacheMapField.setAccessible(true);
        HashMap<String, List<Exchange>> routeCacheMap = (HashMap<String, List<Exchange>>) (routeCacheMapField.get(sess));
        Field ttlField = ProducerSession.class.getDeclaredField("ROUTE_CACHE_TTL_MILLIS");
        ttlField.setAccessible(true);
        long cacheTTL = ttlField.getLong(sess);

        /**
         * The route cache should be empty at the beginning
         */
        assertTrue(routeCacheMap.isEmpty());

        Exchange fooOutEx = new Exchange("queue:foo.out");
        sess.addClient(clientConfig);
        Vector<Exchange> destList = new Vector<Exchange>();
        destList.add(new Exchange("bar.in"));
        RouteFarm.getInstance().getRouteTable().put(fooOutEx.getName(), destList);
        updateRoute.invoke(sess, new Object[] {});
        assertEquals("queue:bar.in", routeCacheMap.get(fooOutEx).get(0).toString());

        /**
         * Update the routing table and invoke updateRoute before the cache
         * expires, and the cache should remain the same
         */
        destList.add(new Exchange("log.in"));
        updateRoute.invoke(sess, new Object[] {});
        assertEquals("queue:bar.in", routeCacheMap.get(fooOutEx).get(0).toString());
        assertEquals(1, routeCacheMap.get(fooOutEx).size());

        /**
         * Wait after the cache expires and invoke updateRoute again
         */
        Thread.sleep(cacheTTL);
        updateRoute.invoke(sess, new Object[] {});
        assertEquals("queue:bar.in", routeCacheMap.get(fooOutEx).get(0).toString());
        assertEquals("queue:log.in", routeCacheMap.get(fooOutEx).get(1).toString());

        /**
         * Clear the routing table
         */
        RouteFarm.getInstance().getRouteTable().clear();
        Thread.sleep(cacheTTL);
        updateRoute.invoke(sess, new Object[] {});
        assertTrue(routeCacheMap.isEmpty());
       
        brk.stop();
    }
View Full Code Here


    public void testSendMessageOK() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InterruptedException, IOException, MistException, JMSServiceException, JMSException, CODIException {
        /**
         * Setup open mq
         */
        OpenMQTestBroker brk = new OpenMQTestBroker("test", 9876);
        brk.start();
        brk.registerOnZk();
        for(int i = 0; i < 10; i++) {
            if(brokerFarm.getBrokerCount() == 1)
                break;
            Utils.justSleep(500);
        }
        assertEquals(1, brokerFarm.getBrokerCount());
        assertTrue(Utils.checkSocketConnectable("localhost", 9876));

        /**
         * Setup session, socket
         */
        GateTalk.Session sessConfig = genSessionConfig("", "", "", "", "");
        ProducerSession sess = new ProducerSession(0, sessConfig);
        GateTalk.Client clientConfig = genClientConfig("foo.out");
        sess.addClient(clientConfig);
        sess.attach(GateTalk.Request.Role.SINK);
        Socket socket = new Socket("localhost", sess.getCommPort());
        for(int i = 0; i < 10; i++) {
            if(sess.isReady())
                break;
            Utils.justSleep(500);
        }
        assertTrue(sess.isReady());

        Packet packet = new Packet();
        BufferedInputStream socketInput = new BufferedInputStream(socket.getInputStream());
        BufferedOutputStream socketOutput = new BufferedOutputStream(socket.getOutputStream());

        /**
         * Send a message to foo.out
         */
        MistMessage.MessageBlock msg = MistMessage.MessageBlock.newBuilder().setId("queue:foo.out").setMessage(ByteString.copyFrom("test".getBytes())).build();
        packet.setPayload(msg.toByteArray());
        packet.write(socketOutput);
        packet.read(socketInput);
        assertTrue(GateTalk.Response.newBuilder().mergeFrom(packet.getPayload()).build().getSuccess());
        byte[] recvMsg = brk.getMessage(true, "foo.out");
        assertEquals("test", new String(recvMsg));

        /**
         * Send a message to bar.out
         */
        msg = MistMessage.MessageBlock.newBuilder().setId("queue:bar.out").setMessage(ByteString.copyFrom("test-bar".getBytes())).build();
        packet.setPayload(msg.toByteArray());
        packet.write(socketOutput);
        packet.read(socketInput);
        assertTrue(GateTalk.Response.newBuilder().mergeFrom(packet.getPayload()).build().getSuccess());
        recvMsg = brk.getMessage(true, "bar.out");
        assertEquals("test-bar", new String(recvMsg));

        /**
         * Test Local Forwarding: foo.out->bar.in, ,log.in
         */
        Vector<Exchange> destList = new Vector<Exchange>();
        destList.add(new Exchange("bar.in"));
        destList.add(new Exchange(""));
        destList.add(new Exchange("log.in"));
        RouteFarm.getInstance().getRouteTable().put("foo.out", destList);
        // Wait until local routing cache expires
        Utils.justSleep(2000);
        msg = MistMessage.MessageBlock.newBuilder().setId("queue:foo.out").setMessage(ByteString.copyFrom("test-route".getBytes())).build();
        packet.setPayload(msg.toByteArray());
        packet.write(socketOutput);
        packet.read(socketInput);
        assertTrue(GateTalk.Response.newBuilder().mergeFrom(packet.getPayload()).build().getSuccess());
        recvMsg = brk.getMessage(true, "bar.in");
        assertEquals("test-route", new String(recvMsg));
        recvMsg = brk.getMessage(true, "log.in");
        assertEquals("test-route", new String(recvMsg));
        brk.stop();
    }
View Full Code Here

    public void testAddClient() throws MistException, ClassNotFoundException, IllegalAccessException, InstantiationException, InterruptedException, IOException, JMSException, CODIException {
        /**
         * Setup open mq
         */
        OpenMQTestBroker brk = new OpenMQTestBroker("test", 9876);
        brk.start();
        brk.registerOnZk();
        for(int i = 0; i < 10; i++) {
            if(brokerFarm.getBrokerCount() == 1)
                break;
            Utils.justSleep(500);
        }
        assertEquals(1, brokerFarm.getBrokerCount());
        assertTrue(Utils.checkSocketConnectable("localhost", 9876));

        GateTalk.Session sessConfig = genSessionConfig("", "", "", "", "");
        Session sess = new ProducerSession(0, sessConfig);

        /**
         * Test add a normal queue client
         */
        GateTalk.Client clientConfig = genClientConfig("foo.out", true, true);
        sess.addClient(clientConfig);
        assertEquals(clientConfig, sess.findClient(new Exchange("queue:foo.out")).getConfig());

        /**
         * Test re-mount the same exchange
         */
        Exception ex = null;
        try {
            sess.addClient(clientConfig);
        }
        catch(MistException e) {
            ex = e;
        }
        assertEquals(MistException.ALREADY_MOUNTED, ex.getMessage());

        /**
         * Test add a normal topic client
         */
        clientConfig = genClientConfig("foo.out", true, false);
        sess.addClient(clientConfig);
        assertEquals(clientConfig, sess.findClient(new Exchange("topic:foo.out")).getConfig());

        /**
         * Test using mist-source to mount a producer session
         */
        clientConfig = genClientConfig("bar.out", false, true);
        ex = null;
        try {
            sess.addClient(clientConfig);
        }
        catch(MistException e) {
            ex = e;
        }
        assertEquals(MistException.INCOMPATIBLE_TYPE_SINK, ex.getMessage());

        brk.stop();
    }
View Full Code Here

    public void testOpen() throws MistException, JMSException, CODIException, ClassNotFoundException, IllegalAccessException, InstantiationException, InterruptedException, IOException, JMSServiceException {
        /**
         * Setup open mq
         */
        OpenMQTestBroker brk = new OpenMQTestBroker("test", 9876);
        brk.start();
        brk.registerOnZk();
        for(int i = 0; i < 10; i++) {
            if(brokerFarm.getBrokerCount() == 1)
                break;
            Utils.justSleep(500);
        }
        assertEquals(1, brokerFarm.getBrokerCount());
        assertTrue(Utils.checkSocketConnectable("localhost", 9876));

        GateTalk.Session sessConfig = genSessionConfig("", "", "", "", "");
        Session sess = new ProducerSession(0, sessConfig);

        /**
         * Test add normal a queue client and open
         */
        GateTalk.Client clientConfig = genClientConfig("foo.out", true, true);
        Client c = sess.addClient(clientConfig);
        assertEquals(clientConfig, sess.findClient(new Exchange("queue:foo.out")).getConfig());
        sess.open(false);
        MessageProducer producer = sess.findClient(new Exchange("queue:foo.out")).getProducer();
        assertNotNull(producer);

        /**
         * Deliver a message
         */
        BytesMessage msg = c.getJMSSession().createBytesMessage();
        msg.writeBytes("test".getBytes());
        producer.send(msg);
        assertEquals("test", new String(brk.getMessage(true, "foo.out")));

        /**
         * Test add another queue client
         */
        clientConfig = genClientConfig("bar.out", true, true);
        c = sess.addClient(clientConfig);
        assertEquals(clientConfig, sess.findClient(new Exchange("queue:bar.out")).getConfig());
        sess.open(false);
        producer = sess.findClient(new Exchange("queue:bar.out")).getProducer();
        assertNotNull(producer);

        msg = c.getJMSSession().createBytesMessage();
        msg.writeBytes("test_bar".getBytes());
        producer.send(msg);
        assertEquals("test_bar", new String(brk.getMessage(true, "bar.out")));

        brk.stop();
    }
View Full Code Here

    public void testOnMessage() throws MistException, UnknownHostException, IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, InterruptedException, JMSException, CODIException, JMSServiceException {
        /**
         * Setup open mq
         */
        OpenMQTestBroker brk = new OpenMQTestBroker("test", 9876);
        brk.start();
        brk.registerOnZk();
        for(int i = 0; i < 10; i++) {
            if(brokerFarm.getBrokerCount() == 1)
                break;
            Utils.justSleep(500);
        }
        assertEquals(1, brokerFarm.getBrokerCount());
        assertTrue(Utils.checkSocketConnectable("localhost", 9876));

        /**
         * Session setup and attach
         */
        GateTalk.Session sessConfig = genSessionConfig("", "", "", "", "");
        ConsumerSession sess = new ConsumerSession(0, sessConfig);
        sess.attach(GateTalk.Request.Role.SOURCE);
        Socket socket = new Socket("localhost", sess.getCommPort());
        assertTrue(socket.isConnected());
        for(int i = 0; i < 10; i++) {
            if(sess.isReady())
                break;
            Utils.justSleep(500);
        }
        assertTrue(sess.isReady());

        Packet packet = new Packet();
        BufferedInputStream socketInput = new BufferedInputStream(socket.getInputStream());
        BufferedOutputStream socketOutput = new BufferedOutputStream(socket.getOutputStream());

        /**
         * Get a message and ack
         */
        brk.sendMessage(true, "foo.in", "foo".getBytes());
        BytesMessage msgin = brk.getBytesMessage(true, "foo.in");
        new OnMessageRunner(sess, msgin).start();
        packet.read(socketInput);
        MistMessage.MessageBlock mBlock = MistMessage.MessageBlock.newBuilder().mergeFrom(packet.getPayload()).build();
        assertEquals("queue:foo.in", mBlock.getId());
        assertEquals("foo", new String(mBlock.getMessage().toByteArray()));

        packet.setPayload(GateTalk.Response.newBuilder().setSuccess(true).build().toByteArray());
        packet.write(socketOutput);

        brk.stop();
    }
View Full Code Here

    public void testReceiveMessage() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InterruptedException, IOException, CODIException, JMSException, MistException, JMSServiceException {
        /**
         * Setup open mq
         */
        OpenMQTestBroker brk = new OpenMQTestBroker("test", 9876);
        brk.start();
        brk.registerOnZk();
        for(int i = 0; i < 10; i++) {
            if(brokerFarm.getBrokerCount() == 1)
                break;
            Utils.justSleep(500);
        }
        assertEquals(1, brokerFarm.getBrokerCount());
        assertTrue(Utils.checkSocketConnectable("localhost", 9876));

        /**
         * Session setup and attach and mount foo.in
         */
        GateTalk.Session sessConfig = genSessionConfig("", "", "", "", "");
        ConsumerSession sess = new ConsumerSession(0, sessConfig);
        GateTalk.Client clientConfig = genClientConfig("foo.in");
        // sess.addClient(new Client(clientConfig, sessConfig));
        sess.addClient(clientConfig);
        sess.attach(GateTalk.Request.Role.SOURCE);
        Socket socket = new Socket("localhost", sess.getCommPort());
        assertTrue(socket.isConnected());
        for(int i = 0; i < 10; i++) {
            if(sess.isReady())
                break;
            Utils.justSleep(500);
        }
        assertTrue(sess.isReady());

        Packet packet = new Packet();
        BufferedInputStream socketInput = new BufferedInputStream(socket.getInputStream());
        BufferedOutputStream socketOutput = new BufferedOutputStream(socket.getOutputStream());

        /**
         * Send and receive a message from foo.in
         */
        brk.sendMessage(true, "foo.in", "foo".getBytes());
        packet.read(socketInput);
        MistMessage.MessageBlock mBlock = MistMessage.MessageBlock.newBuilder().mergeFrom(packet.getPayload()).build();
        assertEquals("queue:foo.in", mBlock.getId());
        assertEquals("foo", new String(mBlock.getMessage().toByteArray()));

        packet.setPayload(GateTalk.Response.newBuilder().setSuccess(true).build().toByteArray());
        packet.write(socketOutput);

        /**
         * Test dynamic mount bar.in
         */
        brk.sendMessage(true, "bar.in", "bar".getBytes());
        clientConfig = genClientConfig("bar.in");
        sess.addClient(clientConfig);

        packet.read(socketInput);
        mBlock = MistMessage.MessageBlock.newBuilder().mergeFrom(packet.getPayload()).build();
        assertEquals("queue:bar.in", mBlock.getId());
        assertEquals("bar", new String(mBlock.getMessage().toByteArray()));

        packet.setPayload(GateTalk.Response.newBuilder().setSuccess(true).build().toByteArray());
        packet.write(socketOutput);

        brk.stop();
    }
View Full Code Here

TOP

Related Classes of com.trendmicro.mist.util.OpenMQTestBroker

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.