Skip to content

Commit 9c93f48

Browse files
Add more information on unclosed session
1 parent 2ecb8d4 commit 9c93f48

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/NHibernate.Test/DebugSessionFactory.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@ public partial class DebugSessionFactory : ISessionFactoryImplementor
4343
/// it debug or not.
4444
/// </summary>
4545
public DebugConnectionProvider DebugConnectionProvider
46-
=> _debugConnectionProvider ??
47-
(_debugConnectionProvider = ActualFactory.ConnectionProvider as DebugConnectionProvider);
46+
=> _debugConnectionProvider ??= ActualFactory.ConnectionProvider as DebugConnectionProvider;
4847
public ISessionFactoryImplementor ActualFactory { get; }
4948

5049
public EventListeners EventListeners => ((SessionFactoryImpl)ActualFactory).EventListeners;
5150

5251
[NonSerialized]
53-
private readonly ConcurrentBag<ISessionImplementor> _openedSessions = new ConcurrentBag<ISessionImplementor>();
52+
private readonly ConcurrentQueue<ISessionImplementor> _openedSessions = new();
5453
private static readonly ILog _log = LogManager.GetLogger(typeof(DebugSessionFactory).Assembly, typeof(TestCase));
5554

5655
public DebugSessionFactory(ISessionFactory actualFactory)
@@ -63,29 +62,43 @@ public DebugSessionFactory(ISessionFactory actualFactory)
6362
public bool CheckSessionsWereClosed()
6463
{
6564
var allClosed = true;
65+
var number = 1;
6666
foreach (var session in _openedSessions)
6767
{
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+
7077
// Catches only session opened from another one while sharing the connection. Those
7178
// opened without sharing the connection stay un-monitored.
7279
foreach (var dependentSession in session.ConnectionManager.DependentSessions.ToList())
7380
{
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;
7588
}
89+
number++;
7690
}
7791

7892
return allClosed;
7993
}
8094

81-
private bool CheckSessionWasClosed(ISessionImplementor session)
95+
private static bool CheckSessionWasClosed(ISessionImplementor session)
8296
{
8397
session.TransactionContext?.Wait();
8498

8599
if (!session.IsOpen)
86100
return true;
87101

88-
_log.Error($"Test case didn't close session {session.SessionId}, closing");
89102
(session as ISession)?.Close();
90103
(session as IStatelessSession)?.Close();
91104
return false;
@@ -101,7 +114,7 @@ ISession ISessionFactory.OpenSession(DbConnection connection)
101114
#pragma warning disable CS0618 // Type or member is obsolete
102115
var s = ActualFactory.OpenSession(connection);
103116
#pragma warning restore CS0618 // Type or member is obsolete
104-
_openedSessions.Add(s.GetSessionImplementation());
117+
_openedSessions.Enqueue(s.GetSessionImplementation());
105118
return s;
106119
}
107120

@@ -110,7 +123,7 @@ ISession ISessionFactory.OpenSession(IInterceptor sessionLocalInterceptor)
110123
#pragma warning disable CS0618 // Type or member is obsolete
111124
var s = ActualFactory.OpenSession(sessionLocalInterceptor);
112125
#pragma warning restore CS0618 // Type or member is obsolete
113-
_openedSessions.Add(s.GetSessionImplementation());
126+
_openedSessions.Enqueue(s.GetSessionImplementation());
114127
return s;
115128
}
116129

@@ -119,14 +132,14 @@ ISession ISessionFactory.OpenSession(DbConnection conn, IInterceptor sessionLoca
119132
#pragma warning disable CS0618 // Type or member is obsolete
120133
var s = ActualFactory.OpenSession(conn, sessionLocalInterceptor);
121134
#pragma warning restore CS0618 // Type or member is obsolete
122-
_openedSessions.Add(s.GetSessionImplementation());
135+
_openedSessions.Enqueue(s.GetSessionImplementation());
123136
return s;
124137
}
125138

126139
ISession ISessionFactory.OpenSession()
127140
{
128141
var s = ActualFactory.OpenSession();
129-
_openedSessions.Add(s.GetSessionImplementation());
142+
_openedSessions.Enqueue(s.GetSessionImplementation());
130143
return s;
131144
}
132145

@@ -138,14 +151,14 @@ IStatelessSessionBuilder ISessionFactory.WithStatelessOptions()
138151
IStatelessSession ISessionFactory.OpenStatelessSession()
139152
{
140153
var s = ActualFactory.OpenStatelessSession();
141-
_openedSessions.Add(s.GetSessionImplementation());
154+
_openedSessions.Enqueue(s.GetSessionImplementation());
142155
return s;
143156
}
144157

145158
IStatelessSession ISessionFactory.OpenStatelessSession(DbConnection connection)
146159
{
147160
var s = ActualFactory.OpenStatelessSession(connection);
148-
_openedSessions.Add(s.GetSessionImplementation());
161+
_openedSessions.Enqueue(s.GetSessionImplementation());
149162
return s;
150163
}
151164

@@ -158,7 +171,7 @@ ISession ISessionFactoryImplementor.OpenSession(
158171
#pragma warning disable CS0618 // Type or member is obsolete
159172
var s = ActualFactory.OpenSession(connection, flushBeforeCompletionEnabled, autoCloseSessionEnabled, connectionReleaseMode);
160173
#pragma warning restore CS0618 // Type or member is obsolete
161-
_openedSessions.Add(s.GetSessionImplementation());
174+
_openedSessions.Enqueue(s.GetSessionImplementation());
162175
return s;
163176
}
164177

@@ -429,7 +442,7 @@ public SessionBuilder(ISessionBuilder actualBuilder, DebugSessionFactory debugFa
429442
ISession ISessionBuilder<ISessionBuilder>.OpenSession()
430443
{
431444
var s = _actualBuilder.OpenSession();
432-
_debugFactory._openedSessions.Add(s.GetSessionImplementation());
445+
_debugFactory._openedSessions.Enqueue(s.GetSessionImplementation());
433446
return s;
434447
}
435448

@@ -504,7 +517,7 @@ public StatelessSessionBuilder(IStatelessSessionBuilder actualBuilder, DebugSess
504517
IStatelessSession IStatelessSessionBuilder.OpenStatelessSession()
505518
{
506519
var s = _actualBuilder.OpenStatelessSession();
507-
_debugFactory._openedSessions.Add(s.GetSessionImplementation());
520+
_debugFactory._openedSessions.Enqueue(s.GetSessionImplementation());
508521
return s;
509522
}
510523

0 commit comments

Comments
 (0)