aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FutureCompletionTestCase.java
blob: a92b52d711680e57df272678586047daf2a87327 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.handler;

import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * @author Simon Thoresen Hult
 */
public class FutureCompletionTestCase {

    @Test
    void requireThatCancelIsUnsupported() {
        FutureCompletion future = new FutureCompletion();
        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
    void requireThatCompletedReturnsTrue() throws Exception {
        FutureCompletion future = new FutureCompletion();
        try {
            future.get(0, TimeUnit.MILLISECONDS);
            fail();
        } catch (TimeoutException e) {

        }
        future.completed();
        assertTrue(future.get(0, TimeUnit.MILLISECONDS));
        assertTrue(future.get());
    }

    @Test
    void requireThatCompletionIsDoneWhenCompleted() {
        FutureCompletion future = new FutureCompletion();
        assertFalse(future.isDone());
        future.completed();
        assertTrue(future.isDone());
    }

    @Test
    void requireThatCompletionIsDoneWhenFailed() {
        FutureCompletion future = new FutureCompletion();
        assertFalse(future.isDone());
        future.failed(new Throwable());
        assertTrue(future.isDone());
    }

    @Test
    void requireThatFailedCauseIsRethrown() throws Exception {
        FutureCompletion future = new FutureCompletion();
        Throwable t = new Throwable();
        future.failed(t);
        try {
            future.get(0, TimeUnit.SECONDS);
            fail();
        } catch (ExecutionException e) {
            assertSame(t, e.getCause());
        }
        try {
            future.get();
            fail();
        } catch (ExecutionException e) {
            assertSame(t, e.getCause());
        }
    }

    @Test
    void requireThatCompletionCanBeListenedTo() throws InterruptedException {
        FutureCompletion completion = new FutureCompletion();
        RunnableLatch listener = new RunnableLatch();
        completion.addListener(listener, Runnable::run);
        assertFalse(listener.await(100, TimeUnit.MILLISECONDS));
        completion.completed();
        assertTrue(listener.await(600, TimeUnit.SECONDS));

        completion = new FutureCompletion();
        listener = new RunnableLatch();
        completion.addListener(listener, Runnable::run);
        assertFalse(listener.await(100, TimeUnit.MILLISECONDS));
        completion.failed(new Throwable());
        assertTrue(listener.await(600, TimeUnit.SECONDS));
    }
}