@@ -18,11 +18,26 @@ namespace Xtensive.Orm.Tests
18
18
public abstract class AutoBuildTest : HasConfigurationAccessTest
19
19
{
20
20
private const string ErrorInTestFixtureSetup = "Error in TestFixtureSetUp:\r \n {0}" ;
21
+ private const string ErrorNotInitializedGlobalSession = "Set InitGlobalSession to true" ;
22
+
21
23
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 ;
22
31
23
32
protected ProviderInfo ProviderInfo { get ; set ; }
24
33
protected Domain Domain { get ; set ; }
25
34
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
+
26
41
[ OneTimeSetUp ]
27
42
public virtual void TestFixtureSetUp ( )
28
43
{
@@ -58,10 +73,16 @@ protected void RebuildDomain()
58
73
59
74
var config = BuildConfiguration ( ) ;
60
75
Domain = BuildDomain ( config ) ;
61
- PopulateData ( ) ;
62
-
63
- if ( Domain != null )
76
+ if ( Domain != null ) {
64
77
ProviderInfo = Domain . StorageProviderInfo ;
78
+ if ( InitGlobalSession ) {
79
+ globalSessionAndTransaction = CreateSessionAndTransaction ( ) ;
80
+ }
81
+ }
82
+ else {
83
+ ProviderInfo = StorageProviderInfo . Instance . Info ;
84
+ }
85
+ PopulateData ( ) ;
65
86
}
66
87
67
88
protected virtual void PopulateData ( )
@@ -74,17 +95,21 @@ protected virtual void CheckRequirements()
74
95
75
96
protected ( Session , TransactionScope ) CreateSessionAndTransaction ( )
76
97
{
98
+ var initDisposable = disposables is null ;
77
99
try {
78
- disposables = new DisposableSet ( ) ;
100
+ if ( initDisposable )
101
+ disposables = new DisposableSet ( ) ;
79
102
var session = Domain . OpenSession ( ) ;
80
103
var transaction = session . OpenTransaction ( ) ;
81
104
_ = disposables . Add ( session ) ;
82
105
_ = disposables . Add ( transaction ) ;
83
106
return ( session , transaction ) ;
84
107
}
85
108
catch {
86
- disposables . DisposeSafely ( ) ;
87
- disposables = null ;
109
+ if ( initDisposable ) {
110
+ disposables . DisposeSafely ( ) ;
111
+ disposables = null ;
112
+ }
88
113
throw ;
89
114
}
90
115
}
0 commit comments