From 0a80335ba722cc19c1b0ff3007fa982a778a1e0d Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 29 Jan 2016 15:17:56 +0100 Subject: [PATCH] Use lower socket timeouts for tests --- .../k9/mail/store/imap/ImapConnection.java | 21 +++++++++++++++---- .../mail/store/imap/ImapConnectionTest.java | 18 +++++++++++----- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapConnection.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapConnection.java index ada1a1f53..bc0399233 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapConnection.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapConnection.java @@ -74,6 +74,8 @@ class ImapConnection { private ImapSettings mSettings; private ConnectivityManager mConnectivityManager; private final TrustedSocketFactory mSocketFactory; + private final int socketConnectTimeout; + private final int socketReadTimeout; public ImapConnection(ImapSettings settings, TrustedSocketFactory socketFactory, @@ -81,6 +83,17 @@ class ImapConnection { this.mSettings = settings; this.mSocketFactory = socketFactory; this.mConnectivityManager = connectivityManager; + this.socketConnectTimeout = SOCKET_CONNECT_TIMEOUT; + this.socketReadTimeout = SOCKET_READ_TIMEOUT; + } + + ImapConnection(ImapSettings settings, TrustedSocketFactory socketFactory, ConnectivityManager connectivityManager, + int socketConnectTimeout, int socketReadTimeout) { + this.mSettings = settings; + this.mSocketFactory = socketFactory; + this.mConnectivityManager = connectivityManager; + this.socketConnectTimeout = socketConnectTimeout; + this.socketReadTimeout = socketReadTimeout; } public Set getCapabilities() { @@ -106,7 +119,7 @@ class ImapConnection { try { mSocket = connect(mSettings, mSocketFactory); - setReadTimeout(SOCKET_READ_TIMEOUT); + setReadTimeout(socketReadTimeout); mIn = new PeekableInputStream(new BufferedInputStream(mSocket.getInputStream(), BUFFER_SIZE)); mParser = new ImapResponseParser(mIn); @@ -547,7 +560,7 @@ class ImapConnection { mSettings.getPort(), mSettings.getClientCertificateAlias()); - mSocket.setSoTimeout(SOCKET_READ_TIMEOUT); + mSocket.setSoTimeout(socketReadTimeout); mIn = new PeekableInputStream(new BufferedInputStream(mSocket.getInputStream(), BUFFER_SIZE)); mParser = new ImapResponseParser(mIn); mOut = new BufferedOutputStream(mSocket.getOutputStream(), BUFFER_SIZE); @@ -562,7 +575,7 @@ class ImapConnection { } } - private static Socket connect(ImapSettings settings, TrustedSocketFactory socketFactory) + private Socket connect(ImapSettings settings, TrustedSocketFactory socketFactory) throws GeneralSecurityException, MessagingException, IOException { // Try all IPv4 and IPv6 addresses of the host Exception connectException = null; @@ -583,7 +596,7 @@ class ImapConnection { } else { socket = new Socket(); } - socket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); + socket.connect(socketAddress, socketConnectTimeout); // Successfully connected to the server; don't try any other addresses return socket; } catch (IOException e) { diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapConnectionTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapConnectionTest.java index 51601525d..2254f5a50 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapConnectionTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapConnectionTest.java @@ -53,6 +53,8 @@ public class ImapConnectionTest { private static final String USERNAME = "user"; private static final String PASSWORD = "123456"; + private static final int SOCKET_CONNECT_TIMEOUT = 2000; + private static final int SOCKET_READ_TIMEOUT = 1000; private TrustedSocketFactory socketFactory; @@ -328,7 +330,7 @@ public class ImapConnectionTest { public void open_withConnectionError_shouldThrow() throws Exception { settings.setHost("127.1.2.3"); settings.setPort(143); - ImapConnection imapConnection = new ImapConnection(settings, socketFactory, connectivityManager); + ImapConnection imapConnection = createImapConnection(settings, socketFactory, connectivityManager); try { imapConnection.open(); @@ -345,7 +347,7 @@ public class ImapConnectionTest { public void open_withInvalidHostname_shouldThrow() throws Exception { settings.setHost("host name"); settings.setPort(143); - ImapConnection imapConnection = new ImapConnection(settings, socketFactory, connectivityManager); + ImapConnection imapConnection = createImapConnection(settings, socketFactory, connectivityManager); try { imapConnection.open(); @@ -499,7 +501,7 @@ public class ImapConnectionTest { @Test public void isOpen_withoutPreviousOpen_shouldReturnFalse() throws Exception { - ImapConnection imapConnection = new ImapConnection(settings, socketFactory, connectivityManager); + ImapConnection imapConnection = createImapConnection(settings, socketFactory, connectivityManager); boolean result = imapConnection.isOpen(); @@ -535,7 +537,7 @@ public class ImapConnectionTest { @Test public void close_withoutOpen_shouldNotThrow() throws Exception { - ImapConnection imapConnection = new ImapConnection(settings, socketFactory, connectivityManager); + ImapConnection imapConnection = createImapConnection(settings, socketFactory, connectivityManager); imapConnection.close(); } @@ -607,11 +609,17 @@ public class ImapConnectionTest { server.verifyInteractionCompleted(); } + private ImapConnection createImapConnection(ImapSettings settings, TrustedSocketFactory socketFactory, + ConnectivityManager connectivityManager) { + return new ImapConnection(settings, socketFactory, connectivityManager, SOCKET_CONNECT_TIMEOUT, + SOCKET_READ_TIMEOUT); + } + private ImapConnection startServerAndCreateImapConnection(MockImapServer server) throws IOException { server.start(); settings.setHost(server.getHost()); settings.setPort(server.getPort()); - return new ImapConnection(settings, socketFactory, connectivityManager); + return createImapConnection(settings, socketFactory, connectivityManager); } private ImapConnection simpleOpen(MockImapServer server) throws Exception {