@@ -43,14 +43,13 @@ public partial class DebugSessionFactory : ISessionFactoryImplementor
43
43
/// it debug or not.
44
44
/// </summary>
45
45
public DebugConnectionProvider DebugConnectionProvider
46
- => _debugConnectionProvider ??
47
- ( _debugConnectionProvider = ActualFactory . ConnectionProvider as DebugConnectionProvider ) ;
46
+ => _debugConnectionProvider ??= ActualFactory . ConnectionProvider as DebugConnectionProvider ;
48
47
public ISessionFactoryImplementor ActualFactory { get ; }
49
48
50
49
public EventListeners EventListeners => ( ( SessionFactoryImpl ) ActualFactory ) . EventListeners ;
51
50
52
51
[ NonSerialized ]
53
- private readonly ConcurrentBag < ISessionImplementor > _openedSessions = new ConcurrentBag < ISessionImplementor > ( ) ;
52
+ private readonly ConcurrentQueue < ISessionImplementor > _openedSessions = new ( ) ;
54
53
private static readonly ILog _log = LogManager . GetLogger ( typeof ( DebugSessionFactory ) . Assembly , typeof ( TestCase ) ) ;
55
54
56
55
public DebugSessionFactory ( ISessionFactory actualFactory )
@@ -63,29 +62,43 @@ public DebugSessionFactory(ISessionFactory actualFactory)
63
62
public bool CheckSessionsWereClosed ( )
64
63
{
65
64
var allClosed = true ;
65
+ var number = 1 ;
66
66
foreach ( var session in _openedSessions )
67
67
{
68
- // Do not inverse, we want to close all of them.
69
- allClosed = CheckSessionWasClosed ( session ) && allClosed ;
68
+ var wasClosed = CheckSessionWasClosed ( session ) ;
69
+ // No early exit out of the loop: we want to close all forgotten sessions.
70
+ if ( ! wasClosed )
71
+ {
72
+ _log . ErrorFormat ( "Test case didn't close session {0}, n°{1} of {2}, closing." ,
73
+ session . SessionId , number , _openedSessions . Count ) ;
74
+ }
75
+ allClosed = wasClosed && allClosed ;
76
+
70
77
// Catches only session opened from another one while sharing the connection. Those
71
78
// opened without sharing the connection stay un-monitored.
72
79
foreach ( var dependentSession in session . ConnectionManager . DependentSessions . ToList ( ) )
73
80
{
74
- allClosed = CheckSessionWasClosed ( dependentSession ) && allClosed ;
81
+ wasClosed = CheckSessionWasClosed ( dependentSession ) ;
82
+ if ( ! wasClosed )
83
+ {
84
+ _log . ErrorFormat ( "Test case didn't close dependent session {0} of the session {3}, n°{1} of {2}, closing." ,
85
+ dependentSession . SessionId , number , _openedSessions . Count , session . SessionId ) ;
86
+ }
87
+ allClosed = wasClosed && allClosed ;
75
88
}
89
+ number ++ ;
76
90
}
77
91
78
92
return allClosed ;
79
93
}
80
94
81
- private bool CheckSessionWasClosed ( ISessionImplementor session )
95
+ private static bool CheckSessionWasClosed ( ISessionImplementor session )
82
96
{
83
97
session . TransactionContext ? . Wait ( ) ;
84
98
85
99
if ( ! session . IsOpen )
86
100
return true ;
87
101
88
- _log . Error ( $ "Test case didn't close session { session . SessionId } , closing") ;
89
102
( session as ISession ) ? . Close ( ) ;
90
103
( session as IStatelessSession ) ? . Close ( ) ;
91
104
return false ;
@@ -101,7 +114,7 @@ ISession ISessionFactory.OpenSession(DbConnection connection)
101
114
#pragma warning disable CS0618 // Type or member is obsolete
102
115
var s = ActualFactory . OpenSession ( connection ) ;
103
116
#pragma warning restore CS0618 // Type or member is obsolete
104
- _openedSessions . Add ( s . GetSessionImplementation ( ) ) ;
117
+ _openedSessions . Enqueue ( s . GetSessionImplementation ( ) ) ;
105
118
return s ;
106
119
}
107
120
@@ -110,7 +123,7 @@ ISession ISessionFactory.OpenSession(IInterceptor sessionLocalInterceptor)
110
123
#pragma warning disable CS0618 // Type or member is obsolete
111
124
var s = ActualFactory . OpenSession ( sessionLocalInterceptor ) ;
112
125
#pragma warning restore CS0618 // Type or member is obsolete
113
- _openedSessions . Add ( s . GetSessionImplementation ( ) ) ;
126
+ _openedSessions . Enqueue ( s . GetSessionImplementation ( ) ) ;
114
127
return s ;
115
128
}
116
129
@@ -119,14 +132,14 @@ ISession ISessionFactory.OpenSession(DbConnection conn, IInterceptor sessionLoca
119
132
#pragma warning disable CS0618 // Type or member is obsolete
120
133
var s = ActualFactory . OpenSession ( conn , sessionLocalInterceptor ) ;
121
134
#pragma warning restore CS0618 // Type or member is obsolete
122
- _openedSessions . Add ( s . GetSessionImplementation ( ) ) ;
135
+ _openedSessions . Enqueue ( s . GetSessionImplementation ( ) ) ;
123
136
return s ;
124
137
}
125
138
126
139
ISession ISessionFactory . OpenSession ( )
127
140
{
128
141
var s = ActualFactory . OpenSession ( ) ;
129
- _openedSessions . Add ( s . GetSessionImplementation ( ) ) ;
142
+ _openedSessions . Enqueue ( s . GetSessionImplementation ( ) ) ;
130
143
return s ;
131
144
}
132
145
@@ -138,14 +151,14 @@ IStatelessSessionBuilder ISessionFactory.WithStatelessOptions()
138
151
IStatelessSession ISessionFactory . OpenStatelessSession ( )
139
152
{
140
153
var s = ActualFactory . OpenStatelessSession ( ) ;
141
- _openedSessions . Add ( s . GetSessionImplementation ( ) ) ;
154
+ _openedSessions . Enqueue ( s . GetSessionImplementation ( ) ) ;
142
155
return s ;
143
156
}
144
157
145
158
IStatelessSession ISessionFactory . OpenStatelessSession ( DbConnection connection )
146
159
{
147
160
var s = ActualFactory . OpenStatelessSession ( connection ) ;
148
- _openedSessions . Add ( s . GetSessionImplementation ( ) ) ;
161
+ _openedSessions . Enqueue ( s . GetSessionImplementation ( ) ) ;
149
162
return s ;
150
163
}
151
164
@@ -158,7 +171,7 @@ ISession ISessionFactoryImplementor.OpenSession(
158
171
#pragma warning disable CS0618 // Type or member is obsolete
159
172
var s = ActualFactory . OpenSession ( connection , flushBeforeCompletionEnabled , autoCloseSessionEnabled , connectionReleaseMode ) ;
160
173
#pragma warning restore CS0618 // Type or member is obsolete
161
- _openedSessions . Add ( s . GetSessionImplementation ( ) ) ;
174
+ _openedSessions . Enqueue ( s . GetSessionImplementation ( ) ) ;
162
175
return s ;
163
176
}
164
177
@@ -429,7 +442,7 @@ public SessionBuilder(ISessionBuilder actualBuilder, DebugSessionFactory debugFa
429
442
ISession ISessionBuilder < ISessionBuilder > . OpenSession ( )
430
443
{
431
444
var s = _actualBuilder . OpenSession ( ) ;
432
- _debugFactory . _openedSessions . Add ( s . GetSessionImplementation ( ) ) ;
445
+ _debugFactory . _openedSessions . Enqueue ( s . GetSessionImplementation ( ) ) ;
433
446
return s ;
434
447
}
435
448
@@ -504,7 +517,7 @@ public StatelessSessionBuilder(IStatelessSessionBuilder actualBuilder, DebugSess
504
517
IStatelessSession IStatelessSessionBuilder . OpenStatelessSession ( )
505
518
{
506
519
var s = _actualBuilder . OpenStatelessSession ( ) ;
507
- _debugFactory . _openedSessions . Add ( s . GetSessionImplementation ( ) ) ;
520
+ _debugFactory . _openedSessions . Enqueue ( s . GetSessionImplementation ( ) ) ;
508
521
return s ;
509
522
}
510
523
0 commit comments