EventSourcing: fix Memory EventStore backward source & stabilize test

Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/9f02f2ca
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/9f02f2ca
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/9f02f2ca

Branch: refs/heads/master
Commit: 9f02f2ca05ff778df7200880dc941dd645576aa3
Parents: 06d8014
Author: Paul Merlin <[email protected]>
Authored: Mon Jul 20 17:32:15 2015 +0200
Committer: Paul Merlin <[email protected]>
Committed: Mon Jul 20 17:32:15 2015 +0200

----------------------------------------------------------------------
 .../MemoryApplicationEventStoreService.java     | 22 +++++++----------
 .../application/ApplicationEventTest.java       | 25 ++++++++++++++------
 2 files changed, 26 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/9f02f2ca/libraries/eventsourcing/src/main/java/org/qi4j/library/eventsourcing/application/source/memory/MemoryApplicationEventStoreService.java
----------------------------------------------------------------------
diff --git 
a/libraries/eventsourcing/src/main/java/org/qi4j/library/eventsourcing/application/source/memory/MemoryApplicationEventStoreService.java
 
b/libraries/eventsourcing/src/main/java/org/qi4j/library/eventsourcing/application/source/memory/MemoryApplicationEventStoreService.java
index cce6cf4..6fa2bce 100644
--- 
a/libraries/eventsourcing/src/main/java/org/qi4j/library/eventsourcing/application/source/memory/MemoryApplicationEventStoreService.java
+++ 
b/libraries/eventsourcing/src/main/java/org/qi4j/library/eventsourcing/application/source/memory/MemoryApplicationEventStoreService.java
@@ -109,25 +109,19 @@ public interface MemoryApplicationEventStoreService
                             public <ReceiverThrowableType extends Throwable> 
void sendTo( Receiver<? super TransactionApplicationEvents, 
ReceiverThrowableType> receiver )
                                 throws ReceiverThrowableType, IOException
                             {
-                                ListIterator<TransactionApplicationEvents> 
iterator = store.listIterator();
+                                Iterator<TransactionApplicationEvents> 
iterator = store.descendingIterator();
 
-                                while( iterator.hasNext() )
+                                long count = 0;
+
+                                while( iterator.hasNext() && count < 
maxTransactions )
                                 {
                                     TransactionApplicationEvents next = 
iterator.next();
-                                    if( next.timestamp().get() >= 
beforeTimestamp )
+                                    if( next.timestamp().get() < 
beforeTimestamp )
                                     {
-                                        break;
+                                        receiver.receive( next );
+                                        count++;
                                     }
                                 }
-
-                                long count = 0;
-
-                                while( iterator.hasPrevious() && count < 
maxTransactions )
-                                {
-                                    TransactionApplicationEvents next = 
iterator.previous();
-                                    receiver.receive( next );
-                                    count++;
-                                }
                             }
                         });
                     }
