1- namespace ServiceControl . Audit . Persistence . Tests
1+ namespace ServiceControl . Audit . Persistence . Tests ;
2+
3+ using System ;
4+ using System . Threading ;
5+ using System . Threading . Tasks ;
6+ using NUnit . Framework ;
7+ using Persistence . RavenDB ;
8+ using Raven . Client . Documents . Indexes ;
9+ using Raven . Client . Documents . Operations . Indexes ;
10+ using SagaAudit ;
11+
12+ [ TestFixture ]
13+ class SagaDetailsIndexTests : PersistenceTestFixture
214{
3- using System ;
4- using System . Threading ;
5- using System . Threading . Tasks ;
6- using NUnit . Framework ;
7- using Raven . Client . Documents . Indexes ;
8- using Raven . Client . Documents . Operations . Indexes ;
9- using ServiceControl . SagaAudit ;
10-
11- [ TestFixture ]
12- class SagaDetailsIndexTests : PersistenceTestFixture
15+ [ Test ]
16+ public async Task Deletes_index_that_does_not_have_cap_of_50000 ( )
1317 {
14- [ Test ]
15- public async Task Deletes_index_that_does_not_have_cap_of_50000 ( )
18+ await configuration . DocumentStore . Maintenance . SendAsync ( new DeleteIndexOperation ( DatabaseSetup . SagaDetailsIndexName ) ) ;
19+
20+ var indexWithout50000capDefinition = new IndexDefinition
1621 {
17- await configuration . DocumentStore . Maintenance . SendAsync ( new DeleteIndexOperation ( "SagaDetailsIndex" ) ) ;
18-
19- var indexWithout50000capDefinition = new IndexDefinition
20- {
21- Name = "SagaDetailsIndex" ,
22- Maps =
23- [
24- @"from doc in docs
22+ Name = DatabaseSetup . SagaDetailsIndexName ,
23+ Maps =
24+ [
25+ @"from doc in docs
2526 select new
2627 {
2728 doc.SagaId,
@@ -41,8 +42,8 @@ public async Task Deletes_index_that_does_not_have_cap_of_50000()
4142 }
4243 }
4344 }"
44- ] ,
45- Reduce = @"from result in results
45+ ] ,
46+ Reduce = @"from result in results
4647 group result by result.SagaId
4748 into g
4849 let first = g.First()
@@ -55,69 +56,66 @@ into g
5556 .OrderByDescending(x => x.FinishTime)
5657 .ToList()
5758 }"
58- } ;
59+ } ;
5960
60- var putIndexesOp = new PutIndexesOperation ( indexWithout50000capDefinition ) ;
61+ var putIndexesOp = new PutIndexesOperation ( indexWithout50000capDefinition ) ;
6162
62- await configuration . DocumentStore . Maintenance . SendAsync ( putIndexesOp ) ;
63+ await configuration . DocumentStore . Maintenance . SendAsync ( putIndexesOp ) ;
6364
64- var sagaDetailsIndexOperation = new GetIndexOperation ( "SagaDetailsIndex" ) ;
65- var sagaDetailsIndexDefinition = await configuration . DocumentStore . Maintenance . SendAsync ( sagaDetailsIndexOperation ) ;
65+ var sagaDetailsIndexOperation = new GetIndexOperation ( DatabaseSetup . SagaDetailsIndexName ) ;
66+ var sagaDetailsIndexDefinition = await configuration . DocumentStore . Maintenance . SendAsync ( sagaDetailsIndexOperation ) ;
6667
67- Assert . That ( sagaDetailsIndexDefinition , Is . Not . Null ) ;
68+ Assert . That ( sagaDetailsIndexDefinition , Is . Not . Null ) ;
6869
69- await Persistence . RavenDB . DatabaseSetup . DeleteLegacySagaDetailsIndex ( configuration . DocumentStore , CancellationToken . None ) ;
70+ await DatabaseSetup . DeleteLegacySagaDetailsIndex ( configuration . DocumentStore , CancellationToken . None ) ;
7071
71- sagaDetailsIndexDefinition = await configuration . DocumentStore . Maintenance . SendAsync ( sagaDetailsIndexOperation ) ;
72+ sagaDetailsIndexDefinition = await configuration . DocumentStore . Maintenance . SendAsync ( sagaDetailsIndexOperation ) ;
7273
73- Assert . That ( sagaDetailsIndexDefinition , Is . Null ) ;
74- }
74+ Assert . That ( sagaDetailsIndexDefinition , Is . Null ) ;
75+ }
7576
76- [ Test ]
77- public async Task Does_not_delete_index_that_does_have_cap_of_50000 ( )
78- {
79- await Persistence . RavenDB . DatabaseSetup . DeleteLegacySagaDetailsIndex ( configuration . DocumentStore , CancellationToken . None ) ;
77+ [ Test ]
78+ public async Task Does_not_delete_index_that_does_have_cap_of_50000 ( )
79+ {
80+ await DatabaseSetup . DeleteLegacySagaDetailsIndex ( configuration . DocumentStore , CancellationToken . None ) ;
8081
81- var sagaDetailsIndexOperation = new GetIndexOperation ( "SagaDetailsIndex" ) ;
82- var sagaDetailsIndexDefinition = await configuration . DocumentStore . Maintenance . SendAsync ( sagaDetailsIndexOperation ) ;
82+ var sagaDetailsIndexOperation = new GetIndexOperation ( DatabaseSetup . SagaDetailsIndexName ) ;
83+ var sagaDetailsIndexDefinition = await configuration . DocumentStore . Maintenance . SendAsync ( sagaDetailsIndexOperation ) ;
8384
84- Assert . That ( sagaDetailsIndexDefinition , Is . Not . Null ) ;
85- }
85+ Assert . That ( sagaDetailsIndexDefinition , Is . Not . Null ) ;
86+ }
87+
88+ [ Test ]
89+ public async Task Should_only_reduce_the_last_50000_saga_state_changes ( )
90+ {
91+ var sagaType = "MySagaType" ;
92+ var sagaState = "some-saga-state" ;
8693
87- [ Test ]
88- public async Task Should_only_reduce_the_last_50000_saga_state_changes ( )
94+ await IngestSagaAudits ( new SagaSnapshot
8995 {
90- var sagaType = "MySagaType" ;
91- var sagaState = "some-saga-state" ;
92-
93- await IngestSagaAudits ( new SagaSnapshot
94- {
95- SagaId = Guid . NewGuid ( ) ,
96- SagaType = sagaType ,
97- Status = SagaStateChangeStatus . New ,
98- StateAfterChange = sagaState
99- } ) ;
100-
101- await configuration . CompleteDBOperation ( ) ;
102-
103- using ( var session = configuration . DocumentStore . OpenAsyncSession ( ) )
104- {
105- var sagaDetailsIndexOperation = new GetIndexOperation ( "SagaDetailsIndex" ) ;
106- var sagaDetailsIndexDefinition = await configuration . DocumentStore . Maintenance . SendAsync ( sagaDetailsIndexOperation ) ;
107-
108- Assert . That ( sagaDetailsIndexDefinition . Reduce , Does . Contain ( "Take(50000)" ) , "The SagaDetails index definition does not contain a .Take(50000) to limit the number of saga state changes that are reduced by the map/reduce" ) ;
109- }
110- }
96+ SagaId = Guid . NewGuid ( ) ,
97+ SagaType = sagaType ,
98+ Status = SagaStateChangeStatus . New ,
99+ StateAfterChange = sagaState
100+ } ) ;
101+
102+ await configuration . CompleteDBOperation ( ) ;
103+
104+ var sagaDetailsIndexOperation = new GetIndexOperation ( DatabaseSetup . SagaDetailsIndexName ) ;
105+ var sagaDetailsIndexDefinition = await configuration . DocumentStore . Maintenance . SendAsync ( sagaDetailsIndexOperation ) ;
111106
112- async Task IngestSagaAudits ( params SagaSnapshot [ ] snapshots )
107+ Assert . That ( sagaDetailsIndexDefinition . Reduce , Does . Contain ( "Take(50000)" ) , "The SagaDetails index definition does not contain a .Take(50000) to limit the number of saga state changes that are reduced by the map/reduce" ) ;
108+ }
109+
110+ async Task IngestSagaAudits ( params SagaSnapshot [ ] snapshots )
111+ {
112+ var unitOfWork = await StartAuditUnitOfWork ( snapshots . Length ) ;
113+ foreach ( var snapshot in snapshots )
113114 {
114- var unitOfWork = await StartAuditUnitOfWork ( snapshots . Length ) ;
115- foreach ( var snapshot in snapshots )
116- {
117- await unitOfWork . RecordSagaSnapshot ( snapshot ) ;
118- }
119- await unitOfWork . DisposeAsync ( ) ;
120- await configuration . CompleteDBOperation ( ) ;
115+ await unitOfWork . RecordSagaSnapshot ( snapshot ) ;
121116 }
117+
118+ await unitOfWork . DisposeAsync ( ) ;
119+ await configuration . CompleteDBOperation ( ) ;
122120 }
123121}
0 commit comments