package net.ranides.assira.events;

import net.ranides.assira.reflection.IClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class TaskBrokerTest {

    @Test
    public void broker_reactor() {
        testBroker(new TaskRouter(EventReactor.newInstance("broker1")));
    }

    @Test
    public void broker_proactor() {
        testBroker(new TaskRouter(EventProactor.newInstance("broker2")));
    }

    @Test
    public void broker_dispatcher() {
        testBroker(new TaskRouter(EventDispatcher.newInstance()));
    }

    protected void testBroker(TaskRouter router) {
        TaskBroker<ICalc> broker = new TaskBroker<>(IClass.typeinfo(ICalc.class), new CCalc(), router);

        ICalc client = broker.client();

        for(int i=0; i<10; i++) {
            assertEquals(11, client.add(4, 7));
            assertEquals(2, client.mod(11, 3));
            assertEquals(8, client.mul(2, 4));
        }
    }

    private interface ICalc {

        int add(int a, int b);

        int mod(int a, int b);

        int mul(int a, int b);

    }

    private static class CCalc implements ICalc {

        @Override
        public int add(int a, int b) {
            return a + b;
        }

        @Override
        public int mod(int a, int b) {
            return a % b;
        }

        @Override
        public int mul(int a, int b) {
            return a * b;
        }

    }

}