This commit is contained in:
2024-11-30 19:03:49 +08:00
commit 1e6763c160
3806 changed files with 737676 additions and 0 deletions

View File

@@ -0,0 +1,130 @@
/*
* 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.group.interceptors;
import java.util.ArrayList;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.apache.catalina.tribes.Channel;
import org.apache.catalina.tribes.ManagedChannel;
import org.apache.catalina.tribes.Member;
import org.apache.catalina.tribes.MembershipListener;
import org.apache.catalina.tribes.group.GroupChannel;
import org.apache.catalina.tribes.util.UUIDGenerator;
public class TestDomainFilterInterceptor {
private static int count = 10;
private ManagedChannel[] channels = new ManagedChannel[count];
private TestMbrListener[] listeners = new TestMbrListener[count];
@Before
public void setUp() throws Exception {
for (int i = 0; i < channels.length; i++) {
channels[i] = new GroupChannel();
channels[i].getMembershipService().setPayload( ("Channel-" + (i + 1)).getBytes("ASCII"));
listeners[i] = new TestMbrListener( ("Listener-" + (i + 1)));
channels[i].addMembershipListener(listeners[i]);
DomainFilterInterceptor filter = new DomainFilterInterceptor();
filter.setDomain(UUIDGenerator.randomUUID(false));
channels[i].addInterceptor(filter);
}
}
public void clear() {
for (int i = 0; i < channels.length; i++) {
listeners[i].members.clear();
}
}
@Test
public void testMemberArrival() throws Exception {
//purpose of this test is to make sure that we have received all the members
//that we can expect before the start method returns
Thread[] threads = new Thread[channels.length];
for (int i=0; i<channels.length; i++ ) {
final Channel channel = channels[i];
Thread t = new Thread() {
@Override
public void run() {
try {
channel.start(Channel.DEFAULT);
}catch ( Exception x ) {
throw new RuntimeException(x);
}
}
};
threads[i] = t;
}
for (int i=0; i<threads.length; i++ ) threads[i].start();
for (int i=0; i<threads.length; i++ ) threads[i].join();
System.out.println("All channels started.");
for (int i=listeners.length-1; i>=0; i-- ) {
Assert.assertEquals("Checking member arrival length",0,listeners[i].members.size());
}
}
@After
public void tearDown() throws Exception {
for (int i = 0; i < channels.length; i++) {
try {
channels[i].stop(Channel.DEFAULT);
} catch (Exception ignore) {
// Ignore
}
}
}
public static class TestMbrListener
implements MembershipListener {
public String name = null;
public TestMbrListener(String name) {
this.name = name;
}
public ArrayList<Member> members = new ArrayList<>();
@Override
public void memberAdded(Member member) {
if (!members.contains(member)) {
members.add(member);
try {
System.out.println(name + ":member added[" + new String(member.getPayload(), "ASCII") + "; Thread:"+Thread.currentThread().getName()+"]");
} catch (Exception x) {
System.out.println(name + ":member added[unknown]");
}
}
}
@Override
public void memberDisappeared(Member member) {
if (members.contains(member)) {
members.remove(member);
try {
System.out.println(name + ":member disappeared[" + new String(member.getPayload(), "ASCII") + "; Thread:"+Thread.currentThread().getName()+"]");
} catch (Exception x) {
System.out.println(name + ":member disappeared[unknown]");
}
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,68 @@
/*
* 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.group.interceptors;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;
public class TestGzipInterceptor {
@Test
public void testSmallerThanBufferSize() throws Exception {
doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE / 2);
}
@Test
public void testJustSmallerThanBufferSize() throws Exception {
doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE -1);
}
@Test
public void testExactBufferSize() throws Exception {
doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE);
}
@Test
public void testJustLargerThanBufferSize() throws Exception {
doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE + 1);
}
@Test
public void testFactor2BufferSize() throws Exception {
doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE * 2);
}
@Test
public void testFactor4BufferSize() throws Exception {
doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE * 4);
}
@Test
public void testMuchLargerThanBufferSize() throws Exception {
doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE * 10 + 1000);
}
private void doCompressDecompress(int size) throws Exception {
byte[] data = new byte[size];
Arrays.fill(data, (byte)1);
byte[] compress = GzipInterceptor.compress(data);
byte[] result = GzipInterceptor.decompress(compress);
Assert.assertTrue(Arrays.equals(data, result));
}
}

View File

@@ -0,0 +1,130 @@
/*
* 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.group.interceptors;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.apache.catalina.tribes.Channel;
import org.apache.catalina.tribes.Member;
import org.apache.catalina.tribes.TesterUtil;
import org.apache.catalina.tribes.group.GroupChannel;
public class TestNonBlockingCoordinator {
private static final int CHANNEL_COUNT = 10;
private GroupChannel[] channels = null;
private NonBlockingCoordinator[] coordinators = null;
@Before
public void setUp() throws Exception {
System.out.println("Setup");
channels = new GroupChannel[CHANNEL_COUNT];
coordinators = new NonBlockingCoordinator[CHANNEL_COUNT];
Thread[] threads = new Thread[CHANNEL_COUNT];
for ( int i=0; i<CHANNEL_COUNT; i++ ) {
channels[i] = new GroupChannel();
coordinators[i] = new NonBlockingCoordinator();
channels[i].addInterceptor(coordinators[i]);
channels[i].addInterceptor(new TcpFailureDetector());
final int j = i;
threads[i] = new Thread() {
@Override
public void run() {
try {
channels[j].start(Channel.DEFAULT);
Thread.sleep(50);
} catch (Exception x) {
x.printStackTrace();
}
}
};
}
TesterUtil.addRandomDomain(channels);
for (int i = 0; i < CHANNEL_COUNT; i++) {
threads[i].start();
}
for (int i = 0; i < CHANNEL_COUNT; i++) {
threads[i].join();
}
Thread.sleep(1000);
}
@Test
public void testCoord1() throws Exception {
int expectedCount = channels[0].getMembers().length;
for (int i = 1; i < CHANNEL_COUNT; i++) {
Assert.assertEquals("Message count expected to be equal.", expectedCount,
channels[i].getMembers().length);
}
Member member = coordinators[0].getCoordinator();
int cnt = 0;
while (member == null && (cnt++ < 100)) {
try {
Thread.sleep(100);
member = coordinators[0].getCoordinator();
} catch (Exception x) {
/* Ignore */
}
}
for (int i = 0; i < CHANNEL_COUNT; i++) {
Assert.assertEquals(member, coordinators[i].getCoordinator());
}
System.out.println("Coordinator[1] is:" + member);
}
@Test
public void testCoord2() throws Exception {
Member member = coordinators[1].getCoordinator();
System.out.println("Coordinator[2a] is:" + member);
int index = -1;
for ( int i=0; i<CHANNEL_COUNT; i++ ) {
if ( channels[i].getLocalMember(false).equals(member) ) {
System.out.println("Shutting down:" + channels[i].getLocalMember(true).toString());
channels[i].stop(Channel.DEFAULT);
index = i;
}
}
int dead = index;
Thread.sleep(1000);
if (index == 0) {
index = 1;
} else {
index = 0;
}
System.out.println("Member count:"+channels[index].getMembers().length);
member = coordinators[index].getCoordinator();
for (int i = 1; i < CHANNEL_COUNT; i++) {
if (i != dead) {
Assert.assertEquals(member, coordinators[i].getCoordinator());
}
}
System.out.println("Coordinator[2b] is:" + member);
}
@After
public void tearDown() throws Exception {
System.out.println("tearDown");
for ( int i=0; i<CHANNEL_COUNT; i++ ) {
channels[i].stop(Channel.DEFAULT);
}
}
}

