/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package nars.core;

import nars.Events;
import nars.event.EventEmitter;
import nars.event.Reaction;
import org.junit.Test;
import reactor.event.Event;

import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.Assert.assertTrue;
import static reactor.event.selector.Selectors.T;

/**
 * @author me
 */
public class EventTest {


    @Test
    public void testReactor() throws InterruptedException {

        EventEmitter e = EventEmitter.newSynchronous();

        AtomicBoolean b = new AtomicBoolean();

        e.on(T(Events.CycleEnd.class), x -> {
            //System.err.println("EVENT: " + x);
            b.set(true);
        });

        e.notify(Events.CycleEnd.class, Event.wrap(true));


        Thread.sleep(100);

        e.shutdown();

        assertTrue(b.get());
    }

    @Test
    public void testReactorException() throws InterruptedException {

        AtomicBoolean b = new AtomicBoolean();

        EventEmitter e = new EventEmitter();

        e.on(Events.CycleEnd.class, new Reaction() {
            @Override
            public void event(Class event, Object[] args) {
                throw new RuntimeException("Exception generated for testing purposes; everything is OK");
            }
        });
        e.on(Events.ERR.class, new Reaction() {
            @Override
            public void event(Class event, Object[] args) {
                b.set(true);
            }
        });
        e.notify(Events.CycleEnd.class);

        Thread.sleep(100);


        assertTrue(b.get());
    }

}

@@ -692,32 +692,33 @@
 omicBoolean();%0A%0A
+%0A
         e.on(T(E
@@ -743,16 +743,26 @@
 ss),
- x -%3E
+new Reaction()
  %7B%0A
+%0A
     
@@ -773,45 +773,138 @@
     
-//System.err.println(%22EVENT: %22 + x);%0A
+@Override%0A            public void event(Class event, Object%5B%5D args) %7B%0A                if (event == Events.CycleEnd.class)%0A        
     
@@ -916,32 +916,46 @@
    b.set(true);%0A
+            %7D%0A
         %7D);%0A%0A   

test update