How to write tests for tracking event flows in eventbus?

302 views Asked by At

Long description: In our gwt with mvp4g app we have pretty complicated flow of events in eventbus. One event like LOGIN produces multiple others as a reaction from presenters/handlers. Currently we have great difficulties with understanding how events interrelated i.e. which events must follow this particular one.
We have tests for presenters and views, but we are lacking tests which would clearly show/model event flows, preferably without usage of real views and services.

Short description: New tests on eventBus(?) should be developed which should clearly describe and test event flows.

I have few rud ideas but they all sounds not satisfactory:

  1. Write custom implementation(could be ugly) of mvp4g eventbus and:

    • use real presenters
    • use mock(?) views
    • mock services
    • verify all produced service calls
    • Why not cool: (a) In this case test would not verify produced events directly but only that ones which have services. (b) EventBus implementation would look rather scarry - it must create each presenter with mocked services and views
  2. Find a way to use some magical mvp4g mechanism to create eventBus in test and mock vies, services.

    • Why not cool : same as prev - only indirect verification through services is possible, and I cannot find how to create eventBus manually and solve all problems with GIN, inter GWT module dependencies and so. I guess there is no simple way to do it.

Is there any general solution for problem of tracking event tree in tests? Guess I'm not the first person to stare at complicated eventbus event flows.

1

There are 1 answers

1
ORDIX team On

Do you want to test the eventBus? Or do you want to track all event which are fired? If you want to track your events, maybe some kind of EventMonitor could help you? A class that implements all necessary EventHandler and log every event that occurs. Something like that? Just instance that class before your tests starts.

import java.util.logging.Logger;

import com.google.gwt.event.shared.GwtEvent;
import com.google.web.bindery.event.shared.EventBus;

public class EventMonitor implements AEventHandler, BEventHandler /* , ... */{

    private static int event_count = 1;

    private final Logger logger = Logger.getLogger(this.getClass().getName());

    public EventMonitor(EventBus eventBus) {
        eventBus.addHandler(AEvent.getType(), this);
        eventBus.addHandler(BEvent.getType(), this);
        // [...]
    }

    private void logEvent(GwtEvent<?> event) {

        logger.info(event_count + " useful information");
        event_count++;
    }

    @Override
    public void onAEvent(AEvent event) {
        logEvent(event);
    }

    @Override
    public void onBEvent(BEvent event) {
        logEvent(event);
    }


}