Line data Source code
1 : using System;
2 : using System.Linq;
3 : using System.Runtime.Caching;
4 : using System.Threading.Tasks;
5 : using cdmdotnet.Logging;
6 : using Cqrs.Cache;
7 : using Cqrs.Domain;
8 : using Cqrs.Domain.Factories;
9 : using Cqrs.Authentication;
10 : using Cqrs.Tests.Substitutes;
11 : using NUnit.Framework;
12 :
13 : namespace Cqrs.Tests.Cache
14 : {
15 : public class When_saving_two_aggregates_in_parallel
16 0 : {
17 : private CacheRepository<ISingleSignOnToken> _rep1;
18 : private TestAggregate _aggregate1;
19 : private TestInMemoryEventStore _testStore;
20 : private TestAggregate _aggregate2;
21 :
22 : [SetUp]
23 0 : public void Setup()
24 : {
25 : // This will clear the cache between runs.
26 : var cacheKeys = MemoryCache.Default.Select(kvp => kvp.Key).ToList();
27 : foreach (var cacheKey in cacheKeys)
28 : MemoryCache.Default.Remove(cacheKey);
29 :
30 : _testStore = new TestInMemoryEventStore();
31 : var dependencyResolver = new TestDependencyResolver(null);
32 : var aggregateFactory = new AggregateFactory(dependencyResolver, dependencyResolver.Resolve<ILogger>());
33 : _rep1 = new CacheRepository<ISingleSignOnToken>(new Repository<ISingleSignOnToken>(aggregateFactory, _testStore, new TestEventPublisher(), new NullCorrelationIdHelper()), _testStore);
34 :
35 : _aggregate1 = new TestAggregate(Guid.NewGuid());
36 : _aggregate2 = new TestAggregate(Guid.NewGuid());
37 :
38 : _rep1.Save(_aggregate1);
39 : _rep1.Save(_aggregate2);
40 :
41 : var t1 = new Task(() =>
42 : {
43 : for (var i = 0; i < 100; i++)
44 : {
45 : var aggregate = _rep1.Get<TestAggregate>(_aggregate1.Id);
46 : aggregate.DoSomething();
47 : _rep1.Save(aggregate);
48 : }
49 : });
50 :
51 : var t2 = new Task(() =>
52 : {
53 : for (var i = 0; i < 100; i++)
54 : {
55 : var aggregate = _rep1.Get<TestAggregate>(_aggregate2.Id);
56 : aggregate.DoSomething();
57 : _rep1.Save(aggregate);
58 : }
59 : });
60 : t1.Start();
61 : t2.Start();
62 :
63 : Task.WaitAll(new[] {t1,t2});
64 : }
65 :
66 : [Test]
67 0 : public void Should_not_get_more_than_one_event_with_same_id()
68 : {
69 : Assert.That(_testStore.Events.Select(x => x.Version).Count(), Is.EqualTo(_testStore.Events.Count));
70 : }
71 :
72 : [Test]
73 0 : public void Should_save_all_events()
74 : {
75 : Assert.That(_testStore.Events.Count(), Is.EqualTo(202));
76 : }
77 :
78 : [Test]
79 0 : public void Should_distibute_events_correct()
80 : {
81 : var aggregate1 = _rep1.Get<TestAggregate>(_aggregate2.Id);
82 : Assert.That(aggregate1.DidSomethingCount, Is.EqualTo(100));
83 : var aggregate2 = _rep1.Get<TestAggregate>(_aggregate2.Id);
84 : Assert.That(aggregate2.DidSomethingCount, Is.EqualTo(100));
85 : }
86 : }
87 : }
|