public static void Test(string srcConstr, string dstConstr, string dstTable) { #if DEBUG string initialQueryTemplate = "create table {0} (col1 int, col2 nvarchar(20), col3 nvarchar(10), col4 varchar(8000))"; string sourceQuery = "select EmployeeID, LastName, FirstName, REPLICATE('a', 8000) from employees"; string initialQuery = string.Format(initialQueryTemplate, dstTable); using (SqlConnection dstConn = new SqlConnection(dstConstr)) using (SqlCommand dstCmd = dstConn.CreateCommand()) { dstConn.Open(); try { Helpers.TryExecute(dstCmd, initialQuery); using (SqlConnection srcConn = new SqlConnection(srcConstr)) using (SqlCommand srcCmd = new SqlCommand(sourceQuery, srcConn)) { srcConn.Open(); using (DbDataReader reader = srcCmd.ExecuteReader()) { using (SqlBulkCopy bulkcopy = new SqlBulkCopy(dstConn)) { bulkcopy.DestinationTableName = dstTable; // Close the bulk copy's connection when it notifies us bulkcopy.NotifyAfter = 1; bulkcopy.SqlRowsCopied += (sender, e) => { dstConn.Close(); }; using (AsyncDebugScope debugScope = new AsyncDebugScope()) { // Force all writes to pend, this will guarantee that we will go through the correct code path debugScope.ForceAsyncWriteDelay = 1; // Check that the copying fails string message = string.Format(SystemDataResourceManager.Instance.ADP_OpenConnectionRequired, "WriteToServer", SystemDataResourceManager.Instance.ADP_ConnectionStateMsg_Closed); DataTestUtility.AssertThrowsWrapper <AggregateException, InvalidOperationException>(() => bulkcopy.WriteToServerAsync(reader).Wait(5000), innerExceptionMessage: message); } } } } } finally { Helpers.TryDropTable(dstConstr, dstTable); } } #endif }
internal static void Run(string connection) { s_connStr = connection; s_useSP = false; Console.WriteLine("Starting Test using AsyncDebugScope"); #if DEBUG do { Console.WriteLine("Using stored procedure {0}", s_useSP); bool oldTypes = false; do { using (AsyncDebugScope debugScope = new AsyncDebugScope()) { for (int run = 0; run < 2; run++) { bool sync = (run == 0); if (run == 2) { debugScope.ForceAsyncWriteDelay = 1; } TestStream(100000, sync, oldTypes, -1); TestStream(10000, sync, oldTypes, 9000); TestStream(10000, sync, oldTypes, 1000); TestStream(500, sync, oldTypes, 9000); TestStream(500, sync, oldTypes, 1000); TestStream(0, sync, oldTypes, -1); TestStream(0, sync, oldTypes, 9000); TestStream(0, sync, oldTypes, 1000); TestStream(10000, sync, oldTypes, 0); TestCustomStream(10000, sync, oldTypes, -1, error: false); TestCustomStream(10000, sync, oldTypes, 9000, error: false); TestCustomStream(10000, sync, oldTypes, 1000, error: false); TestCustomStream(10000, sync, oldTypes, 0, error: false); TestCustomStream(10000, sync, oldTypes, -1, error: true); bool nvarchar = false; do { TestTextReader(100000, sync, oldTypes, -1, nvarchar); TestTextReader(10000, sync, oldTypes, 9000, nvarchar); TestTextReader(10000, sync, oldTypes, 1000, nvarchar); TestTextReader(500, sync, oldTypes, 9000, nvarchar); TestTextReader(500, sync, oldTypes, 1000, nvarchar); TestTextReader(0, sync, oldTypes, -1, nvarchar); TestTextReader(0, sync, oldTypes, 9000, nvarchar); TestTextReader(0, sync, oldTypes, 1000, nvarchar); TestTextReader(10000, sync, oldTypes, 0, nvarchar); TestXml2Text(sync, oldTypes, -1, nvarchar); TestXml2Text(sync, oldTypes, 1000000, nvarchar); TestXml2Text(sync, oldTypes, 9000, nvarchar); TestXml2Text(sync, oldTypes, 1000, nvarchar); TestXml2Text(sync, oldTypes, 0, nvarchar); TestCustomTextReader(10000, sync, oldTypes, -1, nvarchar, error: false); TestCustomTextReader(10000, sync, oldTypes, 9000, nvarchar, error: false); TestCustomTextReader(10000, sync, oldTypes, 1000, nvarchar, error: false); TestCustomTextReader(10000, sync, oldTypes, 0, nvarchar, error: false); TestCustomTextReader(10000, sync, oldTypes, -1, nvarchar, error: true); nvarchar = !nvarchar; } while (nvarchar); if (!oldTypes) { TestXML(sync, true); TestXML(sync, false); TestStream(100000, sync, oldTypes, -1, addWithValue: true); TestStream(0, sync, oldTypes, -1, addWithValue: true); TestCustomStream(10000, sync, oldTypes, -1, error: false, addWithValue: true); TestCustomStream(10000, sync, oldTypes, -1, error: true, addWithValue: true); TestTextReader(100000, sync, oldTypes, -1, nvarchar: true, addWithValue: true); TestTextReader(0, sync, oldTypes, -1, nvarchar: true, addWithValue: true); TestCustomTextReader(10000, sync, oldTypes, -1, nvarchar: true, error: false, addWithValue: true); TestCustomTextReader(10000, sync, oldTypes, -1, nvarchar: true, error: true, addWithValue: true); TestXML(sync, lengthLimited: false, addWithValue: true); } } } oldTypes = !oldTypes; } while (oldTypes); s_useSP = !s_useSP; } while (s_useSP); #else Console.WriteLine("Tests using AsyncDebugScope are only supported in Debug mode!"); #endif Console.WriteLine("Finished Test using AsyncDebugScope"); ImmediateCancelBin(); ImmediateCancelText(); ImmediateCancelXml(); PrepareCommand(); CommandReuse(); }