|
@@ -1,9 +1,16 @@
|
|
|
package org.hswebframework.web.eventbus.spring;
|
|
|
|
|
|
+import org.hswebframework.web.eventbus.EventBus;
|
|
|
import org.hswebframework.web.eventbus.annotation.EventMode;
|
|
|
import org.hswebframework.web.eventbus.annotation.Subscribe;
|
|
|
import org.junit.Assert;
|
|
|
import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
@@ -11,42 +18,22 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|
|
* @author zhouhao
|
|
|
* @since 3.0
|
|
|
*/
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+@SpringBootTest(classes = TestApplication.class,webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
|
|
public class SpringEventBusTest {
|
|
|
- SpringEventBus eventBus = new SpringEventBus();
|
|
|
|
|
|
- AtomicInteger counter = new AtomicInteger();
|
|
|
+ @Autowired
|
|
|
+ EventBus eventBus;
|
|
|
|
|
|
@Test
|
|
|
public void test() throws InterruptedException {
|
|
|
System.out.println(Thread.currentThread().getName());
|
|
|
- eventBus.postProcessAfterInitialization(new Test2(), "test");
|
|
|
long t = System.currentTimeMillis();
|
|
|
eventBus.publish(eventBus);
|
|
|
System.out.println(System.currentTimeMillis() - t);
|
|
|
Thread.sleep(1000);
|
|
|
- Assert.assertEquals(counter.get(), 3);
|
|
|
+ Assert.assertEquals(Test2.counter.get(), 3);
|
|
|
}
|
|
|
|
|
|
- public class Test2 {
|
|
|
- @Subscribe(mode = EventMode.SYNC)
|
|
|
- public void test1(SpringEventBus eventBus) {
|
|
|
-// System.out.println(Thread.currentThread().getName());
|
|
|
-// System.out.println(eventBus);
|
|
|
- counter.addAndGet(1);
|
|
|
- }
|
|
|
|
|
|
- @Subscribe(mode = EventMode.ASYNC, transaction = false)
|
|
|
- public void test2(SpringEventBus eventBus) {
|
|
|
-// System.out.println(Thread.currentThread().getName());
|
|
|
-// System.out.println(eventBus);
|
|
|
- counter.addAndGet(1);
|
|
|
- }
|
|
|
-
|
|
|
- @Subscribe(mode = EventMode.BACKGROUND, transaction = false)
|
|
|
- public void test3(SpringEventBus eventBus) {
|
|
|
-// System.out.println(Thread.currentThread().getName());
|
|
|
-// System.out.println(eventBus);
|
|
|
- counter.addAndGet(1);
|
|
|
- }
|
|
|
- }
|
|
|
}
|