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 : /// <summary>
16 : /// All these tests just test, test code.
17 : /// </summary>
18 : public class When_saving_same_aggregate_in_parallel
19 1 : {
20 : private CacheRepository<ISingleSignOnToken> _rep1;
21 : private CacheRepository<ISingleSignOnToken> _rep2;
22 : private TestAggregate _aggregate;
23 : private TestInMemoryEventStore _testStore;
24 :
25 : [SetUp]
26 0 : public void Setup()
27 : {
28 : // This will clear the cache between runs.
29 : var cacheKeys = MemoryCache.Default.Select(kvp => kvp.Key).ToList();
30 : foreach (var cacheKey in cacheKeys)
31 : MemoryCache.Default.Remove(cacheKey);
32 :
33 : var dependencyResolver = new TestDependencyResolver(null);
34 : var aggregateFactory = new AggregateFactory(dependencyResolver, dependencyResolver.Resolve<ILogger>());
35 : _testStore = new TestInMemoryEventStore();
36 : _rep1 = new CacheRepository<ISingleSignOnToken>(new Repository<ISingleSignOnToken>(aggregateFactory, _testStore, new TestEventPublisher(), new NullCorrelationIdHelper()), _testStore);
37 : _rep2 = new CacheRepository<ISingleSignOnToken>(new Repository<ISingleSignOnToken>(aggregateFactory, _testStore, new TestEventPublisher(), new NullCorrelationIdHelper()), _testStore);
38 :
39 : _aggregate = new TestAggregate(Guid.NewGuid());
40 : _rep1.Save(_aggregate);
41 :
42 : var t1 = new Task(() =>
43 : {
44 : for (var i = 0; i < 100; i++)
45 : {
46 : var aggregate = _rep1.Get<TestAggregate>(_aggregate.Id);
47 : aggregate.DoSomething();
48 : _rep1.Save(aggregate);
49 : }
50 : });
51 :
52 : var t2 = new Task(() =>
53 : {
54 : for (var i = 0; i < 100; i++)
55 : {
56 : var aggregate = _rep2.Get<TestAggregate>(_aggregate.Id);
57 : aggregate.DoSomething();
58 : _rep2.Save(aggregate);
59 : }
60 : });
61 : var t3 = new Task(() =>
62 : {
63 : for (var i = 0; i < 100; i++)
64 : {
65 : var aggregate = _rep2.Get<TestAggregate>(_aggregate.Id);
66 : aggregate.DoSomething();
67 : _rep2.Save(aggregate);
68 : }
69 : });
70 : t1.Start();
71 : t2.Start();
72 : t3.Start();
73 :
74 : Task.WaitAll(new[] {t1,t2, t3});
75 : }
76 :
77 : //[Test]
78 0 : public void Should_not_get_more_than_one_event_with_same_id()
79 : {
80 : Assert.That(_testStore.Events.Select(x => x.Version).Distinct().Count(), Is.EqualTo(_testStore.Events.Count));
81 : }
82 :
83 : //[Test]
84 0 : public void Should_save_all_events()
85 : {
86 : Assert.That(_testStore.Events.Count(), Is.EqualTo(295));
87 : }
88 : }
89 : }
|