Skip to content

Commit

Permalink
Merge pull request square#1290 from square/jw/start
Browse files Browse the repository at this point in the history
Rename play to start.
  • Loading branch information
swankjesse committed Jan 5, 2015
2 parents a5e13bd + 5355762 commit f84c2c0
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private MockWebServer startServer() throws IOException {
}
});

server.play();
server.start();
return server;
}

Expand Down
2 changes: 1 addition & 1 deletion mockwebserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void test() throws Exception {
server.enqueue(new MockResponse().setBody("yo dog"));

// Start the server.
server.play();
server.start();

// Ask the server for its URL. You'll need this to make HTTP requests.
URL baseUrl = server.getUrl("/v1/chat/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,19 @@ public void setServerSocketFactory(ServerSocketFactory serverSocketFactory) {
}

public int getPort() {
if (port == -1) throw new IllegalStateException("Call play() before getPort()");
if (port == -1) throw new IllegalStateException("Call start() before getPort()");
return port;
}

public String getHostName() {
if (inetAddress == null) throw new IllegalStateException("Call play() before getHostName()");
if (inetAddress == null) throw new IllegalStateException("Call start() before getHostName()");
return inetAddress.getHostName();
}

public Proxy toProxyAddress() {
if (inetAddress == null) throw new IllegalStateException("Call play() before toProxyAddress()");
if (inetAddress == null) {
throw new IllegalStateException("Call start() before toProxyAddress()");
}
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(inetAddress, getPort()));
}

Expand Down Expand Up @@ -258,20 +260,30 @@ public void enqueue(MockResponse response) {
((QueueDispatcher) dispatcher).enqueueResponse(response.clone());
}

/** Equivalent to {@code play(0)}. */
/** @deprecated Use {@link #start()}. */
public void play() throws IOException {
play(0);
start();
}

/** @deprecated Use {@link #start(int)}. */
public void play(int port) throws IOException {
start(port);
}

/** Equivalent to {@code start(0)}. */
public void start() throws IOException {
start(0);
}

