Use lower socket timeouts for tests

This commit is contained in:
cketti 2016-01-29 15:17:56 +01:00
parent 21204f8bec
commit 0a80335ba7
2 changed files with 30 additions and 9 deletions

View file

@ -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<String> 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) {

View file

@ -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 {