File: LoginTimeoutTest.java

package info (click to toggle)
libpgjava 9.1-901-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,884 kB
  • sloc: java: 34,831; xml: 3,204; makefile: 16; sh: 10
file content (155 lines) | stat: -rw-r--r-- 5,073 bytes parent folder | download
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*-------------------------------------------------------------------------
*
* Copyright (c) 2005-2011, PostgreSQL Global Development Group
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/LoginTimeoutTest.java,v 1.4 2011/08/02 13:50:29 davecramer Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc2;

import org.postgresql.test.TestUtil;
import junit.framework.TestCase;
import java.sql.*;

import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.io.IOException;
import java.util.Properties;

public class LoginTimeoutTest extends TestCase
{
    public LoginTimeoutTest(String name)
    {
        super(name);
    }

    public void setUp() throws Exception {
        TestUtil.initDriver(); // Set up log levels, etc.
    }

    public void testIntTimeout() throws Exception {
        Properties props = new Properties();
        props.setProperty("user", TestUtil.getUser());
        props.setProperty("password", TestUtil.getPassword());
        props.setProperty("loginTimeout", "10");

        Connection conn = java.sql.DriverManager.getConnection(TestUtil.getURL(), props);
        conn.close();
    }

    public void testFloatTimeout() throws Exception {
        Properties props = new Properties();
        props.setProperty("user", TestUtil.getUser());
        props.setProperty("password", TestUtil.getPassword());
        props.setProperty("loginTimeout", "10.0");

        Connection conn = java.sql.DriverManager.getConnection(TestUtil.getURL(), props);
        conn.close();
    }

    public void testZeroTimeout() throws Exception {
        Properties props = new Properties();
        props.setProperty("user", TestUtil.getUser());
        props.setProperty("password", TestUtil.getPassword());
        props.setProperty("loginTimeout", "0");

        Connection conn = java.sql.DriverManager.getConnection(TestUtil.getURL(), props);
        conn.close();
    }

    public void testNegativeTimeout() throws Exception {
        Properties props = new Properties();
        props.setProperty("user", TestUtil.getUser());
        props.setProperty("password", TestUtil.getPassword());
        props.setProperty("loginTimeout", "-1");

        Connection conn = java.sql.DriverManager.getConnection(TestUtil.getURL(), props);
        conn.close();
    }

    public void testBadTimeout() throws Exception {
        Properties props = new Properties();
        props.setProperty("user", TestUtil.getUser());
        props.setProperty("password", TestUtil.getPassword());
        props.setProperty("loginTimeout", "zzzz");

        Connection conn = java.sql.DriverManager.getConnection(TestUtil.getURL(), props);
        conn.close();
    }

    private static class TimeoutHelper implements Runnable {
        TimeoutHelper() throws IOException {
            this.listenSocket = new ServerSocket(0, 1, InetAddress.getLocalHost());
        }

        String getHost() {
            return listenSocket.getInetAddress().getHostAddress();
        }

        int getPort() {
            return listenSocket.getLocalPort();
        }

        public void run() {
            try {
                Socket newSocket = listenSocket.accept();
                try {
                    Thread.sleep(30000);
                } catch (InterruptedException e) {
                    // Ignore it.
                }
                newSocket.close();
            } catch (IOException e) {
                // Ignore it.
            }
        }

        void kill() {
            try {
                listenSocket.close();
            } catch (IOException e) {}
        }

        private final ServerSocket listenSocket;
    }


    public void testTimeoutOccurs() throws Exception {
        // Spawn a helper thread to accept a connection and do nothing with it;
        // this should trigger a timeout.
        TimeoutHelper helper = new TimeoutHelper();
        new Thread(helper, "timeout listen helper").start();
        
        try {
            String url = "jdbc:postgresql://" + helper.getHost() + ":" + helper.getPort() + "/dummy";
            Properties props = new Properties();
            props.setProperty("user", "dummy");
            props.setProperty("loginTimeout", "5");
            
            // This is a pretty crude check, but should help distinguish
            // "can't connect" from "timed out".
            long startTime = System.currentTimeMillis();
            Connection conn = null;
            try {
                conn = java.sql.DriverManager.getConnection(url, props);
                fail("connection was unexpectedly successful");
            } catch (SQLException e) {
                // Ignored.
            } finally {
                if (conn != null)
                    conn.close();
            }
            
            long endTime = System.currentTimeMillis();
            assertTrue(endTime > startTime + 2500);
        } finally {
            helper.kill();
        }
    }
}