Jump to content
  • Advertisement
Sign in to follow this  
altich88

Is my event manager class sufficiently decoupled and unit testable?

This topic is 1932 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

I'm trying to learn some architecture and design patterns in my latest project including the use of dependency injection and hopefully unit-testable code.

 

I've written a simple event manager and would like some feedback on its implementation. All functionality currently resides in a single class which features the relevant data structures to store events and callbacks as well as the public methods used to offer clients the required functionality.

 

public class EventManager {
	private static final String LOG_TAG = EventManager.class.getSimpleName();
	
	private final int NUMBER_EVENT_QUEUES = 2;

	private int activeQueueIndex = 0;

	private Array<SortedIntList<Event>> eventQueues;

	private ObjectMap<Class<? extends Event>, SortedIntList<EventCallback>> eventCallbackMap;

	// Default constructor
	@Inject
	public EventManager() {
		eventQueues = new Array<SortedIntList<Event>>(NUMBER_EVENT_QUEUES);

		// Create the empty event queues
		for (int i = 0; i < NUMBER_EVENT_QUEUES; i++) {
			eventQueues.add(new SortedIntList<Event>());
		}

		eventCallbackMap = new ObjectMap<Class<? extends Event>, SortedIntList<EventCallback>>();
	}

	public void onUpdate() {
		// Iterates all events in the current queue and calls the registered callbacks for each...
	}

	public boolean queueEvent(Event event) {
		// Queues an event if there is at least 1 callback registered tor receive it (returns true)...
	}

	public boolean triggerEvent(Event event) {
		// Calls all registered callbacks for the given event (returns false is there are none)...
	}

	public boolean addCallback(Class<? extends Event> eventType, EventCallback callback) {
		// Adds a callback to the map, returns true if successful, false if already registered...
	}
}

 

My concern is that my unit tests would not be able to probe deep enough when testing correct behaviour. In testing the addCallback function for example I would be limited to checking the boolean returned and would not be able to directly check whether the callback had in fact been added by looking at the last added item to the corresponding list.

 

Similarly for testing triggerEvent, I would first have to add a callback for a type of event (thus relying on a method outside of the current test), before calling triggerEvent and then checking to see if the callback executed (maybe by having the callback alter some variable I could watch or something equally horrible).

 

This doesn't feel right but I'm not sure how best to split this up so my testing can be more thorough.

 

Do the data structures of events/callbacks count as dependencies, or not as they are initially all empty?

 

Any comments would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement

If it's white box testing that you're looking for, you could use a mocking framework (like Easymock or Mockito) and test only the EventManager's dispaching logic. It's relatively simple to ensure that the registered callbacks are indeed being called.

 

P.S. I'm assuming this is in Java .

Share this post


Link to post
Share on other sites

Thanks for the advice, I've managed to split it up some more now (EventManager, EventCallbackStore and EventQueue). I haven't achieved full white box testing as my custom data structure tests rely on using add/retrieve methods in combination but I think that's as close as I can get without adding methods to expose the underlying data structures.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!