提交 5609867a 编写于 作者: J Jesse Wilson

Merge pull request #1313 from square/jw/call-is-executed

Add isExecuted method to Call.
......@@ -119,6 +119,10 @@ final class BehaviorCall<T> implements Call<T> {
});
}
@Override public synchronized boolean isExecuted() {
return executed;
}
@Override public Response<T> execute() throws IOException {
final AtomicReference<Response<T>> responseRef = new AtomicReference<>();
final AtomicReference<Throwable> failureRef = new AtomicReference<>();
......
......@@ -36,6 +36,10 @@ public final class Calls {
callback.onResponse(response);
}
@Override public boolean isExecuted() {
return false;
}
@Override public void cancel() {
}
......@@ -60,6 +64,10 @@ public final class Calls {
callback.onFailure(failure);
}
@Override public boolean isExecuted() {
return false;
}
@Override public void cancel() {
}
......
......@@ -46,6 +46,12 @@ public interface Call<T> extends Cloneable {
*/
void enqueue(Callback<T> callback);
/**
* Returns true if this call has been either {@linkplain #execute() executed} or {@linkplain
* #enqueue(Callback) enqueued}. It is an error to execute or enqueue a call more than once.
*/
boolean isExecuted();
/**
* Cancel this call. An attempt will be made to cancel in-flight calls, and if the call has not
* yet been executed it never will be.
......
......@@ -78,6 +78,10 @@ final class ExecutorCallAdapterFactory implements CallAdapter.Factory {
});
}
@Override public boolean isExecuted() {
return delegate.isExecuted();
}
@Override public Response<T> execute() throws IOException {
return delegate.execute();
}
......
......@@ -102,6 +102,10 @@ final class OkHttpCall<T> implements Call<T> {
});
}
@Override public synchronized boolean isExecuted() {
return executed;
}
public Response<T> execute() throws IOException {
synchronized (this) {
if (executed) throw new IllegalStateException("Already executed");
......
......@@ -515,6 +515,41 @@ public final class CallTest {
assertThat(rawBody.contentType().toString()).isEqualTo("text/stringy");
}
@Test public void reportsExecutedSync() throws IOException {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(new ToStringConverterFactory())
.build();
Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse().setBody("Hi"));
Call<String> call = example.getString();
assertThat(call.isExecuted()).isFalse();
call.execute();
assertThat(call.isExecuted()).isTrue();
}
@Test public void reportsExecutedAsync() throws InterruptedException {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(new ToStringConverterFactory())
.build();
Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse().setBody("Hi"));
Call<String> call = example.getString();
assertThat(call.isExecuted()).isFalse();
call.enqueue(new Callback<String>() {
@Override public void onResponse(Response<String> response) {}
@Override public void onFailure(Throwable t) {}
});
assertThat(call.isExecuted()).isTrue();
}
@Test public void cancelBeforeExecute() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
......
......@@ -151,6 +151,10 @@ public final class ExecutorCallAdapterFactoryTest {
throw new UnsupportedOperationException();
}
@Override public boolean isExecuted() {
return false;
}
@Override public Response<String> execute() throws IOException {
throw new UnsupportedOperationException();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册