Code improvement
This commit is contained in:
parent
5329c54f28
commit
2892e93476
@ -14,6 +14,9 @@ import okhttp3.OkHttpClient;
|
|||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
//@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
//@WebAppConfiguration
|
||||||
|
//@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/api-servlet.xml")
|
||||||
public class OkHttpGetLiveTest {
|
public class OkHttpGetLiveTest {
|
||||||
|
|
||||||
private static final String BASE_URL = "http://localhost:8080/spring-rest";
|
private static final String BASE_URL = "http://localhost:8080/spring-rest";
|
||||||
|
@ -37,7 +37,7 @@ public class OkHttpMiscLiveTest {
|
|||||||
response.close();
|
response.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = IOException.class)
|
||||||
public void whenCancelRequest_thenCorrect() throws IOException {
|
public void whenCancelRequest_thenCorrect() throws IOException {
|
||||||
|
|
||||||
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
|
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
|
||||||
@ -49,30 +49,22 @@ public class OkHttpMiscLiveTest {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
final int seconds = 1;
|
final int seconds = 1;
|
||||||
|
|
||||||
final long startNanos = System.nanoTime();
|
final long startNanos = System.nanoTime();
|
||||||
|
|
||||||
final Call call = client.newCall(request);
|
final Call call = client.newCall(request);
|
||||||
|
|
||||||
// Schedule a job to cancel the call in 1 second.
|
// Schedule a job to cancel the call in 1 second.
|
||||||
executor.schedule(new Runnable() {
|
executor.schedule(() -> {
|
||||||
public void run() {
|
|
||||||
|
logger.debug("Canceling call: " + (System.nanoTime() - startNanos) / 1e9f);
|
||||||
|
call.cancel();
|
||||||
|
logger.debug("Canceled call: " + (System.nanoTime() - startNanos) / 1e9f);
|
||||||
|
|
||||||
logger.debug("Canceling call: " + (System.nanoTime() - startNanos) / 1e9f);
|
|
||||||
call.cancel();
|
|
||||||
logger.debug("Canceled call: " + (System.nanoTime() - startNanos) / 1e9f);
|
|
||||||
}
|
|
||||||
}, seconds, TimeUnit.SECONDS);
|
}, seconds, TimeUnit.SECONDS);
|
||||||
|
|
||||||
try {
|
logger.debug("Executing call: " + (System.nanoTime() - startNanos) / 1e9f);
|
||||||
|
Response response = call.execute();
|
||||||
logger.debug("Executing call: " + (System.nanoTime() - startNanos) / 1e9f);
|
logger.debug("Call completed: " + (System.nanoTime() - startNanos) / 1e9f, response);
|
||||||
Response response = call.execute();
|
|
||||||
logger.debug("Call was expected to fail, but completed: " + (System.nanoTime() - startNanos) / 1e9f, response);
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
|
|
||||||
logger.debug("Call failed as expected: " + (System.nanoTime() - startNanos) / 1e9f, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user