Beispiel #1
0
        public void commits_with_unexpected_starting_sequence_should_fail()
        {
            var commitId = Guid.NewGuid();
            var commit2Id = Guid.NewGuid();
            var commits = new CommitBuilder()
               .NewCommit(commitId, "foo", "stream")
               .Event(new EventForStorage(Guid.NewGuid(), 1, DateTime.UtcNow, "someType", null, "someBody"))
               .Event(new EventForStorage(Guid.NewGuid(), 2, DateTime.UtcNow, "someType", null, "someBody"))
               .NewCommit(commit2Id, "foo", "anotherStream")
               .Event(new EventForStorage(Guid.NewGuid(), 1, DateTime.UtcNow, "someType", null, "someBody"))
               .Build();

            _storage.Store(commits);

            var commit3Id = Guid.NewGuid();
            var commit4Id = Guid.NewGuid();
            var newCommits = new CommitBuilder()
               .NewCommit(commit3Id, "foo", "stream")
               .Event(new EventForStorage(Guid.NewGuid(), 2, DateTime.UtcNow, "someType", null, "someBody"))
               .Event(new EventForStorage(Guid.NewGuid(), 3, DateTime.UtcNow, "someType", null, "someBody"))
               .NewCommit(commit4Id, "foo", "anotherStream")
               .Event(new EventForStorage(Guid.NewGuid(), 2, DateTime.UtcNow, "someType", null, "someBody"))
               .Build();

            var results = _storage.Store(newCommits);
            Assert.AreEqual(1, results.SuccessfulCommits.Length);
            Assert.AreEqual(1, results.FailedDueToConcurrencyCommits.Length);
            Assert.AreEqual(commit4Id, results.SuccessfulCommits[0]);
            Assert.AreEqual(commit3Id, results.FailedDueToConcurrencyCommits[0]);
        }
Beispiel #2
0
        public void commits_with_concurrency_check_disabled_should_append()
        {
            var commits = new CommitBuilder()
                .NewCommit(Guid.NewGuid(), "foo", "stream")
                .Event(new EventForStorage(Guid.NewGuid(), -1, DateTime.UtcNow, "someType", null, "someBody"))
                .Event(new EventForStorage(Guid.NewGuid(), -1, DateTime.UtcNow, "someType", null, "someBody"))
                .NewCommit(Guid.NewGuid(), "foo", "AnotherStream")
                .Event(new EventForStorage(Guid.NewGuid(), -1, DateTime.UtcNow, "someType", null, "someBody"))
                .Build();

            var result = _storage.Store(commits);

            Assert.AreEqual(2, result.SuccessfulCommits.Length);
        }
Beispiel #3
0
        public void commits_with_concurrency_check_disabled_should_append_when_there_are_existing_events()
        {
            var commits = new CommitBuilder()
                .NewCommit(Guid.NewGuid(), "foo", "stream")
                .Event(new EventForStorage(Guid.NewGuid(), 1, DateTime.UtcNow, "someType", null, "someBody"))
                .Event(new EventForStorage(Guid.NewGuid(), 2, DateTime.UtcNow, "someType", null, "someBody"))
                .Build();

            _storage.Store(commits);

            commits = new CommitBuilder()
                .NewCommit(Guid.NewGuid(), "foo", "stream")
                .Event(new EventForStorage(Guid.NewGuid(), -1, DateTime.UtcNow, "someNewType", null, "someBody1"))
                .Event(new EventForStorage(Guid.NewGuid(), -1, DateTime.UtcNow, "someNewType2", null, "someBody2"))
                .Build();

            var result = _storage.Store(commits);

            Assert.AreEqual(1, result.SuccessfulCommits.Length);
        }
Beispiel #4
0
        public List<CommitModel> InsertEvents(Dictionary<Guid, CreateEventsMetaData> createEventDetails)
        {
            var listOfCommits = new List<CommitModel>();
            foreach (var key in createEventDetails.Keys)
            {
                var createEventMetaData = createEventDetails[key];
                var listOfEvents = new List<EventStorageInfo>();
                for (var i = 1; i <= createEventMetaData.NumberOfEvents; i++)
                {
                    var @event = new EventStorageInfo(Guid.NewGuid(), i, createEventMetaData.SameTimeStamps[i - 1], "sometype", "somebody", null);
                    listOfEvents.Add(@event);
                }
                var commit = new CommitModel(key, createEventMetaData.Context, createEventMetaData.Stream,
                    listOfEvents.ToArray());
                listOfCommits.Add(commit);

            }

            foreach (var commit in listOfCommits)
            {
                var newcommit = new CommitBuilder()
                    .NewCommit(commit.CommitId, commit.Context, commit.Stream);
                var firstEvent = commit.Events[0];
                var anotherCommitingEvent = newcommit.Event(new EventForStorage(firstEvent.EventId, firstEvent.Sequence, firstEvent.Timestamp, firstEvent.TypeKey, null, firstEvent.Body));
                if (commit.Events.Length > 1)
                {
                    for (var e = 1; e < commit.Events.Length; e++)
                    {
                        anotherCommitingEvent =
                            anotherCommitingEvent.Event(new EventForStorage(commit.Events[e].EventId,
                                commit.Events[e].Sequence, commit.Events[e].Timestamp, commit.Events[e].TypeKey, null, commit.Events[e].Body));
                    }
                }
                var builtCommit = anotherCommitingEvent.Build();
                _storage.Store(builtCommit);

            }
            return listOfCommits;
        }
Beispiel #5
0
 public ExpectingEvent(CommitInfo commit, CommitBuilder instance)
 {
     _commit = commit;
     _instance = instance;
 }
Beispiel #6
0
 public ExpectingAnotherEventOrCommit(CommitInfo commit, CommitBuilder instance)
 {
     _commit = commit;
     _instance = instance;
 }
Beispiel #7
0
        public void commit_having_initial_sequence_0_should_not_be_saved()
        {
            var commits = new CommitBuilder()
                .NewCommit(Guid.NewGuid(), "foo", "stream")
                .Event(new EventForStorage(Guid.NewGuid(), 1, DateTime.UtcNow, "someType", null, "someBody"))
                .Event(new EventForStorage(Guid.NewGuid(), 2, DateTime.UtcNow, "someType", null, "someBody"))
                .NewCommit(Guid.NewGuid(), "foo", "AnotherStream")
                .Event(new EventForStorage(Guid.NewGuid(), 0, DateTime.UtcNow, "someType", null, "someBody"))
                .Build();

            var result=_storage.Store(commits);

            Assert.AreEqual(1,result.SuccessfulCommits.Length);
            Assert.AreEqual(1,result.FailedDueToConcurrencyCommits.Length);
        }
Beispiel #8
0
        public void Successful_commits_should_be_stored_in_a_batch_of_successful_and_unsucessful_commits()
        {
            var commitId = Guid.NewGuid();
            var commit2Id = Guid.NewGuid();
            var commits = new CommitBuilder()
               .NewCommit(commitId, "foo", "stream")
               .Event(new EventForStorage(Guid.NewGuid(), 1, DateTime.UtcNow, "someType", null, "someBody"))
               .Event(new EventForStorage(Guid.NewGuid(), 2, DateTime.UtcNow, "someType", null, "someBody"))
               .NewCommit(commit2Id, "foo", "anotherStream")
               .Event(new EventForStorage(Guid.NewGuid(), 1, DateTime.UtcNow, "someType", null, "someBody"))
               .Build();

            var results=_storage.Store(commits);
            var successfulCommitIds = new List<Guid>();
            successfulCommitIds.AddRange(results.SuccessfulCommits);
            var commit3Id = Guid.NewGuid();
            var commit4Id = Guid.NewGuid();
            var newCommits = new CommitBuilder()
               .NewCommit(commit3Id, "foo", "stream")
               .Event(new EventForStorage(Guid.NewGuid(), 2, DateTime.UtcNow, "someType", null, "someBody"))
               .Event(new EventForStorage(Guid.NewGuid(), 3, DateTime.UtcNow, "someType", null, "someBody"))
               .NewCommit(commit4Id, "foo", "anotherStream")
               .Event(new EventForStorage(Guid.NewGuid(), 2, DateTime.UtcNow, "someType", null, "someBody"))
               .Build();

            var expectedListOfCommitIds = new List<Guid>();

            expectedListOfCommitIds.Add(commitId);
            expectedListOfCommitIds.Add(commit2Id);
            expectedListOfCommitIds.Add(commit4Id);

            results=_storage.Store(newCommits);
            successfulCommitIds.AddRange(results.SuccessfulCommits);

            var listOfStoredEvents = new List<EventInStorage>();

            var loadedEvents = _storage.LoadEvents("foo", "stream");
            listOfStoredEvents.AddRange(loadedEvents);
            loadedEvents = _storage.LoadEvents("foo", "anotherStream");
            listOfStoredEvents.AddRange(loadedEvents);

            var successfulResultCount = listOfStoredEvents.Count;

            Assert.AreEqual(4, successfulResultCount);
            Assert.That(successfulCommitIds, Is.SubsetOf(expectedListOfCommitIds));
        }
