This commit is contained in:
benjamin 2013-07-04 14:26:11 +02:00
parent 46cedcee46
commit 5915038aca
5 changed files with 84 additions and 6 deletions

View File

@ -6,7 +6,7 @@ import net.engio.mbassy.dispatch.ReflectiveHandlerInvocation;
import java.lang.annotation.*;
/**
* Mark any method of any object(=listener) as a message handler and configure the handler
* Mark any method of any class(=listener) as a message handler and configure the handler
* using different properties.
*
* @author bennidi

View File

@ -70,8 +70,8 @@ public class Subscription {
public static final Comparator<Subscription> SubscriptionByPriorityDesc = new Comparator<Subscription>() {
@Override
public int compare(Subscription o1, Subscription o2) {
int result = o1.getPriority() - o2.getPriority();
return result == 0 ? o1.id.compareTo(o2.id) : result;
int byPriority = ((Integer)o1.getPriority()).compareTo(o2.getPriority());
return byPriority == 0 ? o1.id.compareTo(o2.id) : byPriority;
}
};

View File

@ -12,6 +12,11 @@ import org.junit.Test;
import java.util.Collection;
/**
*
* Test the subscriptions as generated and organized by the subscription manager. Tests use different sets of listeners
* and corresponding expected set of subscriptions that should result from subscribing the listeners. The subscriptions
* are tested for the type of messages they should handle and
*
* @author bennidi
* Date: 5/12/13
*/
@ -166,7 +171,6 @@ public class SubscriptionManagerTest extends AssertSupport {
SubscriptionManager subscriptionManager = new SubscriptionManager(new MetadataReader(), new SubscriptionFactory());
ConcurrentExecutor.runConcurrent(TestUtil.subscriber(subscriptionManager, listeners), ConcurrentUnits);
listeners.clear();
runGC();
@ -177,6 +181,7 @@ public class SubscriptionManagerTest extends AssertSupport {
}
private ListenerFactory listeners(Class ...listeners){
ListenerFactory factory = new ListenerFactory();
for(Class listener : listeners){
@ -203,4 +208,5 @@ public class SubscriptionManagerTest extends AssertSupport {
}

View File

@ -5,10 +5,15 @@ import net.engio.mbassy.common.ConcurrentExecutor;
import net.engio.mbassy.common.ListenerFactory;
import net.engio.mbassy.common.MessageBusTest;
import net.engio.mbassy.common.TestUtil;
import net.engio.mbassy.listeners.*;
import net.engio.mbassy.listener.Handler;
import net.engio.mbassy.listeners.CustomInvocationListener;
import net.engio.mbassy.listeners.ExceptionThrowingListener;
import net.engio.mbassy.listeners.IMessageListener;
import net.engio.mbassy.listeners.MessagesListener;
import net.engio.mbassy.messages.MessageTypes;
import net.engio.mbassy.messages.MultipartMessage;
import net.engio.mbassy.messages.StandardMessage;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger;
@ -67,6 +72,7 @@ public abstract class SyncBusTest extends MessageBusTest {
}
@Test
public void testExceptionInHandlerInvocation(){
final AtomicInteger exceptionCount = new AtomicInteger(0);
@ -135,6 +141,32 @@ public abstract class SyncBusTest extends MessageBusTest {
}
@Test
public void testHandlerPriorities(){
final ISyncMessageBus bus = getSyncMessageBus();
ListenerFactory listeners = new ListenerFactory()
.create(InstancesPerListener, PrioritizedListener.class)
.create(InstancesPerListener, Object.class);
ConcurrentExecutor.runConcurrent(TestUtil.subscriber(bus, listeners), ConcurrentUnits);
Runnable publishAndCheck = new Runnable() {
@Override
public void run() {
bus.post(new IncrementingMessage()).now();
}
};
// single threaded
ConcurrentExecutor.runConcurrent(publishAndCheck, 1);
// multi threaded
ConcurrentExecutor.runConcurrent(publishAndCheck, ConcurrentUnits);
}
public static class MBassadorTest extends SyncBusTest {
@ -155,4 +187,45 @@ public abstract class SyncBusTest extends MessageBusTest {
}
}
static class IncrementingMessage{
private int count = 1;
public void markHandled(int newVal){
// only transitions by the next handler are allowed
if(count == newVal || count + 1 == newVal) count = newVal;
else Assert.fail("Message was handled out of order");
}
}
public static class PrioritizedListener{
@Handler(priority = Integer.MIN_VALUE)
public void handle1(IncrementingMessage message) {
message.markHandled(1);
}
@Handler(priority = -2)
public void handle2(IncrementingMessage message) {
message.markHandled(2);
}
@Handler
public void handle3(IncrementingMessage message) {
message.markHandled(3);
}
@Handler(priority = Integer.MAX_VALUE)
public void handle4(IncrementingMessage message) {
message.markHandled(4);
}
}
}

View File

@ -6,7 +6,6 @@ import net.engio.mbassy.subscription.SubscriptionManager;
import java.util.*;
/**
* Todo: Add javadoc
*
* @author bennidi
* Date: 5/25/13