Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8a4a507aa0ccece1db2bfc6c55993a56ac15271d (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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*******************************************************************************
 * Copyright (c) 2006, 2010 Wind River Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.tests.dsf.concurrent;

import static org.junit.Assert.*;

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

import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.DsfRunnable;
import org.eclipse.cdt.dsf.concurrent.IDsfStatusConstants;
import org.eclipse.cdt.dsf.concurrent.Query;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor.ICanceledListener;
import org.eclipse.cdt.tests.dsf.DsfTestPlugin;
import org.eclipse.cdt.tests.dsf.TestDsfExecutor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Tests that exercise the Query object.
 */
public class DsfQueryTests {
    TestDsfExecutor fExecutor;
    
    @Before 
    public void startServices() throws ExecutionException, InterruptedException {
        fExecutor = new TestDsfExecutor();
    }   
    
    @After 
    public void shutdownServices() throws ExecutionException, InterruptedException {
        fExecutor.submit(new DsfRunnable() { public void run() {
            fExecutor.shutdown();
        }}).get();
        if (fExecutor.exceptionsCaught()) {
            Throwable[] exceptions = fExecutor.getExceptions();
            throw new ExecutionException(exceptions[0]);
        }
        fExecutor = null;
    }
    
    @Test 
    public void simpleGetTest() throws InterruptedException, ExecutionException {
        Query<Integer> q = new Query<Integer>() { 
            @Override
            protected void execute(DataRequestMonitor<Integer> rm) {
                rm.setData(1);
                rm.done();
            }
        };
        // Check initial state
        assertTrue(!q.isDone());
        assertTrue(!q.isCancelled());
        
        fExecutor.execute(q);
        assertEquals(1, (int)q.get());
        
        // Check final state
        assertTrue(q.isDone());
        assertTrue(!q.isCancelled());

    }

    @Test 
    public void getErrorTest() throws InterruptedException, ExecutionException {
        final String error_message = "Test Error";
        
        Query<Integer> q = new Query<Integer>() { 
            @Override
            protected void execute(DataRequestMonitor<Integer> rm) {
                rm.setStatus(new Status(IStatus.ERROR, DsfTestPlugin.PLUGIN_ID, IDsfStatusConstants.INTERNAL_ERROR, error_message, null)); //$NON-NLS-1$
                rm.done();
            }
        };

        // Check initial state
        assertTrue(!q.isDone());
        assertTrue(!q.isCancelled());
        
        fExecutor.execute(q);
        
        try {
            q.get();
            fail("Expected exception");
        } catch (ExecutionException e) {
            assertEquals(e.getCause().getMessage(), error_message);
        }
        
        // Check final state
        assertTrue(q.isDone());
        assertTrue(!q.isCancelled());

    }

    @Test 
    public void doneExceptionTest() throws InterruptedException, ExecutionException {
        Query<Integer> q = new Query<Integer>() { 
            @SuppressWarnings("deprecation")
            @Override
            protected void execute(DataRequestMonitor<Integer> rm) {
                doneException(new Throwable());
            }
        };

        // Check initial state
        assertTrue(!q.isDone());
        assertTrue(!q.isCancelled());
        
        fExecutor.execute(q);
        
        try {
            q.get();
            fail("Expected exception");
        } catch (ExecutionException e) {
        }
        
        // Check final state
        assertTrue(q.isDone());
        assertTrue(!q.isCancelled());

    }

    
    @Test 
    public void getWithMultipleDispatchesTest() throws InterruptedException, ExecutionException {
        Query<Integer> q = new Query<Integer>() { 
            @Override
            protected void execute(final DataRequestMonitor<Integer> rm) {
                fExecutor.execute(new DsfRunnable() { 
                    public void run() {
                        rm.setData(1);
                        rm.done();
                    }
                    @Override
                    public String toString() { return super.toString() + "\n       getWithMultipleDispatchesTest() second runnable"; } //$NON-NLS-1$
                });
            }
            @Override
            public String toString() { return super.toString() + "\n       getWithMultipleDispatchesTest() first runnable (query)"; } //$NON-NLS-1$
        };
        fExecutor.execute(q);
        assertEquals(1, (int)q.get()); 
    }

    @Test (expected = ExecutionException.class)
    public void exceptionOnGetTest() throws InterruptedException, ExecutionException {
        Query<Integer> q = new Query<Integer>() { 
            @Override
            protected void execute(final DataRequestMonitor<Integer> rm) {
                rm.setStatus(new Status(IStatus.ERROR, DsfTestPlugin.PLUGIN_ID, -1, "", null)); //$NON-NLS-1$
                rm.done();
            }
        };
        
        fExecutor.execute(q);
        
        try {
            q.get();
        } finally {
            assertTrue(q.isDone());
            assertTrue(!q.isCancelled());
        }            
    }

    @Test
    public void cancelBeforeWaitingTest() throws InterruptedException, ExecutionException {
        final Query<Integer> q = new Query<Integer>() { 
            @Override protected void execute(final DataRequestMonitor<Integer> rm) {
                fail("Query was cancelled, it should not be called."); //$NON-NLS-1$
                rm.done();
            }
        };
        
        // Cancel before invoking the query.
        q.cancel(false);

        assertTrue(q.isDone());
        assertTrue(q.isCancelled());            

        // Start the query.
        fExecutor.execute(q);
        
        
        
        // Block to retrieve data
        try {
            q.get();
        } catch (CancellationException e) {
            return; // Success
        } finally {
            assertTrue(q.isDone());
            assertTrue(q.isCancelled());            
        }            
        assertTrue("CancellationException should have been thrown", false); //$NON-NLS-1$
    }

    @Test
    public void cancelWhileWaitingTest() throws InterruptedException, ExecutionException {
        final DataRequestMonitor<?>[] rmHolder = new DataRequestMonitor<?>[1];   
        final Boolean[] cancelCalled = new Boolean[] { Boolean.FALSE };
        
        final Query<Integer> q = new Query<Integer>() { 
            @Override protected void execute(final DataRequestMonitor<Integer> rm) {
                synchronized (rmHolder) {
                    rmHolder[0] = rm;
                    rmHolder.notifyAll();
                }
            }
        };
        
        // Start the query.
        fExecutor.execute(q);

        // Wait until the query is started
        synchronized (rmHolder) {
            while(rmHolder[0] == null) {
                rmHolder.wait();
            }
        }        
        
        // Add a cancel listener to the query RM
        rmHolder[0].addCancelListener(new ICanceledListener() {
            
            public void requestCanceled(RequestMonitor rm) {
                cancelCalled[0] = Boolean.TRUE;
            }
        });
        
        // Cancel running request.
        q.cancel(false);
        
        assertTrue(cancelCalled[0]);
        assertTrue(rmHolder[0].isCanceled());
        assertTrue(q.isCancelled());
        assertTrue(q.isDone());
        
        // Retrieve data
        try {
            q.get();
        } catch (CancellationException e) {
            return; // Success
        } finally {
            assertTrue(q.isDone());
            assertTrue(q.isCancelled());            
        }            
        
        // Complete rm and query.
        @SuppressWarnings("unchecked")
        DataRequestMonitor<Integer> drm = (DataRequestMonitor<Integer>)rmHolder[0]; 
        drm.setData(new Integer(1));
        rmHolder[0].done();
        
        // Try to retrieve data again, it should still result in 
        // cancellation exception.
        try {
            q.get();
        } catch (CancellationException e) {
            return; // Success
        } finally {
            assertTrue(q.isDone());
            assertTrue(q.isCancelled());            
        }            

        
        assertTrue("CancellationException should have been thrown", false); //$NON-NLS-1$
    }

    
    @Test
    public void getTimeoutTest() throws InterruptedException, ExecutionException {
        final Query<Integer> q = new Query<Integer>() { 
            @Override
            protected void execute(final DataRequestMonitor<Integer> rm) {
                // Call done with a delay of 1 second, to avoid stalling the tests.
                fExecutor.schedule(
                    new DsfRunnable() {
                        public void run() { rm.done(); }
                    }, 
                    60, TimeUnit.SECONDS);
            }
        };

        fExecutor.execute(q);

        // Note: no point in checking isDone() and isCancelled() here, because
        // the value could change on timing.
        
        try {
            q.get(1, TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            return; // Success
        } finally {
            assertFalse("Query should not be done yet, it should have timed out first.", q.isDone()); //$NON-NLS-1$
        }            
        assertTrue("TimeoutException should have been thrown", false); //$NON-NLS-1$
    }

}

Back to the top