summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/core/src/test/java/org/apache/tuscany/sca/core/work/impl/ThreadPoolWorkManagerTestCase.java
blob: 122609d7e6b86c62eb6fb85fd33b1c7ec0752411 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.tuscany.sca.core.work.impl;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * This test case will test the ThreadPoolWorkManager
 *
 * @version $Rev$ $Date$
 */
public class ThreadPoolWorkManagerTestCase {

    /**
     * Wait up to 20 seconds for the Work units to complete
     */
    private static final long WAIT_TIMEOUT = 20000;

    /**
     * This is the shared instance of the ThreadPoolWorkManager used by the tests
     */
    private static ThreadPoolWorkManager workManager = null;

    /**
     * Setup the ThreadPoolWorkManager
     */
    @BeforeClass
    public static void setup() {
        workManager = new ThreadPoolWorkManager(10);
    }

    /**
     * Make sure that the ThreadPoolWorkManager is stopped after running the tests
     */
    @AfterClass
    public static void destroy() {
        if (workManager != null) {
            workManager.destroy();
        }
    }

    /**
     * Tests running a single fast job on the ThreadPoolWorkManager
     */
    @Test
    public void testSingleFastJob() {
        // Create the work and register it
        TimeDelayWork fast = new TimeDelayWork(10);
        TestWorkListener listener = new TestWorkListener();
        workManager.schedule(fast, listener);

        // Wait for the 1 job to complete
        waitForWorkToComplete(listener, 1);

        // Test that the job completed successfully.
        Assert.assertEquals(1, listener.getWorkAcceptedCallCount());
        Assert.assertEquals(0, listener.getWorkRejectedCallCount());
        Assert.assertEquals(1, listener.getWorkStartedCallCount());
        Assert.assertEquals(1, listener.getWorkCompletedCallCount());
        Assert.assertEquals(0, listener.getWorkExceptions().size());
    }

    /**
     * Tests running a single job that fails on the ThreadPoolWorkManager
     */
    @Test
    public void testSingleFailingJob() {
        // Create the work and register it
        FailingWork fail = new FailingWork();
        TestWorkListener listener = new TestWorkListener();
        workManager.schedule(fail, listener);

        // Wait for the 1 job to complete
        waitForWorkToComplete(listener, 1);

        // Test that the job completed successfully.
        Assert.assertEquals(1, listener.getWorkAcceptedCallCount());
        Assert.assertEquals(0, listener.getWorkRejectedCallCount());
        Assert.assertEquals(1, listener.getWorkStartedCallCount());
        Assert.assertEquals(1, listener.getWorkCompletedCallCount());
        Assert.assertEquals(1, listener.getWorkExceptions().size());
    }

    /**
     * Tests running a mixture of fast and slow jobs on the ThreadPoolWorkManager
     */
    @Test
    public void testMultipleJobs() {
        // Create the work and register it
        TimeDelayWork fast1 = new TimeDelayWork(5);
        TimeDelayWork fast2 = new TimeDelayWork(10);
        TimeDelayWork fast3 = new TimeDelayWork(20);
        TimeDelayWork slow1= new TimeDelayWork(200);
        TimeDelayWork slow2 = new TimeDelayWork(200);
        TestWorkListener listener = new TestWorkListener();
        workManager.schedule(fast1, listener);
        workManager.schedule(fast2, listener);
        workManager.schedule(fast3, listener);
        workManager.schedule(slow1, listener);
        workManager.schedule(slow2, listener);

        // Wait for the 5 jobs to complete
        waitForWorkToComplete(listener, 5);

        // Test that the job completed successfully.
        Assert.assertEquals(5, listener.getWorkAcceptedCallCount());
        Assert.assertEquals(0, listener.getWorkRejectedCallCount());
        Assert.assertEquals(5, listener.getWorkStartedCallCount());
        Assert.assertEquals(5, listener.getWorkCompletedCallCount());
        Assert.assertEquals(0, listener.getWorkExceptions().size());
    }

    /**
     * Tests running a mixture of fast and slow jobs some of which fail on the
     * ThreadPoolWorkManager
     */
    @Test
    public void testMultipleJobsSomeFail() {
        // Create the work and register it
        TimeDelayWork fast1 = new TimeDelayWork(5);
        TimeDelayWork fast2 = new TimeDelayWork(10);
        TimeDelayWork fast3 = new TimeDelayWork(20);
        TimeDelayWork slow1= new TimeDelayWork(200);
        TimeDelayWork slow2 = new TimeDelayWork(200);
        FailingWork fail1 = new FailingWork();
        FailingWork fail2 = new FailingWork();
        TestWorkListener listener = new TestWorkListener();
        workManager.schedule(fast1, listener);
        workManager.schedule(fast2, listener);
        workManager.schedule(fail1, listener);
        workManager.schedule(fast3, listener);
        workManager.schedule(slow1, listener);
        workManager.schedule(fail2, listener);
        workManager.schedule(slow2, listener);

        // Wait for the 7 jobs to complete
        waitForWorkToComplete(listener, 7);

        // Test that the job completed successfully.
        Assert.assertEquals(7, listener.getWorkAcceptedCallCount());
        Assert.assertEquals(0, listener.getWorkRejectedCallCount());
        Assert.assertEquals(7, listener.getWorkStartedCallCount());
        Assert.assertEquals(7, listener.getWorkCompletedCallCount());
        Assert.assertEquals(2, listener.getWorkExceptions().size());
    }

    /**
     * Tests running a single job that has no listener
     */
    @Test
    public void testSingleFastJobWithNoListener() {
        // Create the work and register it
        TimeDelayWork fast = new TimeDelayWork(10);
        workManager.schedule(fast);

        // Wait for the job to complete
        long startTime = System.currentTimeMillis();
        while (true) {
            int completedCount = fast.getRunCompletedCount();
            if (completedCount == 1) {
                break;
            }

            if (System.currentTimeMillis() - startTime > WAIT_TIMEOUT) {
                Assert.fail("Only " + completedCount + " work items completed before timeout");
                return;
            }

            // Lets wait for the job to complete
            try {
                Thread.sleep(25);
            } catch (InterruptedException ex) {
                Assert.fail("Unexpected exception: " + ex);
            }
        }

        // Make sure we have got one completed run
        Assert.assertEquals(1, fast.getRunCompletedCount());
    }

    /**
     * Waits for the specified number of jobs to complete or the timeout to fire.
     *
     * @param listener The listener to use to track Work unit completion
     * @param completedWorkItemsToWaitFor The number of Work items to complete
     */
    private void waitForWorkToComplete(TestWorkListener listener, int completedWorkItemsToWaitFor) {
        long startTime = System.currentTimeMillis();
        while (true) {
            int completedCount = listener.getWorkCompletedCallCount();
            if (completedCount == completedWorkItemsToWaitFor) {
                return;
            }

            if (System.currentTimeMillis() - startTime > WAIT_TIMEOUT) {
                Assert.fail("Only " + completedCount + " work items completed before timeout");
                return;
            }

            // Lets wait for more jobs to complete
            try {
                Thread.sleep(25);
            } catch (InterruptedException ex) {
                Assert.fail("Unexpected exception: " + ex);
            }
        }
    }
}