aboutsummaryrefslogtreecommitdiffstats
path: root/tests/netx/unit/net/sourceforge/jnlp/AsyncCallTest.java
blob: eb6903b13cfd843c56781778fd539dc7468b8ef5 (plain)
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
package net.sourceforge.jnlp;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.concurrent.Callable;

import org.junit.Test;

public class AsyncCallTest {

    @Test
    public void timeOutTest() {
        final boolean[] wasInterrupted = { false };

        AsyncCall<Void> call = AsyncCall.startWithTimeOut(new Callable<Void>() {
            @Override
            public synchronized Void call() {
                try {
                    wait();
                } catch (InterruptedException ie) {
                    // Received on time-out
                    wasInterrupted[0] = true;
                }
                return null;
            }
        }, 100 /* 100 millisecond time-out */);

        boolean completedNormally = false;

        try {
            call.join();
            completedNormally = true;
        } catch (Exception e) {
            ServerAccess.logErrorReprint(e.toString());
            assertTrue(e instanceof AsyncCall.TimeOutException);
        }

        assertFalse(completedNormally);
        assertTrue(wasInterrupted[0]);
    }

    @Test
    public void normalReturnTest() {
        AsyncCall<Integer> call = AsyncCall.startWithTimeOut(new Callable<Integer>() {
            @Override
            public Integer call() {
                return 1;
            }
        });

        Integer result = null;
        boolean completedNormally = false;

        try {
            result = call.join();
            completedNormally = true;
        } catch (Exception e) {
            ServerAccess.logErrorReprint(e.toString());
        }

        assertTrue(completedNormally);
        assertEquals(Integer.valueOf(1), result);
    }

    @Test
    public void thrownExceptionTest() {

        @SuppressWarnings("serial")
        class TestException extends RuntimeException {
        }

        AsyncCall<Void> call = AsyncCall.startWithTimeOut(new Callable<Void>() {
            @Override
            public Void call() {
                throw new TestException();
            }
        });

        boolean completedNormally = false;

        try {
            call.join();
            completedNormally = true;
        } catch (Exception e) {
            ServerAccess.logErrorReprint(e.toString());
            assertTrue(e instanceof TestException);
        }

        assertFalse(completedNormally);
    }
}