Last active
February 10, 2021 19:36
-
-
Save cheenamalhotra/5f4b35603c491484e51be38abf15548a to your computer and use it in GitHub Desktop.
Forced Repro of wrong data issue
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
using System; | |
using System.Data; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.Data.SqlClient; | |
namespace AllManagedSNITests | |
{ | |
class _659_repro | |
{ | |
// Delay Timeout method "OnTimeout" by 10 seconds and run this repro. | |
// Pass or include your connection string here. | |
public static void RunTest(string connString) | |
{ | |
try | |
{ | |
Console.WriteLine("\n*************** Running issue #659 - Forced Repro **************\n"); | |
Console.WriteLine("Connection String : " + connString + "\n"); | |
using var cn = new SqlConnection(connString); | |
cn.Open(); | |
// Query 1: WAITFOR DELAY '00:00:02'; select 1 as Id | |
// Command Timeout = 1; | |
// Expectation : Should Timeout! | |
QueryAndVerify(1, 1, cn, timeoutExpected: true).Wait(); | |
Thread.Sleep(11000); // Wait for Attention to complete before closing connection. | |
cn.Close(); | |
cn.Open(); | |
// Query 2: WAITFOR DELAY '00:00:02'; select 2 as Id | |
// Command Timeout = 10; | |
// Expectation : Should Complete and give result as 2 | |
QueryAndVerify(10, 2, cn).Wait(); | |
cn.Close(); | |
cn.Open(); | |
// Query 1: WAITFOR DELAY '00:00:02'; select 3 as Id | |
// Command Timeout = 10; | |
// Expectation : Should Complete and give result as 3 | |
QueryAndVerify(10, 3, cn).Wait(); | |
cn.Close(); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message + e.StackTrace); | |
} | |
} | |
private static async Task QueryAndVerify(int timeout, int index, SqlConnection cn, bool timeoutExpected = false) | |
{ | |
try | |
{ | |
SqlTransaction tx = cn.BeginTransaction(IsolationLevel.ReadCommitted); | |
var sql = $"WAITFOR DELAY '00:00:02';select {index} as Id;"; // 2 seconds delay followed with select query. | |
var command = new SqlCommand(sql, cn, tx) | |
{ | |
Transaction = tx, | |
CommandTimeout = timeout | |
}; | |
var result = -1; | |
using (SqlDataReader reader = await command.ExecuteReaderAsync().ConfigureAwait(false)) | |
{ | |
while (await reader.ReadAsync().ConfigureAwait(false)) | |
{ | |
var columnIndex = reader.GetOrdinal("Id"); | |
result = reader.GetInt32(columnIndex); | |
break; | |
} | |
} | |
tx.Commit(); | |
Console.WriteLine($"Value: {index} | Timeout Exception Expected: {timeoutExpected} | Result Received {result}"); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine($"Value: {index} | Timeout Exception Expected: {timeoutExpected} | Exception occurred: {e.Message}"); | |
} | |
} | |
} | |
} |
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
using System; | |
using System.Data; | |
using System.Diagnostics; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.Data.SqlClient; | |
namespace AllManagedSNITests | |
{ | |
class _659_repro | |
{ | |
static Stopwatch watch; | |
// Delay Timeout method "OnTimeout" by 5 seconds and run this repro. | |
public static void RunTest(string connString) | |
{ | |
try | |
{ | |
Console.WriteLine("\n*************** Running issue #659 - Forced Repro **************\n"); | |
Console.WriteLine("Connection String : " + connString + "\n"); | |
watch = new Stopwatch(); | |
TestAsync(watch, connString); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message + e.StackTrace); | |
} | |
} | |
//******************************** ASYNC TESTS ************************************* | |
private static void TestAsync(Stopwatch watch, string connString) | |
{ | |
watch.Start(); | |
var cn = new SqlConnection(connString); | |
cn.Open(); | |
QueryAndVerifyAsync(1, 1, cn, timeoutExpected: true).Wait(); | |
Thread.Sleep(6000); // Wait for Attention to complete before closing connection. | |
cn.Close(); | |
cn.Open(); | |
QueryAndVerifyAsync(10, 2, cn).Wait(); | |
cn.Close(); | |
cn.Open(); | |
QueryAndVerifyAsync(10, 3, cn).Wait(); | |
cn.Close(); | |
cn.Open(); | |
QueryAndVerifyAsync(2, 4, cn, timeoutExpected: true).Wait(); | |
// Thread.Sleep(10000); // Wait for Attention to complete before closing connection. | |
// This time attention is never received on server and timeout does not occur. | |
cn.Close(); | |
cn.Open(); | |
QueryAndVerifyAsync(10, 5, cn).Wait(); | |
cn.Close(); | |
cn.Open(); | |
QueryAndVerifyAsync(10, 6, cn).Wait(); | |
cn.Close(); | |
cn.Open(); | |
QueryAndVerifyAsync(10, 7, cn).Wait(); | |
cn.Close(); | |
cn.Open(); | |
QueryAndVerifyAsync(10, 8, cn).Wait(); | |
cn.Close(); | |
cn.Open(); | |
QueryAndVerifyAsync(10, 9, cn).Wait(); | |
cn.Close(); | |
cn.Open(); | |
QueryAndVerifyAsync(10, 10, cn).Wait(); | |
cn.Close(); | |
cn.Open(); | |
QueryAndVerifyAsync(10, 11, cn).Wait(); | |
cn.Close(); | |
cn.Open(); | |
QueryAndVerifyAsync(10, 12, cn).Wait(); | |
cn.Close(); | |
watch.Stop(); | |
} | |
private static async Task QueryAndVerifyAsync(int timeout, int index, SqlConnection cn, bool timeoutExpected = false) | |
{ | |
try | |
{ | |
SqlTransaction tx = cn.BeginTransaction(IsolationLevel.ReadCommitted); | |
var sql = (index == 4) ? $"WAITFOR DELAY '00:00:10';select {index} as Id;" : | |
$"WAITFOR DELAY '00:00:03';select {index} as Id;"; // 3 seconds delay followed with select query. | |
var command = new SqlCommand(sql, cn, tx) | |
{ | |
Transaction = tx, | |
CommandTimeout = timeout | |
}; | |
var result = -1; | |
result = (int)await command.ExecuteScalarAsync().ConfigureAwait(false); | |
//using (SqlDataReader reader = await command.ExecuteReaderAsync().ConfigureAwait(false)) | |
//{ | |
// while (await reader.ReadAsync().ConfigureAwait(false)) | |
// { | |
// var columnIndex = reader.GetOrdinal("Id"); | |
// result = reader.GetInt32(columnIndex); | |
// break; | |
// } | |
//} | |
tx.Commit(); | |
Console.WriteLine($"{(double)watch.ElapsedMilliseconds / 1000} \t| Async | Value: {index} | Timeout Exception Expected: {timeoutExpected} | Result Received {result}"); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine($"{(double)watch.ElapsedMilliseconds / 1000} \t| Async | Value: {index} | Timeout Exception Expected: {timeoutExpected} | Exception occurred: {e.Message}"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment