package com.stripe.functional;

import com.stripe.BaseStripeTest;
import com.stripe.Stripe;
import com.stripe.exception.APIConnectionException;
import com.stripe.exception.StripeException;
import com.stripe.model.Balance;
import com.stripe.net.RequestOptions;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.ServerSocket;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class TimeoutTest extends BaseStripeTest {
  @Rule
  public ExpectedException thrown = ExpectedException.none();

  @Test
  public void testConnectTimeout() throws IOException, StripeException {
    // Kind of a hack, but we use the non-routable address 10.255.255.0 to trigger a connection
    // timeout
    Stripe.overrideApiBase(String.format("http://10.255.255.0"));

    thrown.expect(APIConnectionException.class);
    thrown.expectMessage("connect timed out");

    final RequestOptions options = RequestOptions.builder().setConnectTimeout(1).build();
    Balance.retrieve(options);
  }

  @Test
  public void testReadTimeout() throws IOException, StripeException {
    // Create a local server that does nothing to trigger a read timeout
    try (final ServerSocket serverSocket =
        new ServerSocket(0, 1, Inet4Address.getByName("localhost"))) {
      Stripe.overrideApiBase(String.format("http://localhost:%d", serverSocket.getLocalPort()));

      thrown.expect(APIConnectionException.class);
      thrown.expectMessage("Read timed out");

      final RequestOptions options = RequestOptions.builder().setReadTimeout(1).build();
      Balance.retrieve(options);
    }
  }
}

@@ -557,490 +557,8 @@
 );%0A%0A
-  @Test%0A  public void testConnectTimeout() throws IOException, StripeException %7B%0A    // Kind of a hack, but we use the non-routable address 10.255.255.0 to trigger a connection%0A    // timeout%0A    Stripe.overrideApiBase(String.format(%22http://10.255.255.0%22));%0A%0A    thrown.expect(APIConnectionException.class);%0A    thrown.expectMessage(%22connect timed out%22);%0A%0A    final RequestOptions options = RequestOptions.builder().setConnectTimeout(1).build();%0A    Balance.retrieve(options);%0A  %7D%0A%0A
   @T

Drop connection timeout test