|
|
@@ -27,77 +27,60 @@ public class EventLockTest {
|
|
|
throw ExceptionUtils.rethrow(ex);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * MUSIMY tu mieć duże opóźnienia, jeśli jednocześnie chcemy sprawdzić
|
|
|
- * naprawdę EventLock oraz uniknąć race-condition. Moglibyśmy wprowadzić
|
|
|
- * jakąś synchronizację... ale wtedy z definicji byśmy testowali tę
|
|
|
- * smart-synchronizację, a nie nasz EventLock.
|
|
|
- */
|
|
|
- @Category(JCategories.UnstableTest.class)
|
|
|
+
|
|
|
@Test
|
|
|
public void testSingleLock() throws InterruptedException {
|
|
|
final EventReactor main = EventReactor.newInstance("name", 32, 1000);
|
|
|
|
|
|
+ EventLock<WriteEvent> we1 = EventLock.singleLock(WriteEvent.class, main);
|
|
|
+ EventLock<WriteEvent> we2 = EventLock.singleLock(WriteEvent.class, main);
|
|
|
+
|
|
|
+ EventLock<ReadEvent> re1 = EventLock.singleLock(ReadEvent.class, main);
|
|
|
+ EventLock<ReadEvent> re2 = EventLock.singleLock(ReadEvent.class, main);
|
|
|
+ EventLock<ReadEvent> re3 = EventLock.singleLock(ReadEvent.class, main, 1);
|
|
|
+
|
|
|
+ EventLock<IOEvent> io1 = EventLock.singleLock(IOEvent.class, main);
|
|
|
+ EventLock<IOEvent> io2 = EventLock.singleLock(IOEvent.class, main);
|
|
|
+
|
|
|
new Thread(() -> {
|
|
|
- // sleep unikają race condition (event wysłany przed rozpoczęciem waitForEvent)
|
|
|
- sleep(100);
|
|
|
main.signalEvent(new WriteEvent(11));
|
|
|
- sleep(100);
|
|
|
main.signalEvent(new ReadEvent(12));
|
|
|
- sleep(100);
|
|
|
main.signalEvent(new ReadEvent(13));
|
|
|
- sleep(100);
|
|
|
main.signalEvent(new ReadEvent(14));
|
|
|
- sleep(100);
|
|
|
main.signalEvent(new Event(){});
|
|
|
- sleep(100);
|
|
|
main.signalEvent(new ReadEvent(16));
|
|
|
- sleep(100);
|
|
|
main.signalEvent(new WriteEvent(17));
|
|
|
- sleep(100);
|
|
|
-
|
|
|
main.signalEvent(new WriteEvent(19));
|
|
|
- sleep(400);
|
|
|
main.signalEvent(new WriteEvent(20));
|
|
|
- sleep(100);
|
|
|
-
|
|
|
main.signalEvent(new ReadEvent(21));
|
|
|
- sleep(400);
|
|
|
main.signalEvent(new ReadEvent(22));
|
|
|
- sleep(100);
|
|
|
-
|
|
|
- main.stop();
|
|
|
}).start();
|
|
|
-
|
|
|
-
|
|
|
- IOEvent event;
|
|
|
-
|
|
|
- // taka konstrukcja normalnie jest podatna na race condition
|
|
|
- event = EventLock.singleLock(WriteEvent.class, main).waitForEvent();
|
|
|
- assertEquals(11, event.handle());
|
|
|
-
|
|
|
- event = EventLock.singleLock(ReadEvent.class, main).waitForEvent(ReadEvent.class);
|
|
|
- assertEquals(12, event.handle());
|
|
|
-
|
|
|
- event = EventLock.singleLock(WriteEvent.class, main).waitForEvent(WriteEvent.class);
|
|
|
- assertEquals(17, event.handle());
|
|
|
-
|
|
|
- event = EventLock.singleLock(IOEvent.class, main).waitForEvent(300).get();
|
|
|
- assertEquals(19, event.handle());
|
|
|
-
|
|
|
- event = EventLock.singleLock(IOEvent.class, main).waitForEvent(200).orElse(null);
|
|
|
- assertNull(event);
|
|
|
-
|
|
|
- event = EventLock.singleLock(IOEvent.class, main).waitForEvent(ReadEvent.class, 600).get();
|
|
|
- assertEquals(21, event.handle());
|
|
|
-
|
|
|
- event = EventLock.singleLock(IOEvent.class, main).waitForEvent(ReadEvent.class, 200).orElse(null);
|
|
|
- assertNull(event);
|
|
|
+
|
|
|
+ sleep(100);
|
|
|
+
|
|
|
+ assertEquals(7, main.getEventListenersCount());
|
|
|
+
|
|
|
+ assertEquals(11, we1.waitForEvent().handle());
|
|
|
+ assertEquals(17, we2.waitForEvent(e -> e.handle()>11).handle());
|
|
|
+
|
|
|
+ assertEquals(5, main.getEventListenersCount());
|
|
|
+
|
|
|
+ assertEquals(12, re1.waitForEvent().handle());
|
|
|
+ assertEquals(21, re2.waitForEvent(e -> e.handle()>20).handle());
|
|
|
+
|
|
|
+ assertEquals(22, re3.waitForEvent().handle());
|
|
|
+ assertEquals(5, re3.discarded());
|
|
|
+
|
|
|
+ assertEquals(2, main.getEventListenersCount());
|
|
|
+
|
|
|
+ assertEquals(16, io1.waitForEvent(e -> e.handle() == 16, 100).get().handle());
|
|
|
+ assertFalse(io2.waitForEvent(e -> e.handle() == 116, 100).isPresent());
|
|
|
|
|
|
assertEquals(0, main.getEventListenersCount());
|
|
|
+
|
|
|
+ main.stop();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* MUSIMY tu mieć duże opóźnienia, jeśli jednocześnie chcemy sprawdzić
|
|
|
* naprawdę EventLock oraz uniknąć race-condition. Moglibyśmy wprowadzić
|
|
|
@@ -243,7 +226,6 @@ public class EventLockTest {
|
|
|
assertEquals(0, main.getEventListenersCount());
|
|
|
}
|
|
|
|
|
|
- @Category(JCategories.UnstableTest.class)
|
|
|
@Test
|
|
|
public void testEventLockDiscard() throws InterruptedException {
|
|
|
final EventReactor main = EventReactor.newInstance("name", 32, 1000);
|
|
|
@@ -267,10 +249,10 @@ public class EventLockTest {
|
|
|
main.signalEvent(new ReadEvent(108));
|
|
|
main.signalEvent(new ReadEvent(109));
|
|
|
main.signalEvent(new ReadEvent(110));
|
|
|
- main.stop();
|
|
|
+ main.stop();
|
|
|
}).start();
|
|
|
|
|
|
- sleep(750);
|
|
|
+ sleep(500);
|
|
|
|
|
|
assertEquals(201, lock.waitForEvent(IOEvent.class).handle());
|
|
|
assertEquals(202, lock.waitForEvent(IOEvent.class).handle());
|