Beispiel #9
0
        public void should_fetch_strored_events()
        {
            var event1Id = Guid.NewGuid();
            var event2Id = Guid.NewGuid();
            var event3Id = Guid.NewGuid();

            var commits = new CommitBuilder()
                .NewCommit(Guid.NewGuid(), "foo", "stream")
                .Event(new EventForStorage(event1Id, 1, DateTime.UtcNow, "someType", null, "someBody"))
                .Event(new EventForStorage(event2Id, 2, DateTime.UtcNow, "someType", "header1:foo", "someBody"))
                .NewCommit(Guid.NewGuid(), "foo", "AnotherStream")
                .Event(new EventForStorage(event3Id, 1, DateTime.UtcNow, "someType", null, "someBody"))
                .Build();

            _storage.Store(commits);

            var requests = new[]
                               {
                                   new FetchEventRequest(event1Id, "foo", "stream"),
                                   new FetchEventRequest(event2Id, "foo", "stream"),
                                   new FetchEventRequest(event3Id, "foo", "AnotherStream"),
                                   new FetchEventRequest(event1Id, "foo", "Missing")
                               };

            var results = _storage.FetchEvent(requests);

            Assert.AreEqual(event1Id, results[requests[0].RequestId].EventId);
            Assert.AreEqual(event2Id, results[requests[1].RequestId].EventId);
            Assert.AreEqual(event3Id, results[requests[2].RequestId].EventId);
            Assert.IsFalse(results.ContainsKey(requests[3].RequestId));

            Assert.IsNull(results[requests[0].RequestId].Headers);
            Assert.AreEqual("header1:foo", results[requests[1].RequestId].Headers);
            Assert.IsNull(results[requests[2].RequestId].Headers);
        }
Beispiel #10
0
        public void should_be_able_to_fetch_a_collection_of_stored_events()
        {
            var commits = new CommitBuilder()
                .NewCommit(Guid.NewGuid(), "foo", "stream")
                .Event(new EventForStorage(Guid.NewGuid(), 1, DateTime.UtcNow, "someType", null, "someBody"))
                .Event(new EventForStorage(Guid.NewGuid(), 2, DateTime.UtcNow, "someType", null, "someBody"))
                .NewCommit(Guid.NewGuid(), "foo", "anotherStream")
                .Event(new EventForStorage(Guid.NewGuid(), 1, DateTime.UtcNow, "someType", null, "someBody"))
                .Build();

            _storage.Store(commits);

            var events = _storage.LoadEvents("foo", "stream");

            Assert.AreEqual(2, events.Length, "stream should have two events");
            Assert.AreEqual("stream", events[0].Stream);
            Assert.AreEqual("someType", events[0].TypeKey);
            Assert.AreEqual("someBody", events[0].Body);

            var events2 = _storage.LoadEvents("foo", "anotherStream");

            Assert.AreEqual(1, events2.Length, "another stream should have 1 event.");
            Assert.AreEqual("anotherStream", events2[0].Stream);
            Assert.AreEqual("someType", events2[0].TypeKey);
            Assert.AreEqual("someBody", events2[0].Body);
        }
Beispiel #11
0
        public void out_of_two_same_commits_the_one_with_lowest_timestamp_only_should_be_stored()
        {
            var commit1Id = Guid.NewGuid();
            var commit2Id = Guid.NewGuid();
            var commits = new CommitBuilder()
                .NewCommit(commit1Id, "foo", "stream")
                .Event(new EventForStorage(Guid.NewGuid(), 1, DateTime.UtcNow.AddDays(1), "someType", null, "someBody"))
                .Event(new EventForStorage(Guid.NewGuid(), 2, DateTime.UtcNow.AddDays(1), "someType", null, "someBody"))
                .NewCommit(commit2Id, "foo", "stream")
                .Event(new EventForStorage(Guid.NewGuid(), 1, DateTime.UtcNow, "someType", null, "someBody"))
                .Event(new EventForStorage(Guid.NewGuid(), 2, DateTime.UtcNow, "someType", null, "someBody"))
                .Build();

            var numberOfInitialCommits=commits.Commits.Length;

            Assert.AreEqual(2,numberOfInitialCommits);

            var result=_storage.Store(commits);

            var successfulCommit = result.SuccessfulCommits[0];
            Assert.That(commit2Id,Is.EqualTo(successfulCommit));

            var unsuccessfulCommit = result.FailedDueToConcurrencyCommits[0];
            Assert.That(commit1Id, Is.EqualTo(unsuccessfulCommit));
        }
Beispiel #12
0
        public void out_of_two_same_commits_the_one_comes_first_should_be_saved()
        {
            var commit1Id = Guid.NewGuid();
            var commit2Id = Guid.NewGuid();

            var timeStamp = DateTime.UtcNow;
            var commits = new CommitBuilder()
                .NewCommit(commit1Id, "foo", "stream")
                .Event(new EventForStorage(Guid.NewGuid(), 1, timeStamp, "someType", null, "someBody"))
                .Event(new EventForStorage(Guid.NewGuid(), 2, timeStamp, "someType", null, "someBody"))
                .NewCommit(commit2Id, "foo", "stream")
                .Event(new EventForStorage(Guid.NewGuid(), 1, timeStamp, "someType", null, "someBody"))
                .Event(new EventForStorage(Guid.NewGuid(), 2, timeStamp, "someType", null, "someBody"))
                .Build();

            var result = _storage.Store(commits);

            Assert.AreEqual(1, result.SuccessfulCommits.Length);
            Assert.AreEqual(1, result.FailedDueToConcurrencyCommits.Length);
        }