Skip to content

Commit 3a3181a

Browse files
Fix flaky SequentialAccess test with dynamic expected value (#4224)
1 parent 822dca4 commit 3a3181a

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/DataStreamTest/DataStreamTest.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -961,13 +961,23 @@ private static void SequentialAccess(string connectionString)
961961
long cb = 0;
962962
long di = 0;
963963
long cbTotal = 0;
964+
long expectedTotal = 0;
964965
object o;
965966
int i;
966967
SqlBinary sqlbin;
967968

968969
using (SqlConnection conn = new SqlConnection(connectionString))
969970
{
970971
conn.Open();
972+
// Compute the expected total bytes independently of the C# APIs under test in order to compare.
973+
//The inner SELECT generates the XML, CAST converts it to a Unicode string, LEN() * 2 matches s.Length * 2 in C# since UTF-16 uses 2 bytes per character.
974+
// SUM() totals across all rows, matching the loop below.
975+
using (SqlCommand cmdExpected = new SqlCommand("SELECT SUM(LEN(CAST(xml_data AS NVARCHAR(MAX))) * 2) FROM " +
976+
"(SELECT CAST((SELECT * FROM orders FOR XML AUTO) AS NVARCHAR(MAX)) AS xml_data) t", conn))
977+
{
978+
expectedTotal = (long)cmdExpected.ExecuteScalar();
979+
}
980+
971981
using (SqlCommand cmd = new SqlCommand("select * from orders for xml auto", conn))
972982
{
973983
// Simple reads
@@ -982,7 +992,7 @@ private static void SequentialAccess(string connectionString)
982992
}
983993
} while (reader.NextResult());
984994
}
985-
DataTestUtility.AssertEqualsWithDescription((long)536198, cbTotal, "FAILED: cbTotal result did not have expected value");
995+
DataTestUtility.AssertEqualsWithDescription(expectedTotal, cbTotal, "FAILED: cbTotal result did not have expected value");
986996

987997
// Simple GetFieldValue<T>
988998
cbTotal = 0;
@@ -997,7 +1007,7 @@ private static void SequentialAccess(string connectionString)
9971007
}
9981008
} while (reader.NextResult());
9991009
}
1000-
DataTestUtility.AssertEqualsWithDescription((long)536198, cbTotal, "FAILED: cbTotal result did not have expected value");
1010+
DataTestUtility.AssertEqualsWithDescription(expectedTotal, cbTotal, "FAILED: cbTotal result did not have expected value");
10011011

10021012
// Simple GetFieldValueAsync<T>
10031013
cbTotal = 0;
@@ -1012,7 +1022,7 @@ private static void SequentialAccess(string connectionString)
10121022
}
10131023
} while (reader.NextResult());
10141024
}
1015-
DataTestUtility.AssertEqualsWithDescription((long)536198, cbTotal, "FAILED: cbTotal result did not have expected value");
1025+
DataTestUtility.AssertEqualsWithDescription(expectedTotal, cbTotal, "FAILED: cbTotal result did not have expected value");
10161026

10171027
// test sequential access reading everything
10181028
cbTotal = 0;
@@ -1036,7 +1046,7 @@ private static void SequentialAccess(string connectionString)
10361046
}
10371047
} while (reader.NextResult());
10381048
}
1039-
DataTestUtility.AssertEqualsWithDescription((long)536198, cbTotal, "FAILED: cbTotal result did not have expected value");
1049+
DataTestUtility.AssertEqualsWithDescription(expectedTotal, cbTotal, "FAILED: cbTotal result did not have expected value");
10401050
}
10411051

10421052
// Test IsDBNull

0 commit comments

Comments
 (0)