blob: 9cc453faef1e4e8cf681b91093c129c4cdb88f62 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.handler;
import com.google.common.util.concurrent.MoreExecutors;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.test.NonWorkingContentChannel;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
/**
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*/
public class FutureResponseTestCase {
@Test
public void requireThatCancelIsUnsupported() {
FutureResponse future = new FutureResponse();
assertFalse(future.isCancelled());
try {
future.cancel(true);
fail();
} catch (UnsupportedOperationException e) {
}
assertFalse(future.isCancelled());
try {
future.cancel(false);
fail();
} catch (UnsupportedOperationException e) {
}
assertFalse(future.isCancelled());
}
@Test
public void requireThatCompletionIsDoneWhenHandlerIsCalled() {
FutureResponse future = new FutureResponse();
assertFalse(future.isDone());
future.handleResponse(new Response(69));
assertTrue(future.isDone());
}
@Test
public void requireThatResponseBecomesAvailable() throws Exception {
FutureResponse future = new FutureResponse();
try {
future.get(0, TimeUnit.MILLISECONDS);
fail();
} catch (TimeoutException e) {
}
Response response = new Response(Response.Status.OK);
future.handleResponse(response);
assertSame(response, future.get(0, TimeUnit.MILLISECONDS));
}
@Test
public void requireThatResponseContentIsReturnedToCaller() throws Exception {
ContentChannel content = new NonWorkingContentChannel();
FutureResponse future = new FutureResponse(content);
Response response = new Response(Response.Status.OK);
assertSame(content, future.handleResponse(response));
}
@Test
public void requireThatResponseCanBeListenedTo() throws InterruptedException {
FutureResponse response = new FutureResponse();
RunnableLatch listener = new RunnableLatch();
response.addListener(listener, MoreExecutors.directExecutor());
assertFalse(listener.await(100, TimeUnit.MILLISECONDS));
response.handleResponse(new Response(Response.Status.OK));
assertTrue(listener.await(600, TimeUnit.SECONDS));
}
}
|