001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.catalina.tribes.test.channel;
018:
019: import junit.framework.TestCase;
020: import java.io.Serializable;
021: import java.util.Random;
022: import java.util.Arrays;
023: import org.apache.catalina.tribes.ChannelListener;
024: import org.apache.catalina.tribes.Member;
025: import org.apache.catalina.tribes.group.GroupChannel;
026: import java.io.PrintStream;
027:
028: /**
029: * <p>Title: </p>
030: *
031: * <p>Description: </p>
032: *
033: * <p>Company: </p>
034: *
035: * @author not attributable
036: * @version 1.0
037: */
038: public class TestRemoteProcessException extends TestCase {
039: int msgCount = 10000;
040: GroupChannel channel1;
041: GroupChannel channel2;
042: Listener listener1;
043:
044: protected void setUp() throws Exception {
045: super .setUp();
046: channel1 = new GroupChannel();
047: channel2 = new GroupChannel();
048: listener1 = new Listener();
049: channel2.addChannelListener(listener1);
050: channel1.start(GroupChannel.DEFAULT);
051: channel2.start(GroupChannel.DEFAULT);
052: }
053:
054: protected void tearDown() throws Exception {
055: super .tearDown();
056: channel1.stop(GroupChannel.DEFAULT);
057: channel2.stop(GroupChannel.DEFAULT);
058: }
059:
060: public void testDataSendSYNCACK() throws Exception {
061: System.err.println("Starting SYNC_ACK");
062: int errC = 0, nerrC = 0;
063: for (int i = 0; i < msgCount; i++) {
064: boolean error = Data.r.nextBoolean();
065: channel1.send(channel1.getMembers(), Data
066: .createRandomData(error),
067: GroupChannel.SEND_OPTIONS_SYNCHRONIZED_ACK
068: | GroupChannel.SEND_OPTIONS_USE_ACK);
069: if (error)
070: errC++;
071: else
072: nerrC++;
073: }
074: System.err.println("Finished SYNC_ACK");
075: assertEquals("Checking failure messages.", errC,
076: listener1.errCnt);
077: assertEquals("Checking success messages.", nerrC,
078: listener1.noErrCnt);
079: assertEquals("Checking all messages.", msgCount,
080: listener1.noErrCnt + listener1.errCnt);
081: System.out.println("Listener 1 stats:");
082: listener1.printStats(System.out);
083: }
084:
085: public static class Listener implements ChannelListener {
086: long noErrCnt = 0;
087: long errCnt = 0;
088:
089: public boolean accept(Serializable s, Member m) {
090: return (s instanceof Data);
091: }
092:
093: public void messageReceived(Serializable s, Member m) {
094: Data d = (Data) s;
095: if (!Data.verify(d)) {
096: System.err.println("ERROR");
097: } else {
098: if (d.error) {
099: errCnt++;
100: if ((errCnt % 100) == 0) {
101: printStats(System.err);
102: }
103: throw new IllegalArgumentException();
104: } else {
105: noErrCnt++;
106: if ((noErrCnt % 100) == 0) {
107: printStats(System.err);
108: }
109: }
110: }
111: }
112:
113: public void printStats(PrintStream stream) {
114: stream.println("NORMAL:" + noErrCnt);
115: stream.println("FAILURES:" + errCnt);
116: stream.println("TOTAL:" + (errCnt + noErrCnt));
117: }
118: }
119:
120: public static class Data implements Serializable {
121: public int length;
122: public byte[] data;
123: public byte key;
124: public boolean error = false;
125: public static Random r = new Random(System.currentTimeMillis());
126:
127: public static Data createRandomData(boolean error) {
128: int i = r.nextInt();
129: i = (i % 127);
130: int length = Math.abs(r.nextInt() % 65555);
131: Data d = new Data();
132: d.length = length;
133: d.key = (byte) i;
134: d.data = new byte[length];
135: Arrays.fill(d.data, d.key);
136: d.error = error;
137: return d;
138: }
139:
140: public static boolean verify(Data d) {
141: boolean result = (d.length == d.data.length);
142: for (int i = 0; result && (i < d.data.length); i++)
143: result = result && d.data[i] == d.key;
144: return result;
145: }
146: }
147:
148: }
|