forked from microsoft/mssql-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimeoutTest.java
134 lines (113 loc) · 4.87 KB
/
TimeoutTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Microsoft JDBC Driver for SQL Server
*
* Copyright(c) 2016 Microsoft Corporation All rights reserved.
*
* This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/
package com.microsoft.sqlserver.jdbc.connection;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import com.microsoft.sqlserver.jdbc.SQLServerConnection;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.util.RandomUtil;
@RunWith(JUnitPlatform.class)
public class TimeoutTest extends AbstractTest {
String randomServer = RandomUtil.getIdentifier("Server");
String waitForDelaySPName = "waitForDelaySP";
final int waitForDelaySeconds = 10;
@Test
public void testDefaultLoginTimeout() {
long timerStart = 0;
long timerEnd = 0;
try {
timerStart = System.currentTimeMillis();
// Try a non existing server and see if the default timeout is 15 seconds
DriverManager.getConnection("jdbc:sqlserver://" + randomServer + ";user=sa;password=pwd;");
} catch (Exception e) {
assertTrue(e.getMessage().contains("The TCP/IP connection to the host"));
timerEnd = System.currentTimeMillis();
}
assertTrue(0 != timerEnd, "Should not have connected.");
long timeDiff = timerEnd - timerStart;
assertTrue(timeDiff > 14000);
}
@Test
public void testFailoverInstanceResolution() throws SQLServerException {
long timerStart = 0;
long timerEnd = 0;
try {
timerStart = System.currentTimeMillis();
// Try a non existing server and see if the default timeout is 15 seconds
DriverManager.getConnection("jdbc:sqlserver://" + randomServer + ";databaseName=FailoverDB_abc;failoverPartner=" + randomServer + "\\foo;user=sa;password=pwd;");
} catch (Exception e) {
assertTrue(e.getMessage().contains("The TCP/IP connection to the host"));
timerEnd = System.currentTimeMillis();
}
assertTrue(0 != timerEnd, "Should not have connected.");
long timeDiff = timerEnd - timerStart;
assertTrue(timeDiff > 14000);
}
@Test
public void testFOInstanceResolution2() throws SQLServerException {
long timerStart = 0;
long timerEnd = 0;
try {
timerStart = System.currentTimeMillis();
// Try a non existing server and see if the default timeout is 15 secs at least
DriverManager.getConnection("jdbc:sqlserver://" + randomServer + "\\fooggg;databaseName=FailoverDB;failoverPartner=" + randomServer + "\\foo;user=sa;password=pwd;");
} catch (Exception e) {
timerEnd = System.currentTimeMillis();
}
assertTrue(0 != timerEnd, "Should not have connected.");
long timeDiff = timerEnd - timerStart;
assertTrue(timeDiff > 14000);
}
@Test
public void testQueryTimeout() throws Exception {
SQLServerConnection conn = (SQLServerConnection) DriverManager.getConnection(connectionString);
dropWaitForDelayProcedure(conn);
createWaitForDelayPreocedure(conn);
conn = (SQLServerConnection) DriverManager.getConnection(connectionString + ";queryTimeout=" + (waitForDelaySeconds / 2) + ";");
try {
conn.createStatement().execute("exec " + waitForDelaySPName);
throw new Exception("Exception for queryTimeout is not thrown.");
} catch (Exception e) {
if (!(e instanceof SQLServerException)) {
throw e;
}
assertEquals(e.getMessage(), "The query has timed out.", "Invalid exception message");
}
}
@Test
public void testSocketTimeout() throws Exception {
SQLServerConnection conn = (SQLServerConnection) DriverManager.getConnection(connectionString);
dropWaitForDelayProcedure(conn);
createWaitForDelayPreocedure(conn);
conn = (SQLServerConnection) DriverManager.getConnection(connectionString + ";socketTimeout=" + (waitForDelaySeconds * 1000 / 2) + ";");
try {
conn.createStatement().execute("exec " + waitForDelaySPName);
throw new Exception("Exception for socketTimeout is not thrown.");
} catch (Exception e) {
if (!(e instanceof SQLServerException)) {
throw e;
}
assertEquals(e.getMessage(), "Read timed out", "Invalid exception message");
}
}
private void dropWaitForDelayProcedure(SQLServerConnection conn) throws SQLException {
String sql = " IF EXISTS (select * from sysobjects where id = object_id(N'" + waitForDelaySPName + "') and OBJECTPROPERTY(id, N'IsProcedure') = 1)" + " DROP PROCEDURE "
+ waitForDelaySPName;
conn.createStatement().execute(sql);
}
private void createWaitForDelayPreocedure(SQLServerConnection conn) throws SQLException {
String sql = "CREATE PROCEDURE " + waitForDelaySPName + " AS" + " BEGIN" + " WAITFOR DELAY '00:00:" + waitForDelaySeconds + "';" + " END";
conn.createStatement().execute(sql);
}
}