public void TestWriteReadStream()
        {
            EventJournal journal = EventJournal.Open("test");

            journal.Write("name123", 1, EventBatch.Of("type1", "type1_instance1"));
            journal.Write("name456", 1, EventBatch.Of("type2", "type2_instance1"));
            journal.Write("name123", 2, EventBatch.Of("type1-1", "type1-1_instance1"));
            journal.Write("name123", 3, EventBatch.Of("type1-2", "type1-2_instance1"));
            journal.Write("name456", 2, EventBatch.Of("type2-1", "type2-1_instance1"));

            EventStreamReader streamReader = journal.StreamReader();

            EventStream eventStream123 = streamReader.StreamFor("name123");

            Assert.AreEqual(3, eventStream123.StreamVersion);
            Assert.AreEqual(3, eventStream123.Stream.Count);
            Assert.AreEqual(new EventValue("name123", 1, "type1", "type1_instance1", ""), eventStream123.Stream[0]);
            Assert.AreEqual(new EventValue("name123", 2, "type1-1", "type1-1_instance1", ""), eventStream123.Stream[1]);
            Assert.AreEqual(new EventValue("name123", 3, "type1-2", "type1-2_instance1", ""), eventStream123.Stream[2]);

            EventStream eventStream456 = streamReader.StreamFor("name456");

            Assert.AreEqual(2, eventStream456.StreamVersion);
            Assert.AreEqual(2, eventStream456.Stream.Count);
            Assert.AreEqual(new EventValue("name456", 1, "type2", "type2_instance1", ""), eventStream456.Stream[0]);
            Assert.AreEqual(new EventValue("name456", 2, "type2-1", "type2-1_instance1", ""), eventStream456.Stream[1]);

            journal.Close();
        }
        public void TestWriteReadStreamSnapshot()
        {
            EventJournal journal = EventJournal.Open("test");

            journal.Write("name123", 1, EventBatch.Of("type1", "type1_instance1", "SNAPSHOT123-1"));
            journal.Write("name456", 1, EventBatch.Of("type2", "type2_instance1", "SNAPSHOT456-1"));
            journal.Write("name123", 2, EventBatch.Of("type1-1", "type1-1_instance1", "SNAPSHOT123-2"));
            journal.Write("name123", 3, EventBatch.Of("type1-2", "type1-2_instance1"));
            journal.Write("name456", 2, EventBatch.Of("type2-1", "type2-1_instance1", "SNAPSHOT456-2"));

            EventStreamReader streamReader = journal.StreamReader();

            EventStream eventStream123 = streamReader.StreamFor("name123");

            Assert.AreEqual("name123", eventStream123.StreamName);
            Assert.AreEqual(3, eventStream123.StreamVersion);
            Assert.AreEqual(1, eventStream123.Stream.Count);
            Assert.AreEqual("SNAPSHOT123-2", eventStream123.Snapshot);

            EventStream eventStream456 = streamReader.StreamFor("name456");

            Assert.AreEqual("name456", eventStream456.StreamName);
            Assert.AreEqual(2, eventStream456.StreamVersion);
            Assert.AreEqual(0, eventStream456.Stream.Count);
            Assert.AreEqual("SNAPSHOT456-2", eventStream456.Snapshot);

            journal.Close();
        }
 public void Write(String streamName, int streamVersion, EventBatch batch)
 {
     lock (store)
     {
         foreach (EventBatch.Entry entry in batch.Entries)
         {
             store.Add(new EventValue(streamName, streamVersion, entry.Type, entry.Body, entry.Snapshot));
         }
     }
 }
 public void Write(EventBatch batch)
 {
     lock (store)
     {
         foreach (EventBatch.Entry entry in batch.Entries)
         {
             store.Add(new EventValue("", 0, entry.Type, entry.Body, ""));
         }
     }
 }
        protected EventBatch ToBatch(List <DomainEvent> domainEvents)
        {
            EventBatch batch = new EventBatch(domainEvents.Count);

            foreach (DomainEvent domainEvent in domainEvents)
            {
                string eventBody = Serialization.Serialize(domainEvent);
                batch.AddEntry(domainEvent.GetType().AssemblyQualifiedName, eventBody);
            }
            return(batch);
        }
        public void TestWriteRead()
        {
            EventJournal journal = EventJournal.Open("test");

            journal.Write("name123", 1, EventBatch.Of("type1", "type1_instance1"));
            journal.Write("name456", 1, EventBatch.Of("type2", "type2_instance1"));
            EventJournalReader reader = journal.Reader("test_reader");

            Assert.AreEqual(new StoredEvent(0, new EventValue("name123", 1, "type1", "type1_instance1", "")), reader.ReadNext());
            reader.Acknowledge(0);
            Assert.AreEqual(new StoredEvent(1, new EventValue("name456", 1, "type2", "type2_instance1", "")), reader.ReadNext());
            reader.Acknowledge(1);
            Assert.AreEqual(new StoredEvent(-1, new EventValue("", -1, "", "", "")), reader.ReadNext());

            journal.Close();
        }
        public void TestEventJournalPublisher()
        {
            EventJournal          eventJournal     = EventJournal.Open("test-ej");
            MessageBus            messageBus       = MessageBus.Start("test-bus");
            Topic                 topic            = messageBus.OpenTopic("test-topic");
            EventJournalPublisher journalPublisher =
                EventJournalPublisher.From(eventJournal.Name, messageBus.Name, topic.Name);

            EventJournalPublisherTestSubscriber subscriber = new EventJournalPublisherTestSubscriber();

            topic.Subscribe(subscriber);

            EventBatch batch1 = new EventBatch();

            for (int idx = 0; idx < 3; ++idx)
            {
                batch1.AddEntry("test1type", "test1instance" + idx);
            }
            eventJournal.Write("test1", 0, batch1);

            EventBatch batch2 = new EventBatch();

            for (int idx = 0; idx < 3; ++idx)
            {
                batch2.AddEntry("test2type", "test2instance" + idx);
            }
            eventJournal.Write("test2", 0, batch2);

            subscriber.WaitForExpectedMessages(6);

            topic.Close();

            journalPublisher.Close();

            subscriber.handledMessages.Count.Should().Be(6);
        }