-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ConnectionTime and ClientConnectionId reported by SqlStatistics (#…
…341) * fix sqlstatistics * add test * remove excess using * remove setting closeTimestamp * rename directory to be case sensitive * add error message to test
- Loading branch information
1 parent
0e97cac
commit 6ade367
Showing
6 changed files
with
84 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlStatisticsTest/SqlStatisticsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Data; | ||
using System.Data.Common; | ||
using System.Collections; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.SqlClient.ManualTesting.Tests | ||
{ | ||
public class SqlStatisticsTest | ||
{ | ||
private static DateTime startTime = new DateTime(); | ||
private static Guid clientConnectionId = Guid.Empty; | ||
|
||
[CheckConnStrSetupFact] | ||
public static void TestRetrieveStatistics() | ||
{ | ||
startTime = DateTime.Now; | ||
SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString); | ||
string text = "SELECT TOP 2000 * from [dbo].[Order Details]"; | ||
using (SqlCommand command = new SqlCommand(text)) | ||
{ | ||
connection.StatisticsEnabled = true; | ||
connection.Open(); | ||
connection.StateChange += new StateChangeEventHandler(HandleStateChange); | ||
|
||
command.Connection = connection; | ||
using (SqlDataReader dr = command.ExecuteReader()) | ||
{ | ||
IDictionary stats1 = connection.RetrieveStatistics(); | ||
|
||
// Ensure ConnectionTime is within a reasonable range | ||
Assert.True((long)stats1["ConnectionTime"] < DateTime.Now.Subtract(startTime).TotalMilliseconds + 1000, "Unexpected ConnectionTime: " + stats1["ConnectionTime"]); | ||
clientConnectionId = connection.ClientConnectionId; | ||
Assert.True(clientConnectionId != Guid.Empty); | ||
|
||
int row = 0; | ||
while (dr.Read()) | ||
{ | ||
row++; | ||
} | ||
} | ||
} | ||
// Ensure calling RetrieveStatistics multiple times do not affect the ConnectionTime | ||
connection.RetrieveStatistics(); | ||
connection.RetrieveStatistics(); | ||
connection.RetrieveStatistics(); | ||
connection.RetrieveStatistics(); | ||
connection.RetrieveStatistics(); | ||
connection.RetrieveStatistics(); | ||
connection.Close(); | ||
IDictionary stats2 = connection.RetrieveStatistics(); | ||
Assert.True((long)stats2["ConnectionTime"] < DateTime.Now.Subtract(startTime).TotalMilliseconds + 1000, "Unexpected ConnectionTime: " + stats2["ConnectionTime"]); | ||
// Ensure ClientConnectionId remains available even after the connection is closed | ||
Assert.True(connection.ClientConnectionId == clientConnectionId); | ||
} | ||
|
||
private static void HandleStateChange(object sender, StateChangeEventArgs args) | ||
{ | ||
if (args.CurrentState == ConnectionState.Closed) | ||
{ | ||
System.Collections.IDictionary stats = ((SqlConnection)sender).RetrieveStatistics(); | ||
Assert.True((long)stats["ConnectionTime"] < DateTime.Now.Subtract(startTime).TotalMilliseconds + 1000, "Unexpected ConnectionTime: " + stats["ConnectionTime"]); | ||
Assert.True(((SqlConnection)sender).ClientConnectionId == clientConnectionId); | ||
} | ||
} | ||
} | ||
} |