@@ -142,7 +136,7 @@ public interface MemoryApplicationEventStoreService
         @Override
         protected void storeEvents( TransactionApplicationEvents 
transactionDomain ) throws IOException
         {
-            store.add(transactionDomain);
+            store.add( transactionDomain );
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/9f02f2ca/libraries/eventsourcing/src/test/java/org/qi4j/library/eventsourcing/application/ApplicationEventTest.java
----------------------------------------------------------------------
diff --git 
a/libraries/eventsourcing/src/test/java/org/qi4j/library/eventsourcing/application/ApplicationEventTest.java
 
b/libraries/eventsourcing/src/test/java/org/qi4j/library/eventsourcing/application/ApplicationEventTest.java
index f566db8..7ccf0a2 100644
--- 
a/libraries/eventsourcing/src/test/java/org/qi4j/library/eventsourcing/application/ApplicationEventTest.java
+++ 
b/libraries/eventsourcing/src/test/java/org/qi4j/library/eventsourcing/application/ApplicationEventTest.java
@@ -92,7 +92,7 @@ public class ApplicationEventTest
 
 
     @Test
-    public void testApplicationEvent() throws UnitOfWorkCompletionException, 
IOException
+    public void testApplicationEvent() throws Exception
     {
         Users users = module.newTransient( Users.class );
 
@@ -107,20 +107,23 @@ public class ApplicationEventTest
 
         UnitOfWork uow1 = module.newUnitOfWork( UsecaseBuilder.newUsecase( 
"User signup" ) );
         uow1.setMetaInfo( administratorPrincipal );
-        users.signup( null, "user1", Arrays.asList( "news1", "news2" ) );
+        users.signup( null, "user1", Arrays.asList( "news-a", "news-b" ) );
         uow1.complete();
 
+        Thread.sleep( 1 ); // For UoWs not getting the same `currentTime`
+
         UnitOfWork uow2 = module.newUnitOfWork();
         uow2.setMetaInfo( administratorPrincipal );
         users.signup( null, "user2", Collections.EMPTY_LIST );
         uow2.complete();
 
+        Thread.sleep( 1 ); // For UoWs not getting the same `currentTime`
+
         UnitOfWork uow3 = module.newUnitOfWork();
         uow3.setMetaInfo( administratorPrincipal );
-        users.signup( null, "user3", Collections.singletonList( "news1" ) );
+        users.signup( null, "user3", Collections.singletonList( "news-c" ) );
         uow3.complete();
 
-
         // receive events from uow2 and later forwards
         EventsInbox afterInbox = new EventsInbox();
         eventSource.transactionsAfter( uow2.currentTime() - 1, 
Integer.MAX_VALUE ).transferTo( afterInbox );
@@ -128,24 +131,32 @@ public class ApplicationEventTest
         assertEquals( 2, afterInbox.getEvents().size() );
 
         ApplicationEvent signupEvent2 = afterInbox.getEvents().get( 0 
).events().get().get( 0 );
+        ApplicationEvent signupEvent3 = afterInbox.getEvents().get( 1 
).events().get().get( 0 );
 
         assertEquals( "signup", signupEvent2.name().get() );
         assertEquals( "user2", ApplicationEventParameters.getParameter( 
signupEvent2, "param1" ) );
         assertEquals( "[]", ApplicationEventParameters.getParameter( 
signupEvent2, "param2" ) );
 
+        assertEquals( "signup", signupEvent3.name().get() );
+        assertEquals( "user3", ApplicationEventParameters.getParameter( 
signupEvent3, "param1" ) );
+        assertEquals( "[\"news-c\"]", ApplicationEventParameters.getParameter( 
signupEvent3, "param2" ) );
+
         // receive events from uow2 backwards
         EventsInbox beforeInbox = new EventsInbox();
         eventSource.transactionsBefore( uow3.currentTime(), Integer.MAX_VALUE 
).transferTo( beforeInbox );
 
         assertEquals( 2, beforeInbox.getEvents().size() );
 
+        signupEvent2 = beforeInbox.getEvents().get( 0 ).events().get().get( 0 
);
         ApplicationEvent signupEvent1 = beforeInbox.getEvents().get( 1 
).events().get().get( 0 );
 
+        assertEquals( "signup", signupEvent2.name().get() );
+        assertEquals( "user2", ApplicationEventParameters.getParameter( 
signupEvent2, "param1" ) );
+        assertEquals( "[]", ApplicationEventParameters.getParameter( 
signupEvent2, "param2" ) );
+
         assertEquals( "signup", signupEvent1.name().get());
         assertEquals( "user1", ApplicationEventParameters.getParameter( 
signupEvent1, "param1" ) );
-        assertEquals( "[\"news1\",\"news2\"]", 
ApplicationEventParameters.getParameter( signupEvent1, "param2" ) );
-
-
+        assertEquals( "[\"news-a\",\"news-b\"]", 
ApplicationEventParameters.getParameter( signupEvent1, "param2" ) );
     }
 
     static class EventsInbox implements Output<TransactionApplicationEvents, 
RuntimeException>

Reply via email to