Skip to content

Commit

Permalink
Merge pull request square#1289 from square/jw/time-unit
Browse files Browse the repository at this point in the history
Clean up time-based APIs for throttle and delay.
  • Loading branch information
JakeWharton committed Jan 5, 2015
2 parents f84c2c0 + 8f54a48 commit 5f44ca7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ public final class MockResponse implements Cloneable {

private Buffer body;

private int throttleBytesPerPeriod = Integer.MAX_VALUE;
private long throttlePeriod = 1;
private TimeUnit throttleUnit = TimeUnit.SECONDS;
private long throttleBytesPerPeriod = Long.MAX_VALUE;
private long throttlePeriodAmount = 1;
private TimeUnit throttlePeriodUnit = TimeUnit.SECONDS;

private SocketPolicy socketPolicy = SocketPolicy.KEEP_OPEN;

private int bodyDelayTimeMs = 0;
private long bodyDelayAmount = 0;
private TimeUnit bodyDelayUnit = TimeUnit.MILLISECONDS;

private List<PushPromise> promises = new ArrayList<>();
private WebSocketListener webSocketListener;
Expand Down Expand Up @@ -185,36 +186,33 @@ public MockResponse setSocketPolicy(SocketPolicy socketPolicy) {
* series of {@code bytesPerPeriod} bytes are written. Use this to simulate
* network behavior.
*/
public MockResponse throttleBody(int bytesPerPeriod, long period, TimeUnit unit) {
public MockResponse throttleBody(long bytesPerPeriod, long period, TimeUnit unit) {
this.throttleBytesPerPeriod = bytesPerPeriod;
this.throttlePeriod = period;
this.throttleUnit = unit;
this.throttlePeriodAmount = period;
this.throttlePeriodUnit = unit;
return this;
}

public int getThrottleBytesPerPeriod() {
public long getThrottleBytesPerPeriod() {
return throttleBytesPerPeriod;
}

public long getThrottlePeriod() {
return throttlePeriod;
}

public TimeUnit getThrottleUnit() {
return throttleUnit;
public long getThrottlePeriod(TimeUnit unit) {
return unit.convert(throttlePeriodAmount, throttlePeriodUnit);
}

/**
* Set the delayed time of the response body to {@code delay}. This applies to the
* response body only; response headers are not affected.
*/
public MockResponse setBodyDelayTimeMs(int delay) {
bodyDelayTimeMs = delay;
public MockResponse setBodyDelay(long delay, TimeUnit unit) {
bodyDelayAmount = delay;
bodyDelayUnit = unit;
return this;
}

public int getBodyDelayTimeMs() {
return bodyDelayTimeMs;
public long getBodyDelay(TimeUnit unit) {
return unit.convert(bodyDelayAmount, bodyDelayUnit);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,9 +677,10 @@ private void writeHttpResponse(Socket socket, BufferedSink sink, MockResponse re
}

private void sleepIfDelayed(MockResponse response) {
if (response.getBodyDelayTimeMs() != 0) {
long delayMs = response.getBodyDelay(TimeUnit.MILLISECONDS);
if (delayMs != 0) {
try {
Thread.sleep(response.getBodyDelayTimeMs());
Thread.sleep(delayMs);
} catch (InterruptedException e) {
throw new AssertionError(e);
}
Expand All @@ -696,8 +697,8 @@ private void throttledTransfer(MockResponse throttlePolicy, Socket socket, Buffe
if (byteCount == 0) return;

Buffer buffer = new Buffer();
int bytesPerPeriod = throttlePolicy.getThrottleBytesPerPeriod();
long delayMs = throttlePolicy.getThrottleUnit().toMillis(throttlePolicy.getThrottlePeriod());
long bytesPerPeriod = throttlePolicy.getThrottleBytesPerPeriod();
long periodDelayMs = throttlePolicy.getThrottlePeriod(TimeUnit.MILLISECONDS);

while (!socket.isClosed()) {
for (int b = 0; b < bytesPerPeriod; ) {
Expand All @@ -713,9 +714,9 @@ private void throttledTransfer(MockResponse throttlePolicy, Socket socket, Buffe
if (byteCount == 0) return;
}

if (delayMs != 0) {
if (periodDelayMs != 0) {
try {
Thread.sleep(delayMs);
Thread.sleep(periodDelayMs);
} catch (InterruptedException e) {
throw new AssertionError();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.junit.Test;

import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -253,12 +254,12 @@ public final class MockWebServerTest {
}

/**
* Delay the response body by sleeping 1000ms.
* Delay the response body by sleeping 1s.
*/
@Test public void delayResponse() throws IOException {
server.enqueue(new MockResponse()
.setBody("ABCDEF")
.setBodyDelayTimeMs(1000));
.setBodyDelay(1, SECONDS));

long startNanos = System.nanoTime();
URLConnection connection = server.getUrl("/").openConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
Expand Down Expand Up @@ -316,7 +317,7 @@ protected HttpOverSpdyTest(Protocol protocol){

@Test public void spdyConnectionTimeout() throws Exception {
MockResponse response = new MockResponse().setBody("A");
response.setBodyDelayTimeMs(1000);
response.setBodyDelay(1, TimeUnit.SECONDS);
server.enqueue(response);

HttpURLConnection connection1 = client.open(server.getUrl("/"));
Expand Down

0 comments on commit 5f44ca7

Please sign in to comment.