alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Tomcat example source code file (TestDataIntegrity.java)

This example Tomcat source code file (TestDataIntegrity.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Tomcat tags/keywords

checking, data, data, exception, exception, groupchannel, io, listener, member, member, messagedispatch15interceptor, serializable, testcase, thread, thread, util

The Tomcat TestDataIntegrity.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.catalina.tribes.test.channel;

import junit.framework.TestCase;
import java.io.Serializable;
import java.util.Random;
import java.util.Arrays;
import org.apache.catalina.tribes.ChannelListener;
import org.apache.catalina.tribes.Member;
import org.apache.catalina.tribes.group.GroupChannel;
import org.apache.catalina.tribes.test.channel.TestDataIntegrity.Listener;
import org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor;
import org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor;

/**
 * <p>Title: 

* * <p>Description:

* * <p>Company:

* * @author not attributable * @version 1.0 */ public class TestDataIntegrity extends TestCase { int msgCount = 500; int threadCount = 20; GroupChannel channel1; GroupChannel channel2; Listener listener1; int threadCounter = 0; protected void setUp() throws Exception { super.setUp(); channel1 = new GroupChannel(); channel1.addInterceptor(new MessageDispatch15Interceptor()); channel2 = new GroupChannel(); channel2.addInterceptor(new MessageDispatch15Interceptor()); listener1 = new Listener(); channel2.addChannelListener(listener1); channel1.start(GroupChannel.DEFAULT); channel2.start(GroupChannel.DEFAULT); } protected void tearDown() throws Exception { super.tearDown(); channel1.stop(GroupChannel.DEFAULT); channel2.stop(GroupChannel.DEFAULT); } public void testDataSendNO_ACK() throws Exception { System.err.println("Starting NO_ACK"); Thread[] threads = new Thread[threadCount]; for (int x=0; x<threads.length; x++ ) { threads[x] = new Thread() { public void run() { try { long start = System.currentTimeMillis(); for (int i = 0; i < msgCount; i++) channel1.send(new Member[] {channel2.getLocalMember(false)}, Data.createRandomData(),0); System.out.println("Thread["+this.getName()+"] sent "+msgCount+" messages in "+(System.currentTimeMillis()-start)+" ms."); }catch ( Exception x ) { x.printStackTrace(); return; } finally { threadCounter++; } } }; } for (int x=0; x<threads.length; x++ ) { threads[x].start();} for (int x=0; x<threads.length; x++ ) { threads[x].join();} //sleep for 50 sec, let the other messages in long start = System.currentTimeMillis(); while ( (System.currentTimeMillis()-start)<15000 && msgCount*threadCount!=listener1.count) Thread.sleep(500); System.err.println("Finished NO_ACK ["+listener1.count+"]"); assertEquals("Checking success messages.",msgCount*threadCount,listener1.count); } public void testDataSendASYNCM() throws Exception { System.err.println("Starting ASYNC MULTI THREAD"); Thread[] threads = new Thread[threadCount]; for (int x=0; x<threads.length; x++ ) { threads[x] = new Thread() { public void run() { try { long start = System.currentTimeMillis(); for (int i = 0; i < msgCount; i++) channel1.send(new Member[] {channel2.getLocalMember(false)}, Data.createRandomData(),GroupChannel.SEND_OPTIONS_ASYNCHRONOUS); System.out.println("Thread["+this.getName()+"] sent "+msgCount+" messages in "+(System.currentTimeMillis()-start)+" ms."); }catch ( Exception x ) { x.printStackTrace(); return; } finally { threadCounter++; } } }; } for (int x=0; x<threads.length; x++ ) { threads[x].start();} for (int x=0; x<threads.length; x++ ) { threads[x].join();} //sleep for 50 sec, let the other messages in long start = System.currentTimeMillis(); while ( (System.currentTimeMillis()-start)<15000 && msgCount*threadCount!=listener1.count) Thread.sleep(500); System.err.println("Finished ASYNC MULTI THREAD ["+listener1.count+"]"); assertEquals("Checking success messages.",msgCount*threadCount,listener1.count); } public void testDataSendASYNC() throws Exception { System.err.println("Starting ASYNC"); for (int i=0; i<msgCount; i++) channel1.send(new Member[] {channel2.getLocalMember(false)},Data.createRandomData(),GroupChannel.SEND_OPTIONS_ASYNCHRONOUS); //sleep for 50 sec, let the other messages in long start = System.currentTimeMillis(); while ( (System.currentTimeMillis()-start)<5000 && msgCount!=listener1.count) Thread.sleep(500); System.err.println("Finished ASYNC"); assertEquals("Checking success messages.",msgCount,listener1.count); } public void testDataSendACK() throws Exception { System.err.println("Starting ACK"); for (int i=0; i<msgCount; i++) channel1.send(new Member[] {channel2.getLocalMember(false)},Data.createRandomData(),GroupChannel.SEND_OPTIONS_USE_ACK); Thread.sleep(250); System.err.println("Finished ACK"); assertEquals("Checking success messages.",msgCount,listener1.count); } public void testDataSendSYNCACK() throws Exception { System.err.println("Starting SYNC_ACK"); for (int i=0; i<msgCount; i++) channel1.send(new Member[] {channel2.getLocalMember(false)},Data.createRandomData(),GroupChannel.SEND_OPTIONS_SYNCHRONIZED_ACK|GroupChannel.SEND_OPTIONS_USE_ACK); Thread.sleep(250); System.err.println("Finished SYNC_ACK"); assertEquals("Checking success messages.",msgCount,listener1.count); } public static class Listener implements ChannelListener { long count = 0; public boolean accept(Serializable s, Member m) { return (s instanceof Data); } public void messageReceived(Serializable s, Member m) { Data d = (Data)s; if ( !Data.verify(d) ) { System.err.println("ERROR"); } else { count++; if ((count %1000) ==0 ) { System.err.println("SUCCESS:"+count); } } } } public static class Data implements Serializable { public int length; public byte[] data; public byte key; public static Random r = new Random(System.currentTimeMillis()); public static Data createRandomData() { int i = r.nextInt(); i = ( i % 127 ); int length = Math.abs(r.nextInt() % 65555); Data d = new Data(); d.length = length; d.key = (byte)i; d.data = new byte[length]; Arrays.fill(d.data,d.key); return d; } public static boolean verify(Data d) { boolean result = (d.length == d.data.length); for ( int i=0; result && (i<d.data.length); i++ ) result = result && d.data[i] == d.key; return result; } } }

Other Tomcat examples (source code examples)

Here is a short list of links related to this Tomcat TestDataIntegrity.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.