Skip to content

Commit 2be3980

Browse files
Tests | Fix event counter test intermittent failure (#1358)
1 parent 6cb4ca2 commit 2be3980

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

src/Microsoft.Data.SqlClient/tests/ManualTests/TracingTests/EventCounterTest.cs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,24 @@ public void EventCounter_ReclaimedConnectionsCounter_Functional()
154154

155155
long rc = SqlClientEventSourceProps.ReclaimedConnections;
156156

157-
InternalConnectionWrapper internalConnection = CreateEmancipatedConnection(stringBuilder.ToString());
158-
159-
GC.Collect();
157+
int gcNumber = GC.GetGeneration(CreateEmancipatedConnection(stringBuilder.ToString()));
158+
// Specifying the generation number makes it to run faster by avoiding a full GC process
159+
GC.Collect(gcNumber);
160160
GC.WaitForPendingFinalizers();
161161

162162
using (SqlConnection conn = new SqlConnection(stringBuilder.ToString()))
163163
{
164164
conn.Open();
165165

166-
// when calling open, the connection is reclaimed.
167-
Assert.Equal(rc + 1, SqlClientEventSourceProps.ReclaimedConnections);
166+
// when calling open, the connection could be reclaimed.
167+
if (GC.GetGeneration(conn) == gcNumber)
168+
{
169+
Assert.Equal(rc + 1, SqlClientEventSourceProps.ReclaimedConnections);
170+
}
171+
else
172+
{
173+
Assert.Equal(rc, SqlClientEventSourceProps.ReclaimedConnections);
174+
}
168175
}
169176
}
170177

@@ -256,7 +263,7 @@ private static FieldInfo GetConnectionFactoryField()
256263

257264
internal static class SqlClientEventSourceProps
258265
{
259-
private static readonly object _log;
266+
private static readonly object s_log;
260267
private static readonly FieldInfo _activeHardConnectionsCounter;
261268
private static readonly FieldInfo _hardConnectsCounter;
262269
private static readonly FieldInfo _hardDisconnectsCounter;
@@ -276,14 +283,14 @@ internal static class SqlClientEventSourceProps
276283

277284
static SqlClientEventSourceProps()
278285
{
279-
var sqlClientEventSourceType =
286+
Type sqlClientEventSourceType =
280287
Assembly.GetAssembly(typeof(SqlConnection))!.GetType("Microsoft.Data.SqlClient.SqlClientEventSource");
281288
Debug.Assert(sqlClientEventSourceType != null);
282-
var logField = sqlClientEventSourceType.GetField("Log", BindingFlags.Static | BindingFlags.NonPublic);
289+
FieldInfo logField = sqlClientEventSourceType.GetField("Log", BindingFlags.Static | BindingFlags.NonPublic);
283290
Debug.Assert(logField != null);
284-
_log = logField.GetValue(null);
291+
s_log = logField.GetValue(null);
285292

286-
var _bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
293+
BindingFlags _bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
287294
_activeHardConnectionsCounter =
288295
sqlClientEventSourceType.GetField(nameof(_activeHardConnectionsCounter), _bindingFlags);
289296
Debug.Assert(_activeHardConnectionsCounter != null);
@@ -334,36 +341,36 @@ static SqlClientEventSourceProps()
334341
Debug.Assert(_reclaimedConnectionsCounter != null);
335342
}
336343

337-
public static long ActiveHardConnections => (long)_activeHardConnectionsCounter.GetValue(_log)!;
344+
public static long ActiveHardConnections => (long)_activeHardConnectionsCounter.GetValue(s_log)!;
338345

339-
public static long HardConnects => (long)_hardConnectsCounter.GetValue(_log)!;
346+
public static long HardConnects => (long)_hardConnectsCounter.GetValue(s_log)!;
340347

341-
public static long HardDisconnects => (long)_hardDisconnectsCounter.GetValue(_log)!;
348+
public static long HardDisconnects => (long)_hardDisconnectsCounter.GetValue(s_log)!;
342349

343-
public static long ActiveSoftConnections => (long)_activeSoftConnectionsCounter.GetValue(_log)!;
350+
public static long ActiveSoftConnections => (long)_activeSoftConnectionsCounter.GetValue(s_log)!;
344351

345-
public static long SoftConnects => (long)_softConnectsCounter.GetValue(_log)!;
352+
public static long SoftConnects => (long)_softConnectsCounter.GetValue(s_log)!;
346353

347-
public static long SoftDisconnects => (long)_softDisconnectsCounter.GetValue(_log)!;
354+
public static long SoftDisconnects => (long)_softDisconnectsCounter.GetValue(s_log)!;
348355

349-
public static long NonPooledConnections => (long)_nonPooledConnectionsCounter.GetValue(_log)!;
356+
public static long NonPooledConnections => (long)_nonPooledConnectionsCounter.GetValue(s_log)!;
350357

351-
public static long PooledConnections => (long)_pooledConnectionsCounter.GetValue(_log)!;
358+
public static long PooledConnections => (long)_pooledConnectionsCounter.GetValue(s_log)!;
352359

353-
public static long ActiveConnectionPoolGroups => (long)_activeConnectionPoolGroupsCounter.GetValue(_log)!;
360+
public static long ActiveConnectionPoolGroups => (long)_activeConnectionPoolGroupsCounter.GetValue(s_log)!;
354361

355-
public static long InactiveConnectionPoolGroups => (long)_inactiveConnectionPoolGroupsCounter.GetValue(_log)!;
362+
public static long InactiveConnectionPoolGroups => (long)_inactiveConnectionPoolGroupsCounter.GetValue(s_log)!;
356363

357-
public static long ActiveConnectionPools => (long)_activeConnectionPoolsCounter.GetValue(_log)!;
364+
public static long ActiveConnectionPools => (long)_activeConnectionPoolsCounter.GetValue(s_log)!;
358365

359-
public static long InactiveConnectionPools => (long)_inactiveConnectionPoolsCounter.GetValue(_log)!;
366+
public static long InactiveConnectionPools => (long)_inactiveConnectionPoolsCounter.GetValue(s_log)!;
360367

361-
public static long ActiveConnections => (long)_activeConnectionsCounter.GetValue(_log)!;
368+
public static long ActiveConnections => (long)_activeConnectionsCounter.GetValue(s_log)!;
362369

363-
public static long FreeConnections => (long)_freeConnectionsCounter.GetValue(_log)!;
370+
public static long FreeConnections => (long)_freeConnectionsCounter.GetValue(s_log)!;
364371

365-
public static long StasisConnections => (long)_stasisConnectionsCounter.GetValue(_log)!;
372+
public static long StasisConnections => (long)_stasisConnectionsCounter.GetValue(s_log)!;
366373

367-
public static long ReclaimedConnections => (long)_reclaimedConnectionsCounter.GetValue(_log)!;
374+
public static long ReclaimedConnections => (long)_reclaimedConnectionsCounter.GetValue(s_log)!;
368375
}
369376
}

0 commit comments

Comments
 (0)