コード例 #1
0
        public async Task ExecutionEngineTest_MultiThreading_WithCancel()
        {
            string[] sqlStatement = { "waitfor delay '0:0:10'",
                                      "waitfor delay '0:0:10'",
                                      "waitfor delay '0:0:10'" };

            ExecutionEngineConditions conditions = new ExecutionEngineConditions();

            conditions.IsTransactionWrapped = true;
            conditions.IsParseOnly          = false;
            conditions.IsHaltOnError        = false;

            SqlConnection connection2 = SetUpConnection("test4");
            SqlConnection connection3 = SetUpConnection("test5");

            TestExecutor executor1 = new TestExecutor(sqlStatement[0], connection, conditions, true);

            executor1.CancelTimeOut = 2000;
            TestExecutor executor2 = new TestExecutor(sqlStatement[1], connection2, conditions, true);

            executor1.CancelTimeOut = 2500;
            TestExecutor executor3 = new TestExecutor(sqlStatement[2], connection3, conditions, true);

            executor1.CancelTimeOut = 3000;

            Thread t1 = new Thread(new ThreadStart(executor1.Run));
            Thread t2 = new Thread(new ThreadStart(executor2.Run));
            Thread t3 = new Thread(new ThreadStart(executor3.Run));

            t1.Name = "Executor1";
            t1.Start();
            t2.Name = "Executor2";
            t2.Start();
            t3.Name = "Executor3";
            t3.Start();

            while ((t1.ThreadState != ThreadState.Stopped) &&
                   (t2.ThreadState != ThreadState.Stopped) &&
                   (t3.ThreadState != ThreadState.Stopped))
            {
                Thread.Sleep(1000);
            }

            Assert.True(!executor1.ScriptExecuteThread.IsAlive);
            Assert.True(!executor2.ScriptExecuteThread.IsAlive);
            Assert.True(!executor3.ScriptExecuteThread.IsAlive);

            Assert.True(executor1.CancelEventFired);
            Assert.True(executor2.CancelEventFired);
            Assert.True(executor3.CancelEventFired);

            CloseConnection(connection2);
            CloseConnection(connection3);
            await SqlTestDb.DropDatabase(connection2.Database);

            await SqlTestDb.DropDatabase(connection3.Database);
        }