|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | + |
| 3 | +namespace SqlServerSimulator; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Per-session state on <see cref="SimulatedDbConnection"/> must not leak |
| 7 | +/// between connections that share a <see cref="Simulation"/>. Before |
| 8 | +/// per-connection scoping these all lived on <see cref="Simulation"/> and |
| 9 | +/// each test below would have failed in the cross-connection direction. |
| 10 | +/// </summary> |
| 11 | +[TestClass] |
| 12 | +public sealed class ConnectionIsolationTests |
| 13 | +{ |
| 14 | + [TestMethod] |
| 15 | + public void ScopeIdentity_DoesNotLeakBetweenConnections() |
| 16 | + { |
| 17 | + var simulation = new Simulation(); |
| 18 | + _ = simulation.ExecuteNonQuery("create table t (id int identity(1,1), x int)"); |
| 19 | + |
| 20 | + using var connA = simulation.CreateOpenConnection(); |
| 21 | + using var connB = simulation.CreateOpenConnection(); |
| 22 | + |
| 23 | + _ = connA.CreateCommand("insert t (x) values (42)").ExecuteNonQuery(); |
| 24 | + |
| 25 | + AreEqual(1m, connA.CreateCommand("select SCOPE_IDENTITY()").ExecuteScalar()); |
| 26 | + AreEqual(DBNull.Value, connB.CreateCommand("select SCOPE_IDENTITY()").ExecuteScalar()); |
| 27 | + } |
| 28 | + |
| 29 | + [TestMethod] |
| 30 | + public void RowCount_DoesNotLeakBetweenConnections() |
| 31 | + { |
| 32 | + var simulation = new Simulation(); |
| 33 | + _ = simulation.ExecuteNonQuery("create table t (id int)"); |
| 34 | + |
| 35 | + using var connA = simulation.CreateOpenConnection(); |
| 36 | + using var connB = simulation.CreateOpenConnection(); |
| 37 | + |
| 38 | + _ = connA.CreateCommand("insert t values (1),(2),(3)").ExecuteNonQuery(); |
| 39 | + |
| 40 | + // Connection B never ran a row-producing statement, so its @@ROWCOUNT |
| 41 | + // is still 0 — A's INSERT writing 3 doesn't bleed across. |
| 42 | + AreEqual(0, connB.CreateCommand("select @@ROWCOUNT").ExecuteScalar()); |
| 43 | + } |
| 44 | + |
| 45 | + [TestMethod] |
| 46 | + public void IdentityInsert_DoesNotLeakBetweenConnections() |
| 47 | + { |
| 48 | + var simulation = new Simulation(); |
| 49 | + _ = simulation.ExecuteNonQuery("create table t (id int identity(1,1), x int)"); |
| 50 | + |
| 51 | + using var connA = simulation.CreateOpenConnection(); |
| 52 | + using var connB = simulation.CreateOpenConnection(); |
| 53 | + |
| 54 | + _ = connA.CreateCommand("set identity_insert t on").ExecuteNonQuery(); |
| 55 | + |
| 56 | + // SET IDENTITY_INSERT scopes to A. B still sees the column as a |
| 57 | + // generated identity and rejects an explicit value with Msg 544. |
| 58 | + var ex = Throws<System.Data.Common.DbException>( |
| 59 | + () => connB.CreateCommand("insert t (id, x) values (99, 1)").ExecuteNonQuery()); |
| 60 | + AreEqual("544", ex.Data["HelpLink.EvtID"]); |
| 61 | + } |
| 62 | + |
| 63 | + [TestMethod] |
| 64 | + public void TimeDefault_ParsedOnOneConnection_RunsOnDispatchingConnection() |
| 65 | + { |
| 66 | + // Regression coverage for the parse-once-run-many trap: a column |
| 67 | + // default's getutcdate() is parsed once at CREATE TABLE time on whichever |
| 68 | + // connection issued the CREATE, but every later INSERT can run on a |
| 69 | + // different connection. The default must read the *dispatching* |
| 70 | + // connection's per-statement freeze, not the long-frozen value left on |
| 71 | + // the connection that parsed it. Capturing the connection at parse time |
| 72 | + // would freeze both INSERTs at the CREATE-TABLE timestamp. |
| 73 | + var simulation = new Simulation(); |
| 74 | + using var connA = simulation.CreateOpenConnection(); |
| 75 | + using var connB = simulation.CreateOpenConnection(); |
| 76 | + |
| 77 | + _ = connA.CreateCommand( |
| 78 | + "create table t (id int identity, note nvarchar(10), stamp datetime2(7) default getutcdate())") |
| 79 | + .ExecuteNonQuery(); |
| 80 | + |
| 81 | + _ = connB.CreateCommand("insert t (note) values ('a')").ExecuteNonQuery(); |
| 82 | + Thread.Sleep(10); |
| 83 | + _ = connB.CreateCommand("insert t (note) values ('b')").ExecuteNonQuery(); |
| 84 | + |
| 85 | + using var reader = connB.CreateCommand("select stamp from t order by id").ExecuteReader(); |
| 86 | + IsTrue(reader.Read()); |
| 87 | + var first = reader.GetDateTime(0); |
| 88 | + IsTrue(reader.Read()); |
| 89 | + var second = reader.GetDateTime(0); |
| 90 | + IsGreaterThan(first, second); |
| 91 | + } |
| 92 | +} |
0 commit comments