FileDocCategorySizeDatePackage
TestOrderInterceptor.javaAPI DocApache Tomcat 6.0.147170Fri Jul 20 04:20:34 BST 2007org.apache.catalina.tribes.test.interceptors

TestOrderInterceptor

public class TestOrderInterceptor extends TestCase

Fields Summary
org.apache.catalina.tribes.group.GroupChannel[]
channels
org.apache.catalina.tribes.group.interceptors.OrderInterceptor[]
orderitcs
MangleOrderInterceptor[]
mangleitcs
TestListener[]
test
int
channelCount
Thread[]
threads
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        TestSuite suite = new TestSuite();
        suite.addTestSuite(TestOrderInterceptor.class);
        suite.run(new TestResult());
    
protected voidsetUp()

         
        System.out.println("Setup");
        super.setUp();
        channels = new GroupChannel[channelCount];
        orderitcs = new OrderInterceptor[channelCount];
        mangleitcs = new MangleOrderInterceptor[channelCount];
        test = new TestListener[channelCount];
        threads = new Thread[channelCount];
        for ( int i=0; i<channelCount; i++ ) {
            channels[i] = new GroupChannel();
            
            orderitcs[i] = new OrderInterceptor();
            mangleitcs[i] = new MangleOrderInterceptor();
            orderitcs[i].setExpire(Long.MAX_VALUE);
            channels[i].addInterceptor(orderitcs[i]);
            channels[i].addInterceptor(mangleitcs[i]);
            test[i] = new TestListener(i);
            channels[i].addChannelListener(test[i]);
            final int j = i;
            threads[i] = new Thread() {
                public void run() {
                    try {
                        channels[j].start(Channel.DEFAULT);
                        Thread.sleep(50);
                    } catch (Exception x) {
                        x.printStackTrace();
                    }
                }
            };
        }
        for ( int i=0; i<channelCount; i++ ) threads[i].start();
        for ( int i=0; i<channelCount; i++ ) threads[i].join();
        Thread.sleep(1000);
    
protected voidtearDown()

        System.out.println("tearDown");
        super.tearDown();
        for ( int i=0; i<channelCount; i++ ) {
            channels[i].stop(Channel.DEFAULT);
        }
    
public voidtestOrder1()

        Member[] dest = channels[0].getMembers();
        final AtomicInteger value = new AtomicInteger(0);
        for ( int i=0; i<100; i++ ) {
            channels[0].send(dest,new Integer(value.getAndAdd(1)),0);
        }
        Thread.sleep(5000);
        for ( int i=0; i<test.length; i++ ) {
            super.assertEquals(false,test[i].fail);
        }
    
public voidtestOrder2()

        final Member[] dest = channels[0].getMembers();
        final AtomicInteger value = new AtomicInteger(0);
        Runnable run = new Runnable() {
            public void run() {
                for (int i = 0; i < 100; i++) {
                    try {
                        synchronized (channels[0]) {
                            channels[0].send(dest, new Integer(value.getAndAdd(1)), 0);
                        }
                    }catch ( Exception x ) {
                        x.printStackTrace();
                        assertEquals(true,false);
                    }
                }
            }
        };
        Thread[] threads = new Thread[5];
        for (int i=0;i<threads.length;i++) {
            threads[i] = new Thread(run);
        }
        for (int i=0;i<threads.length;i++) {
            threads[i].start();
        }
        for (int i=0;i<threads.length;i++) {
            threads[i].join();
        }
        Thread.sleep(5000);
        for ( int i=0; i<test.length; i++ ) {
            super.assertEquals(false,test[i].fail);
        }