View File

@@ -0,0 +1,195 @@
/*
* 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.group.interceptors;
import java.io.Serializable;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.apache.catalina.tribes.Channel;
import org.apache.catalina.tribes.ChannelException;
import org.apache.catalina.tribes.ChannelListener;
import org.apache.catalina.tribes.ChannelMessage;
import org.apache.catalina.tribes.Member;
import org.apache.catalina.tribes.TesterUtil;
import org.apache.catalina.tribes.group.ChannelInterceptorBase;
import org.apache.catalina.tribes.group.GroupChannel;
import org.apache.catalina.tribes.group.InterceptorPayload;
public class TestOrderInterceptor {
GroupChannel[] channels = null;
OrderInterceptor[] orderitcs = null;
MangleOrderInterceptor[] mangleitcs = null;
TestListener[] test = null;
int channelCount = 2;
Thread[] threads = null;
@Before
public void setUp() throws Exception {
System.out.println("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() {
@Override
public void run() {
try {
channels[j].start(Channel.DEFAULT);
Thread.sleep(50);
} catch (Exception x) {
x.printStackTrace();
}
}
};
}
TesterUtil.addRandomDomain(channels);
for ( int i=0; i<channelCount; i++ ) threads[i].start();
for ( int i=0; i<channelCount; i++ ) threads[i].join();
Thread.sleep(1500);
}
@Test
public void testOrder1() throws Exception {
Member[] dest = channels[0].getMembers();
final AtomicInteger value = new AtomicInteger(0);
for ( int i=0; i<100; i++ ) {
channels[0].send(dest,Integer.valueOf(value.getAndAdd(1)),0);
}
Thread.sleep(5000);
for ( int i=0; i<test.length; i++ ) {
Assert.assertFalse(test[i].fail);
}
}
@Test
public void testOrder2() throws Exception {
final Member[] dest = channels[0].getMembers();
final AtomicInteger value = new AtomicInteger(0);
final Queue<Exception> exceptionQueue = new ConcurrentLinkedQueue<>();
Runnable run = new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
synchronized (channels[0]) {
channels[0].send(dest, Integer.valueOf(value.getAndAdd(1)), 0);
}
}catch ( Exception x ) {
exceptionQueue.add(x);
}
}
}
};
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();
}
if (!exceptionQueue.isEmpty()) {
Assert.fail("Exception while sending in threads: "
+ exceptionQueue.remove().toString());
}
Thread.sleep(5000);
for ( int i=0; i<test.length; i++ ) {
Assert.assertFalse(test[i].fail);
}
}
@After
public void tearDown() throws Exception {
System.out.println("tearDown");
for ( int i=0; i<channelCount; i++ ) {
channels[i].stop(Channel.DEFAULT);
}
}
public static void main(String[] args) {
org.junit.runner.JUnitCore.main(TestOrderInterceptor.class.getName());
}
public static class TestListener implements ChannelListener {
int id = -1;
public TestListener(int id) {
this.id = id;
}
int cnt = 0;
int total = 0;
volatile boolean fail = false;
@Override
public synchronized void messageReceived(Serializable msg, Member sender) {
total++;
Integer i = (Integer)msg;
if ( i.intValue() != cnt ) fail = true;
else cnt++;
System.out.println("Listener["+id+"] Message received:"+i+" Count:"+total+" Fail:"+fail);
}
@Override
public boolean accept(Serializable msg, Member sender) {
return (msg instanceof Integer);
}
}
public static class MangleOrderInterceptor extends ChannelInterceptorBase {
ChannelMessage hold = null;
Member[] dest = null;
@Override
public synchronized void sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload) throws ChannelException {
if ( hold == null ) {
//System.out.println("Skipping message:"+msg);
hold = (ChannelMessage)msg.deepclone();
dest = new Member[destination.length];
System.arraycopy(destination,0,dest,0,dest.length);
} else {
//System.out.println("Sending message:"+msg);
super.sendMessage(destination,msg,payload);
//System.out.println("Sending message:"+hold);
super.sendMessage(dest,hold,null);
hold = null;
dest = null;
}
}
}
}

View File

@@ -0,0 +1,173 @@
/*
* 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.group.interceptors;
import java.util.ArrayList;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.apache.catalina.tribes.ByteMessage;
import org.apache.catalina.tribes.Channel;
import org.apache.catalina.tribes.ChannelException;
import org.apache.catalina.tribes.ManagedChannel;
import org.apache.catalina.tribes.Member;
import org.apache.catalina.tribes.MembershipListener;
import org.apache.catalina.tribes.TesterUtil;
import org.apache.catalina.tribes.group.GroupChannel;
public class TestTcpFailureDetector {
private TcpFailureDetector tcpFailureDetector1 = null;
private TcpFailureDetector tcpFailureDetector2 = null;
private ManagedChannel channel1 = null;
private ManagedChannel channel2 = null;
private TestMbrListener mbrlist1 = null;
private TestMbrListener mbrlist2 = null;
@Before
public void setUp() throws Exception {
channel1 = new GroupChannel();
channel2 = new GroupChannel();
channel1.getMembershipService().setPayload("Channel-1".getBytes("ASCII"));
channel2.getMembershipService().setPayload("Channel-2".getBytes("ASCII"));
mbrlist1 = new TestMbrListener("Channel-1");
mbrlist2 = new TestMbrListener("Channel-2");
tcpFailureDetector1 = new TcpFailureDetector();
tcpFailureDetector2 = new TcpFailureDetector();
channel1.addInterceptor(tcpFailureDetector1);
channel2.addInterceptor(tcpFailureDetector2);
channel1.addMembershipListener(mbrlist1);
channel2.addMembershipListener(mbrlist2);
TesterUtil.addRandomDomain(new ManagedChannel[] {channel1, channel2});
}
public void clear() {
mbrlist1.members.clear();
mbrlist2.members.clear();
}
@Test
public void testTcpSendFailureMemberDrop() throws Exception {
System.out.println("testTcpSendFailureMemberDrop()");
clear();
channel1.start(Channel.DEFAULT);
channel2.start(Channel.DEFAULT);
//Thread.sleep(1000);
Assert.assertEquals("Expecting member count to be equal",mbrlist1.members.size(),mbrlist2.members.size());
channel2.stop(Channel.SND_RX_SEQ);
ByteMessage msg = new ByteMessage(new byte[1024]);
try {
channel1.send(channel1.getMembers(), msg, 0);
Assert.fail("Message send should have failed.");
} catch ( ChannelException x ) {
// Ignore
}
Assert.assertEquals("Expecting member count to not be equal",mbrlist1.members.size()+1,mbrlist2.members.size());
channel1.stop(Channel.DEFAULT);
channel2.stop(Channel.DEFAULT);
}
@Test
public void testTcpFailureMemberAdd() throws Exception {
System.out.println("testTcpFailureMemberAdd()");
clear();
channel1.start(Channel.DEFAULT);
channel2.start(Channel.SND_RX_SEQ);
channel2.start(Channel.SND_TX_SEQ);
channel2.start(Channel.MBR_RX_SEQ);
channel2.stop(Channel.SND_RX_SEQ);
channel2.start(Channel.MBR_TX_SEQ);
//Thread.sleep(1000);
Assert.assertEquals("Expecting member count to not be equal",mbrlist1.members.size()+1,mbrlist2.members.size());
channel1.stop(Channel.DEFAULT);
channel2.stop(Channel.DEFAULT);
}
@Test
public void testTcpMcastFail() throws Exception {
System.out.println("testTcpMcastFail()");
clear();
channel1.start(Channel.DEFAULT);
channel2.start(Channel.DEFAULT);
//Thread.sleep(1000);
Assert.assertEquals("Expecting member count to be equal",mbrlist1.members.size(),mbrlist2.members.size());
channel2.stop(Channel.MBR_TX_SEQ);
ByteMessage msg = new ByteMessage(new byte[1024]);
try {
Thread.sleep(5000);
Assert.assertEquals("Expecting member count to be equal",mbrlist1.members.size(),mbrlist2.members.size());
channel1.send(channel1.getMembers(), msg, 0);
} catch ( ChannelException x ) {
Assert.fail("Message send should have succeeded.");
}
channel1.stop(Channel.DEFAULT);
channel2.stop(Channel.DEFAULT);
}
@After
public void tearDown() throws Exception {
tcpFailureDetector1 = null;
tcpFailureDetector2 = null;
try {
channel1.stop(Channel.DEFAULT);
} catch (Exception ignore) {
// Ignore
}
channel1 = null;
try {
channel2.stop(Channel.DEFAULT);
} catch (Exception ignore) {
// Ignore
}
channel2 = null;
}
public static class TestMbrListener implements MembershipListener {
public String name = null;
public TestMbrListener(String name) {
this.name = name;
}
public ArrayList<Member> members = new ArrayList<>();
@Override
public void memberAdded(Member member) {
if ( !members.contains(member) ) {
members.add(member);
try{
System.out.println(name + ":member added[" + new String(member.getPayload(), "ASCII") + "]");
}catch ( Exception x ) {
System.out.println(name + ":member added[unknown]");
}
}
}
@Override
public void memberDisappeared(Member member) {
if ( members.contains(member) ) {
members.remove(member);
try{
System.out.println(name + ":member disappeared[" + new String(member.getPayload(), "ASCII") + "]");
}catch ( Exception x ) {
System.out.println(name + ":member disappeared[unknown]");
}
}
}
}
}