@@ -18,11 +18,26 @@ namespace Xtensive.Orm.Tests
1818 public abstract class AutoBuildTest : HasConfigurationAccessTest
1919 {
2020 private const string ErrorInTestFixtureSetup = "Error in TestFixtureSetUp:\r \n {0}" ;
21+ private const string ErrorNotInitializedGlobalSession = "Set InitGlobalSession to true" ;
22+
2123 private DisposableSet disposables ;
24+ private ( Session session , TransactionScope transaction ) globalSessionAndTransaction ;
25+
26+ /// <summary>
27+ /// If set to <see langword="true"/>, global session and transaction will be opened
28+ /// to use one session accross all test methods.
29+ /// </summary>
30+ protected virtual bool InitGlobalSession => false ;
2231
2332 protected ProviderInfo ProviderInfo { get ; set ; }
2433 protected Domain Domain { get ; set ; }
2534
35+ // Use these two for read-only tests only, don't change them, they are controlled by AutoBuildTest.
36+ // If there is need to change Session/Transactionscope or add/modify/remove entities
37+ // then open dedicated Session/TransactionScope within test
38+ protected Session GlobalSession => InitGlobalSession ? globalSessionAndTransaction . session : throw new Exception ( ErrorNotInitializedGlobalSession ) ;
39+ protected TransactionScope GlobalTransaction => InitGlobalSession ? globalSessionAndTransaction . transaction : throw new Exception ( ErrorNotInitializedGlobalSession ) ;
40+
2641 [ OneTimeSetUp ]
2742 public virtual void TestFixtureSetUp ( )
2843 {
@@ -58,10 +73,16 @@ protected void RebuildDomain()
5873
5974 var config = BuildConfiguration ( ) ;
6075 Domain = BuildDomain ( config ) ;
61- PopulateData ( ) ;
62-
63- if ( Domain != null )
76+ if ( Domain != null ) {
6477 ProviderInfo = Domain . StorageProviderInfo ;
78+ if ( InitGlobalSession ) {
79+ globalSessionAndTransaction = CreateSessionAndTransaction ( ) ;
80+ }
81+ }
82+ else {
83+ ProviderInfo = StorageProviderInfo . Instance . Info ;
84+ }
85+ PopulateData ( ) ;
6586 }
6687
6788 protected virtual void PopulateData ( )
@@ -74,17 +95,21 @@ protected virtual void CheckRequirements()
7495
7596 protected ( Session , TransactionScope ) CreateSessionAndTransaction ( )
7697 {
98+ var initDisposable = disposables is null ;
7799 try {
78- disposables = new DisposableSet ( ) ;
100+ if ( initDisposable )
101+ disposables = new DisposableSet ( ) ;
79102 var session = Domain . OpenSession ( ) ;
80103 var transaction = session . OpenTransaction ( ) ;
81104 _ = disposables . Add ( session ) ;
82105 _ = disposables . Add ( transaction ) ;
83106 return ( session , transaction ) ;
84107 }
85108 catch {
86- disposables . DisposeSafely ( ) ;
87- disposables = null ;
109+ if ( initDisposable ) {
110+ disposables . DisposeSafely ( ) ;
111+ disposables = null ;
112+ }
88113 throw ;
89114 }
90115 }
0 commit comments