@@ -20,14 +20,18 @@ namespace Xtensive.Orm.Tests.Storage.Randomized
2020 [ TestFixture ]
2121 public sealed class RandomizedTest : AutoBuildTest
2222 {
23- private const int iterationCount = 1000 ;
24- private const int initialNodeCount = 5000 ;
25- private const int initialTreeCount = 10 ;
23+ private const int IterationCount = 1000 ;
24+ private const int InitialNodeCount = 5000 ;
25+ private const int InitialTreeCount = 10 ;
26+ private const int ConstSeed = 1439675735 ;
27+
28+ private readonly List < Key > entitySetCache = new ( ) ;
29+
2630 private List < Pair < Key , int > > nodesData ;
27- private List < Action > actions ;
31+ private List < Action < Session > > actions ;
2832 private Random randomProvider ;
2933 private bool isSettingUp ;
30- private readonly List < Key > entitySetCache = new List < Key > ( ) ;
34+
3135
3236 protected override DomainConfiguration BuildConfiguration ( )
3337 {
@@ -40,18 +44,23 @@ protected override DomainConfiguration BuildConfiguration()
4044 [ SetUp ]
4145 public void SetUp ( )
4246 {
43- var seed = 1439675735 ; // GetSeed();
47+ var seed = GetSeed ( useConst : true ) ;
4448 Console . WriteLine ( "Seed: {0}" , seed ) ;
4549 randomProvider = new Random ( seed ) ;
46- actions = new List < Action > { AddNode , RemoveNode , TransferNode , AddTree } ;
50+ actions = new List < Action < Session > > { AddNode , RemoveNode , TransferNode , AddTree , /*RemoveTree*/ } ;
4751 nodesData = new List < Pair < Key , int > > ( ) ;
52+
4853 using ( var session = Domain . OpenSession ( ) )
4954 using ( var tx = session . OpenTransaction ( IsolationLevel . ReadCommitted ) ) {
5055 isSettingUp = true ;
51- for ( int i = 0 ; i < initialTreeCount ; i ++ )
52- AddTree ( ) ;
53- for ( var j = 0 ; j < initialNodeCount - initialTreeCount ; j ++ )
54- AddNode ( ) ;
56+ for ( var i = 0 ; i < InitialTreeCount ; i ++ ) {
57+ AddTree ( session ) ;
58+ }
59+
60+ for ( var j = 0 ; j < InitialNodeCount - InitialTreeCount ; j ++ ) {
61+ AddNode ( session ) ;
62+ }
63+
5564 isSettingUp = false ;
5665 tx . Complete ( ) ;
5766 }
@@ -61,46 +70,58 @@ public void SetUp()
6170 public void CombinedTest ( )
6271 {
6372 Require . AnyFeatureSupported ( ProviderFeatures . RowNumber | ProviderFeatures . NativePaging ) ;
64- using ( var session = Domain . OpenSession ( ) )
65- for ( int i = 0 ; i < iterationCount ; i ++ )
66- GetAction ( ) . Invoke ( ) ;
73+
74+ using ( var session = Domain . OpenSession ( ) ) {
75+ for ( var i = 0 ; i < IterationCount ; i ++ ) {
76+ GetAction ( ) . Invoke ( session ) ;
77+ }
78+ }
6779
6880 using ( var session = Domain . OpenSession ( ) )
6981 using ( session . OpenTransaction ( ) ) {
70- var trees = Session . Demand ( ) . Query . All < Tree > ( ) . ToList ( ) ;
71- long totalCount = 0 ;
72- foreach ( var tree in trees )
82+ var trees = session . Query . All < Tree > ( ) . ToList ( ) ;
83+ var totalCount = 0L ;
84+ foreach ( var tree in trees ) {
7385 totalCount += ValidateNodes ( tree . Root ) + 1 ;
86+ }
87+
7488 Assert . AreEqual ( nodesData . Count , totalCount ) ;
7589 }
7690 }
7791
7892 private long ValidateNodes ( TreeNode current )
7993 {
80- if ( current . Parent == null )
94+ if ( current . Parent == null ) {
8195 Assert . IsNotNull ( current . Tree ) ;
82- else
96+ }
97+ else {
8398 Assert . IsNull ( current . Tree ) ;
99+ }
100+
84101 var nodePair = nodesData . Where ( pair => pair . First == current . Key ) . First ( ) ;
85102 Assert . AreEqual ( current . Children . Count , nodePair . Second ) ;
103+
86104 var result = current . Children . Count ;
87- if ( current . Parent != null )
105+ if ( current . Parent != null ) {
88106 Assert . IsTrue ( current . Parent . Children . Contains ( current ) ) ;
89- foreach ( var node in current . Children )
107+ }
108+ foreach ( var node in current . Children ) {
90109 result += ValidateNodes ( node ) ;
110+ }
111+
91112 return result ;
92113 }
93114
94- private void AddNode ( )
115+ private void AddNode ( Session session )
95116 {
96117 Key newNodeKey ;
97118 Key parentNodeKey ;
98119 try {
99- using ( var tx = isSettingUp ? null : Session . Demand ( ) . OpenTransaction ( ) ) {
120+ using ( var tx = isSettingUp ? null : session . OpenTransaction ( ) ) {
100121 parentNodeKey = nodesData [ GetNodeIndex ( ) ] . First ;
101- var parentNode = Session . Demand ( ) . Query . Single < TreeNode > ( parentNodeKey ) ;
122+ var parentNode = session . Query . Single < TreeNode > ( parentNodeKey ) ;
102123 var newNode = new TreeNode ( ) ;
103- parentNode . Children . Add ( newNode ) ;
124+ _ = parentNode . Children . Add ( newNode ) ;
104125 newNodeKey = newNode . Key ;
105126 ThrowOrCompleteTransaction ( tx ) ;
106127 }
@@ -112,25 +133,29 @@ private void AddNode()
112133 UpdateChildrenCount ( parentNodeKey , true ) ;
113134 }
114135
115- private void RemoveNode ( )
136+ private void RemoveNode ( Session session )
116137 {
117138 Key removedNodeKey ;
118139 Key parentNodeKey ;
119140 long removedNodeChildCount ;
120141 int removedNodeIndex ;
121142 try {
122- using ( var tx = Session . Demand ( ) . OpenTransaction ( ) ) {
143+ using ( var tx = session . OpenTransaction ( ) ) {
123144 removedNodeIndex = GetNodeIndex ( ) ;
124145 removedNodeKey = nodesData [ removedNodeIndex ] . First ;
125- var removedNode = Session . Demand ( ) . Query . Single < TreeNode > ( removedNodeKey ) ;
126- if ( removedNode . Parent == null )
146+ var removedNode = session . Query . Single < TreeNode > ( removedNodeKey ) ;
147+ if ( removedNode . Parent == null ) {
127148 return ;
149+ }
150+
128151 parentNodeKey = removedNode . Parent . Key ;
129152 removedNodeChildCount = removedNode . Children . Count ;
130153 entitySetCache . Clear ( ) ;
131154 entitySetCache . AddRange ( removedNode . Children . Select ( n => n . Key ) ) ;
132- foreach ( var key in entitySetCache )
133- removedNode . Parent . Children . Add ( Session . Demand ( ) . Query . Single < TreeNode > ( key ) ) ;
155+ foreach ( var key in entitySetCache ) {
156+ _ = removedNode . Parent . Children . Add ( session . Query . Single < TreeNode > ( key ) ) ;
157+ }
158+
134159 removedNode . Children . Clear ( ) ;
135160 removedNode . Remove ( ) ;
136161 ThrowOrCompleteTransaction ( tx ) ;
@@ -143,29 +168,37 @@ private void RemoveNode()
143168 UpdateChildrenCount ( parentNodeKey , ( int ) ( removedNodeChildCount - 1 ) ) ;
144169 }
145170
146- private void TransferNode ( )
171+ private void TransferNode ( Session session )
147172 {
148173 Key oldParentKey ;
149174 Key newParentKey ;
150175 try {
151- using ( var tx = Session . Demand ( ) . OpenTransaction ( ) ) {
152- var treeCount = Session . Demand ( ) . Query . All < Tree > ( ) . Count ( ) ;
153- if ( nodesData . Count == 1 || treeCount == 1 )
176+ using ( var tx = session . OpenTransaction ( ) ) {
177+ var treeCount = session . Query . All < Tree > ( ) . Count ( ) ;
178+ if ( nodesData . Count == 1 || treeCount == 1 ) {
154179 return ;
180+ }
181+
155182 var nodeIndex = GetNodeIndex ( ) ;
156183 var nodeKey = nodesData [ nodeIndex ] . First ;
157- var node = Session . Demand ( ) . Query . Single < TreeNode > ( nodeKey ) ;
158- if ( node . Parent == null )
184+ var node = session . Query . Single < TreeNode > ( nodeKey ) ;
185+ if ( node . Parent == null ) {
159186 return ;
187+ }
188+
160189 var root = node ;
161- while ( root . Tree == null )
190+ while ( root . Tree == null ) {
162191 root = root . Parent ;
163- var newParentNode = Session . Demand ( ) . Query . All < Tree > ( ) . Where ( t => t != root . Tree )
164- . Skip ( randomProvider . Next ( 0 , treeCount ) ) . First ( ) . Root ;
192+ }
193+
194+ var newParentNode = session . Query . All < Tree > ( ) . Where ( t => t != root . Tree )
195+ . Skip ( randomProvider . Next ( 0 , treeCount ) )
196+ . First ( )
197+ . Root ;
165198 newParentKey = newParentNode . Key ;
166199 oldParentKey = node . Parent . Key ;
167- node . Parent . Children . Remove ( node ) ;
168- newParentNode . Children . Add ( node ) ;
200+ _ = node . Parent . Children . Remove ( node ) ;
201+ _ = newParentNode . Children . Add ( node ) ;
169202 ThrowOrCompleteTransaction ( tx ) ;
170203 }
171204 }
@@ -176,11 +209,11 @@ private void TransferNode()
176209 UpdateChildrenCount ( oldParentKey , false ) ;
177210 }
178211
179- private void AddTree ( )
212+ private void AddTree ( Session session )
180213 {
181214 Key key ;
182215 try {
183- using ( var tx = isSettingUp ? null : Session . Demand ( ) . OpenTransaction ( ) ) {
216+ using ( var tx = isSettingUp ? null : session . OpenTransaction ( ) ) {
184217 var tree = new Tree ( ) ;
185218 tree . Root = new TreeNode { Tree = tree } ;
186219 key = tree . Root . Key ;
@@ -193,18 +226,24 @@ private void AddTree()
193226 nodesData . Add ( new Pair < Key , int > ( key , 0 ) ) ;
194227 }
195228
196- private void RemoveTree ( )
229+ #pragma warning disable IDE0051 // Remove unused private members
230+ private void RemoveTree ( Session session )
231+ #pragma warning restore IDE0051 // Remove unused private members
197232 {
198233 var treeNodeKeys = new List < Key > ( ) ;
199234 try {
200- using ( var tx = Session . Demand ( ) . OpenTransaction ( ) ) {
201- if ( Session . Demand ( ) . Query . All < Tree > ( ) . Count ( ) == 1 )
235+ using ( var tx = session . OpenTransaction ( ) ) {
236+ if ( session . Query . All < Tree > ( ) . Count ( ) == 1 ) {
202237 return ;
238+ }
239+
203240 var nodeIndex = GetNodeIndex ( ) ;
204241 var nodeKey = nodesData [ nodeIndex ] . First ;
205- var node = Session . Demand ( ) . Query . Single < TreeNode > ( nodeKey ) ;
206- while ( node . Tree == null )
242+ var node = session . Query . Single < TreeNode > ( nodeKey ) ;
243+ while ( node . Tree == null ) {
207244 node = node . Parent ;
245+ }
246+
208247 treeNodeKeys . AddRange ( node . Children . Flatten ( n => n . Children , null , true ) . Select ( n => n . Key ) ) ;
209248 treeNodeKeys . Add ( node . Key ) ;
210249 node . Tree . Remove ( ) ;
@@ -216,14 +255,13 @@ private void RemoveTree()
216255 }
217256
218257 // TODO: It's very slow. Probably it should be optimized.
219- foreach ( var key in treeNodeKeys )
258+ foreach ( var key in treeNodeKeys ) {
220259 nodesData . RemoveAt ( FindNodeIndex ( key ) ) ;
260+ }
221261 }
222262
223- private void UpdateChildrenCount ( Key parentNodeKey , bool increment )
224- {
263+ private void UpdateChildrenCount ( Key parentNodeKey , bool increment ) =>
225264 UpdateChildrenCount ( parentNodeKey , increment ? 1 : - 1 ) ;
226- }
227265
228266 private void UpdateChildrenCount ( Key parentNodeKey , int increment )
229267 {
@@ -236,36 +274,40 @@ private int FindNodeIndex(Key key)
236274 {
237275 for ( var i = 0 ; i < nodesData . Count ; i ++ ) {
238276 var pair = nodesData [ i ] ;
239- if ( pair . First == key )
277+ if ( pair . First == key ) {
240278 return i ;
279+ }
241280 }
242281 throw new Exception ( ) ;
243282 }
244283
245- private Action GetAction ( )
284+ private Action < Session > GetAction ( )
246285 {
247286 var index = randomProvider . Next ( 0 , actions . Count ) ;
248287 /*if(!isSettingUp)
249288 Console.WriteLine(actions[index].Method.Name);*/
250289 return actions [ index ] ;
251290 }
252291
253- private int GetNodeIndex ( )
254- {
255- return randomProvider . Next ( 0 , nodesData . Count ) ;
256- }
292+ private int GetNodeIndex ( ) => randomProvider . Next ( 0 , nodesData . Count ) ;
257293
258294 private void ThrowOrCompleteTransaction ( TransactionScope tx )
259295 {
260- if ( isSettingUp )
296+ if ( isSettingUp ) {
261297 return ;
262- if ( randomProvider . Next ( 0 , 2 ) == 1 )
298+ }
299+ if ( randomProvider . Next ( 0 , 2 ) == 1 ) {
263300 throw new InvalidOperationException ( ) ;
301+ }
264302 tx . Complete ( ) ;
265303 }
266304
267- private static int GetSeed ( )
305+ private static int GetSeed ( bool useConst )
268306 {
307+ if ( useConst ) {
308+ return ConstSeed ;
309+ }
310+
269311 var bytes = new byte [ sizeof ( int ) ] ;
270312
271313 using var seedProvider = RandomNumberGenerator . Create ( ) ;
0 commit comments