@@ -0,0 +1,2834 @@
+/*%0A * Microsoft JDBC Driver for SQL Server%0A * %0A * Copyright(c) Microsoft Corporation All rights reserved.%0A * %0A * This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information.%0A */%0Apackage com.microsoft.sqlserver.jdbc.unit.statement;%0A%0Aimport static org.junit.jupiter.api.Assertions.assertTrue;%0A%0Aimport java.sql.Connection;%0Aimport java.sql.SQLException;%0Aimport java.sql.Statement;%0A%0Aimport org.junit.jupiter.api.Test;%0Aimport org.junit.platform.runner.JUnitPlatform;%0Aimport org.junit.runner.RunWith;%0A%0Aimport com.microsoft.sqlserver.jdbc.SQLServerDataSource;%0Aimport com.microsoft.sqlserver.testframework.AbstractTest;%0A%0A/**%0A * Tests batch execution with errors%0A *%0A */%0A@RunWith(JUnitPlatform.class)%0Apublic class StatementCancellationTest extends AbstractTest %7B%0A    private static final long DELAY_WAIT_MILLISECONDS = 10000;%0A    private static final long CANCEL_WAIT_MILLISECONDS = 5000;%0A%0A    @Test%0A    public void setMultiSubnetFailoverToTrue() throws SQLException %7B%0A        long timeStart = 0;%0A        long timeEnd = 0;%0A%0A        try %7B%0A            SQLServerDataSource ds = new SQLServerDataSource();%0A            ds.setURL(connectionString);%0A            ds.setMultiSubnetFailover(true);%0A%0A            try (Connection conn = ds.getConnection();) %7B%0A                try (final Statement stmt = conn.createStatement()) %7B%0A                    final Thread cancellationThread = new Thread() %7B%0A                        public void run() %7B%0A                            try %7B%0A                                Thread.sleep(CANCEL_WAIT_MILLISECONDS);%0A                                stmt.cancel();%0A                            %7D%0A                            catch (Exception e) %7B%0A                                e.printStackTrace();%0A                            %7D%0A                        %7D%0A                    %7D;%0A                    cancellationThread.setName(%22stmt-canceller%22);%0A                    cancellationThread.start();%0A%0A                    try %7B%0A                        timeStart = System.currentTimeMillis();%0A                        stmt.execute(%22WAITFOR DELAY '00:00:%22 + (DELAY_WAIT_MILLISECONDS / 1000) + %22'%22);%0A                    %7D%0A                    catch (SQLException e) %7B%0A                        assertTrue(e.getMessage().startsWith(%22The query was canceled%22), %22Unexpected error message.%22);%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A        finally %7B%0A            timeEnd = System.currentTimeMillis();%0A            long timeDifference = timeEnd - timeStart;%0A            assertTrue(timeDifference %3E= CANCEL_WAIT_MILLISECONDS, %22Cancellation failed.%22);%0A            assertTrue(timeDifference %3C DELAY_WAIT_MILLISECONDS, %22Cancellation failed.%22);%0A            assertTrue((timeDifference - CANCEL_WAIT_MILLISECONDS) %3C 1000, %22Cancellation failed.%22);%0A        %7D%0A    %7D%0A%7D%0A

test statement.cancel() works when MultiSubnetFailover is set to true