@@ -15,11 +15,11 @@ namespace Xtensive.Orm.Reprocessing.Tests.ReprocessingContext
1515 public class Context : IDisposable
1616 {
1717 private readonly Domain domain ;
18+ private readonly AutoResetEvent wait1 = new AutoResetEvent ( false ) ;
19+ private readonly AutoResetEvent wait2 = new AutoResetEvent ( false ) ;
1820
1921 public int Count ;
20- private AutoResetEvent wait1 = new AutoResetEvent ( false ) ;
21- private AutoResetEvent wait2 = new AutoResetEvent ( false ) ;
22-
22+
2323 public bool Disposed { get ; private set ; }
2424
2525 /// <summary>
@@ -52,6 +52,9 @@ public void Run(
5252
5353 public void Deadlock ( bool first , IsolationLevel ? isolationLevel , TransactionOpenMode ? transactionOpenMode )
5454 {
55+ int firstRunsCount = 0 ;
56+ int secondRunsCount = 0 ;
57+
5558 domain . WithStrategy ( ExecuteActionStrategy . HandleUniqueConstraintViolation )
5659 . WithIsolationLevel ( isolationLevel . GetValueOrDefault ( IsolationLevel . RepeatableRead ) )
5760 . WithTransactionOpenMode ( transactionOpenMode . GetValueOrDefault ( TransactionOpenMode . New ) )
@@ -60,21 +63,19 @@ public void Deadlock(bool first, IsolationLevel? isolationLevel, TransactionOpen
6063 _ = new Bar2 ( session , DateTime . Now , Guid . NewGuid ( ) ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
6164 if ( first ) {
6265 _ = session . Query . All < Foo > ( ) . Lock ( LockMode . Exclusive , LockBehavior . Wait ) . ToArray ( ) ;
63- if ( wait1 != null ) {
66+ if ( firstRunsCount == 0 ) {
6467 _ = wait1 . Set ( ) ;
6568 _ = wait2 . WaitOne ( ) ;
66- wait1 . Dispose ( ) ;
67- wait1 = null ;
69+ _ = Interlocked . Increment ( ref firstRunsCount ) ;
6870 }
6971 _ = session . Query . All < Bar > ( ) . Lock ( LockMode . Exclusive , LockBehavior . Wait ) . ToArray ( ) ;
7072 }
7173 else {
7274 _ = session . Query . All < Bar > ( ) . Lock ( LockMode . Exclusive , LockBehavior . Wait ) . ToArray ( ) ;
73- if ( wait2 != null ) {
75+ if ( secondRunsCount == 0 ) {
7476 _ = wait2 . Set ( ) ;
7577 _ = wait1 . WaitOne ( ) ;
76- wait2 . Dispose ( ) ;
77- wait2 = null ;
78+ _ = Interlocked . Increment ( ref secondRunsCount ) ;
7879 }
7980 _ = session . Query . All < Foo > ( ) . Lock ( LockMode . Exclusive , LockBehavior . Wait ) . ToArray ( ) ;
8081 }
@@ -171,6 +172,9 @@ public void Parent(
171172 public void UniqueConstraintViolation (
172173 bool first , IsolationLevel ? isolationLevel , TransactionOpenMode ? transactionOpenMode )
173174 {
175+ int firstRunsCount = 0 ;
176+ int secondRunsCount = 0 ;
177+
174178 domain . WithStrategy ( ExecuteActionStrategy . HandleUniqueConstraintViolation )
175179 . WithIsolationLevel ( isolationLevel . GetValueOrDefault ( IsolationLevel . RepeatableRead ) )
176180 . WithTransactionOpenMode ( transactionOpenMode . GetValueOrDefault ( TransactionOpenMode . New ) )
@@ -180,21 +184,22 @@ public void UniqueConstraintViolation(
180184 _ = new Bar2 ( session , DateTime . Now , Guid . NewGuid ( ) ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
181185 var name = "test" ;
182186 if ( ! session . Query . All < Foo > ( ) . Any ( a => a . Name == name ) ) {
187+ var w1 = wait1 ;
188+ var w2 = wait2 ;
183189 if ( first ) {
184- if ( wait1 != null && wait2 != null ) {
185- _ = wait1 . Set ( ) ;
186- _ = wait2 . WaitOne ( ) ;
187- wait1 . Dispose ( ) ;
188- wait1 = null ;
190+ if ( w1 != null && w2 != null ) {
191+ _ = w1 . Set ( ) ;
192+ _ = w2 . WaitOne ( ) ;
193+ // wait1.Dispose();
194+ w1 = null ;
189195 }
190196 }
191- else if ( wait2 != null && wait2 != null ) {
192- _ = wait2 . Set ( ) ;
193- _ = wait1 . WaitOne ( ) ;
194- wait2 . Dispose ( ) ;
195- wait2 = null ;
197+ else if ( w2 != null && w2 != null ) {
198+ _ = w2 . Set ( ) ;
199+ _ = w1 . WaitOne ( ) ;
200+ // wait2.Dispose();
201+ w2 = null ;
196202 }
197- _ = new Foo ( session ) { Name = name } ;
198203 }
199204 session . SaveChanges ( ) ;
200205 } ) ;
@@ -218,15 +223,15 @@ public void UniqueConstraintViolationPrimaryKey(
218223 if ( w1 != null && w2 != null ) {
219224 _ = w1 . Set ( ) ;
220225 _ = w2 . WaitOne ( ) ;
221- wait1 . Dispose ( ) ;
222- wait1 = null ;
226+ // wait1.Dispose();
227+ w1 = null ;
223228 }
224229 }
225230 else if ( w1 != null && w2 != null ) {
226231 _ = w2 . Set ( ) ;
227232 _ = w1 . WaitOne ( ) ;
228- wait2 . Dispose ( ) ;
229- wait2 = null ;
233+ // wait2.Dispose();
234+ w2 = null ;
230235 }
231236 _ = new Foo ( session , id ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
232237 }
0 commit comments