提交 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> { ...@@ -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 { @Override public Response<T> execute() throws IOException {
final AtomicReference<Response<T>> responseRef = new AtomicReference<>(); final AtomicReference<Response<T>> responseRef = new AtomicReference<>();
final AtomicReference<Throwable> failureRef = new AtomicReference<>(); final AtomicReference<Throwable> failureRef = new AtomicReference<>();
......
...@@ -36,6 +36,10 @@ public final class Calls { ...@@ -36,6 +36,10 @@ public final class Calls {
callback.onResponse(response); callback.onResponse(response);
} }
@Override public boolean isExecuted() {
return false;
}
@Override public void cancel() { @Override public void cancel() {
} }
...@@ -60,6 +64,10 @@ public final class Calls { ...@@ -60,6 +64,10 @@ public final class Calls {
callback.onFailure(failure); callback.onFailure(failure);
} }
@Override public boolean isExecuted() {
return false;
}
@Override public void cancel() { @Override public void cancel() {
} }
......
...@@ -46,6 +46,12 @@ public interface Call<T> extends Cloneable { ...@@ -46,6 +46,12 @@ public interface Call<T> extends Cloneable {
*/ */
void enqueue(Callback<T> callback); 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 * 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. * yet been executed it never will be.
......
...@@ -78,6 +78,10 @@ final class ExecutorCallAdapterFactory implements CallAdapter.Factory { ...@@ -78,6 +78,10 @@ final class ExecutorCallAdapterFactory implements CallAdapter.Factory {
}); });
} }
@Override public boolean isExecuted() {
return delegate.isExecuted();
}
@Override public Response<T> execute() throws IOException { @Override public Response<T> execute() throws IOException {
return delegate.execute(); return delegate.execute();
} }
......
...@@ -102,6 +102,10 @@ final class OkHttpCall<T> implements Call<T> { ...@@ -102,6 +102,10 @@ final class OkHttpCall<T> implements Call<T> {
}); });
} }
@Override public synchronized boolean isExecuted() {
return executed;
}
public Response<T> execute() throws IOException { public Response<T> execute() throws IOException {
synchronized (this) { synchronized (this) {
if (executed) throw new IllegalStateException("Already executed"); if (executed) throw new IllegalStateException("Already executed");
......
...@@ -515,6 +515,41 @@ public final class CallTest { ...@@ -515,6 +515,41 @@ public final class CallTest {
assertThat(rawBody.contentType().toString()).isEqualTo("text/stringy"); 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() { @Test public void cancelBeforeExecute() {
Retrofit retrofit = new Retrofit.Builder() Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/")) .baseUrl(server.url("/"))
......
...@@ -151,6 +151,10 @@ public final class ExecutorCallAdapterFactoryTest { ...@@ -151,6 +151,10 @@ public final class ExecutorCallAdapterFactoryTest {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override public boolean isExecuted() {
return false;
}
@Override public Response<String> execute() throws IOException { @Override public Response<String> execute() throws IOException {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册