/**
* Starts the server, serves all enqueued requests, and shuts the server down.
* Starts the server.
*
* @param port the port to listen to, or 0 for any available port. Automated
* tests should always use port 0 to avoid flakiness when a specific port
* is unavailable.
*/
public void play(int port) throws IOException {
if (executor != null) throw new IllegalStateException("play() already called");
public void start(int port) throws IOException {
if (executor != null) throw new IllegalStateException("start() already called");
executor = Executors.newCachedThreadPool(Util.threadFactory("MockWebServer", false));
inetAddress = InetAddress.getByName(null);
serverSocket = serverSocketFactory.createServerSocket();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class MockWebServerRule extends ExternalResource {
if (started) return;
started = true;
try {
server.play();
server.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class CustomDispatcherTest {
}

@Test public void simpleDispatch() throws Exception {
mockWebServer.play();
mockWebServer.start();
final List<RecordedRequest> requestsMade = new ArrayList<>();
final Dispatcher dispatcher = new Dispatcher() {
@Override
Expand All @@ -56,7 +56,7 @@ public MockResponse dispatch(RecordedRequest request) throws InterruptedExceptio
@Test public void outOfOrderResponses() throws Exception {
AtomicInteger firstResponseCode = new AtomicInteger();
AtomicInteger secondResponseCode = new AtomicInteger();
mockWebServer.play();
mockWebServer.start();
final String secondRequest = "/bar";
final String firstRequest = "/foo";
final CountDownLatch latch = new CountDownLatch(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class OkApacheClientTest {
@Before public void setUp() throws IOException {
client = new OkApacheClient();
server = new MockWebServer();
server.play();
server.start();
}

@After public void tearDown() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private void assertCached(boolean shouldPut, int responseCode) throws Exception
mockResponse.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
}
server.enqueue(mockResponse);
server.play();
server.start();

Request request = new Request.Builder()
.url(server.getUrl("/"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ private void setUp(int poolSize) throws Exception {
List<ConnectionSpec> connectionSpecs = Util.immutableList(
ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT);

httpServer.play();
httpServer.start();
httpAddress = new Address(httpServer.getHostName(), httpServer.getPort(), socketFactory, null,
null, null, AuthenticatorAdapter.INSTANCE, null,
Util.immutableList(Protocol.SPDY_3, Protocol.HTTP_1_1), connectionSpecs, proxySelector);
httpSocketAddress = new InetSocketAddress(InetAddress.getByName(httpServer.getHostName()),
httpServer.getPort());

spdyServer.play();
spdyServer.start();
spdyAddress = new Address(spdyServer.getHostName(), spdyServer.getPort(), socketFactory,
sslContext.getSocketFactory(), new RecordingHostnameVerifier(), CertificatePinner.DEFAULT,
AuthenticatorAdapter.INSTANCE, null, Util.immutableList(Protocol.SPDY_3, Protocol.HTTP_1_1),
Expand Down Expand Up @@ -558,7 +558,7 @@ private void assertPooled(ConnectionPool pool, Connection... connections) throws
private static class FakeExecutor implements Executor {

private Runnable runnable;

@Override
public void execute(Runnable runnable) {
// This is a bonus assertion for the invariant: At no time should two runnables be scheduled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class SocksProxyTest {
private final MockWebServer server = new MockWebServer();

@Before public void setUp() throws Exception {
server.play();
server.start();
socksProxy.play();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void testNetscapeResponse() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
MockWebServer server = new MockWebServer();
server.play();
server.start();

server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; "
+ "expires=Fri, 31-Dec-9999 23:59:59 GMT; "
Expand Down Expand Up @@ -91,7 +91,7 @@ public void testNetscapeResponse() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
MockWebServer server = new MockWebServer();
server.play();
server.start();

server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; "
+ "Comment=this cookie is delicious; "
Expand Down Expand Up @@ -121,7 +121,7 @@ public void testNetscapeResponse() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
MockWebServer server = new MockWebServer();
server.play();
server.start();

server.enqueue(new MockResponse().addHeader("Set-Cookie2: a=android; "
+ "Comment=this cookie is delicious; "
Expand Down Expand Up @@ -155,7 +155,7 @@ public void testNetscapeResponse() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
MockWebServer server = new MockWebServer();
server.play();
server.start();

server.enqueue(new MockResponse().addHeader("Set-Cookie2: a=\"android\"; "
+ "Comment=\"this cookie is delicious\"; "
Expand Down Expand Up @@ -188,7 +188,7 @@ public void testNetscapeResponse() throws Exception {
@Test public void testSendingCookiesFromStore() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse());
server.play();
server.start();

CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookieA = new HttpCookie("a", "android");
Expand All @@ -213,13 +213,13 @@ public void testNetscapeResponse() throws Exception {
@Test public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
MockWebServer redirectTarget = new MockWebServer();
redirectTarget.enqueue(new MockResponse().setBody("A"));
redirectTarget.play();
redirectTarget.start();

MockWebServer redirectSource = new MockWebServer();
redirectSource.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.addHeader("Location: " + redirectTarget.getUrl("/")));
redirectSource.play();
redirectSource.start();

CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookie = new HttpCookie("c", "cookie");
Expand Down Expand Up @@ -267,7 +267,7 @@ public Map<String, List<String>> get(URI uri,
});
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse());
server.play();
server.start();

HttpURLConnection connection = new OkUrlFactory(client).open(server.getUrl("/"));
assertEquals(Collections.<String, List<String>>emptyMap(),
Expand Down Expand Up @@ -316,7 +316,7 @@ public Map<String, List<String>> get(URI uri,
});
MockWebServer server = new MockWebServer();
server. enqueue(new MockResponse());
server.play();
server.start();

get(server, "/");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void configureSocket(Socket socket) throws IOException {

server.enqueue(new MockResponse()
.throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps
server.play();
server.start();

HttpURLConnection connection = new OkUrlFactory(client).open(server.getUrl("/"));
disconnectLater(connection, 500);
Expand All @@ -100,7 +100,7 @@ protected void configureSocket(Socket socket) throws IOException {
server.enqueue(new MockResponse()
.setBody(new Buffer().write(new byte[responseBodySize]))
.throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps
server.play();
server.start();

HttpURLConnection connection = new OkUrlFactory(client).open(server.getUrl("/"));
disconnectLater(connection, 500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected void configureSocket(Socket socket) throws IOException {

server.enqueue(new MockResponse()
.throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps
server.play();
server.start();

interruptLater(500);

Expand All @@ -102,7 +102,7 @@ protected void configureSocket(Socket socket) throws IOException {
server.enqueue(new MockResponse()
.setBody(new Buffer().write(new byte[responseBodySize]))
.throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps
server.play();
server.start();

interruptLater(500);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private void assertCached(boolean shouldPut, int responseCode) throws Exception
response.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
}
server.enqueue(response);
server.play();
server.start();

URL url = server.getUrl("/");
HttpURLConnection conn = client.open(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ private void executePost(HttpURLConnection connection) throws IOException {

private URL configureServer(MockResponse mockResponse) throws Exception {
server.enqueue(mockResponse);
server.play();
server.start();
return server.getUrl("/");
}

private URL configureHttpsServer(MockResponse mockResponse) throws Exception {
server.useHttps(sslContext.getSocketFactory(), false /* tunnelProxy */);
server.enqueue(mockResponse);
server.play();
server.start();
return server.getUrl("/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void run() throws IOException {
MockWebServer server = new MockWebServer();
server.useHttps(sslContext.getSocketFactory(), false);
server.setDispatcher(this);
server.play(port);
server.start(port);
}

@Override public MockResponse dispatch(RecordedRequest request) {
Expand Down

0 comments on commit f84c2c0

Please sign in to comment.