Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1c790c7fa2c9936eb2fa2eb0a23e1d84b31995fd (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
/*******************************************************************************
 * 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.tcf.debug.test;

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

import junit.framework.Assert;
import junit.framework.TestCase;

import org.eclipse.tcf.debug.test.util.Callback;
import org.eclipse.tcf.debug.test.util.Callback.ICanceledListener;
import org.eclipse.tcf.debug.test.util.DataCallback;
import org.eclipse.tcf.debug.test.util.Query;
import org.eclipse.tcf.protocol.Protocol;
import org.junit.Test;

/**
 * Tests that exercise the Query object.
 */
public class QueryTests  extends TestCase{
    
    public void testSimpleGet() throws InterruptedException, ExecutionException {
        Query<Integer> q = new Query<Integer>() { 
            @Override
            protected void execute(DataCallback<Integer> rm) {
                rm.setData(1);
                rm.done();
            }
        };
        // Check initial state
        Assert.assertTrue(!q.isDone());
        Assert.assertTrue(!q.isCancelled());
        
        q.invoke();
        Assert.assertEquals(1, (int)q.get());
        
        // Check final state
        Assert.assertTrue(q.isDone());
        Assert.assertTrue(!q.isCancelled());

    }

    public void testGetError() throws InterruptedException, ExecutionException {
        final String error_message = "Test Error";
        
        Query<Integer> q = new Query<Integer>() { 
            @Override
            protected void execute(DataCallback<Integer> rm) {
                rm.setError(new Throwable(error_message)); //$NON-NLS-1$
                rm.done();
            }
        };

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

    }
     
    public void testGetWithMultipleDispatches() throws InterruptedException, ExecutionException {
        Query<Integer> q = new Query<Integer>() { 
            @Override
            protected void execute(final DataCallback<Integer> rm) {
                Protocol.invokeLater(new Runnable() { 
                    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$
        };
        q.invoke();
        Assert.assertEquals(1, (int)q.get()); 
    }

    public void testExceptionOnGet() throws InterruptedException, ExecutionException {
        Query<Integer> q = new Query<Integer>() { 
            @Override
            protected void execute(final DataCallback<Integer> rm) {
                rm.setError(new Throwable("")); //$NON-NLS-1$
                rm.done();
            }
        };
        
        q.invoke();
        
        try {
            q.get();
            Assert.fail("Excpected ExecutionExeption");
        } catch (ExecutionException e) {
        } finally {
            Assert.assertTrue(q.isDone());
            Assert.assertTrue(!q.isCancelled());
        }            
    }

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

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

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

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

        // 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(Callback rm) {
                cancelCalled[0] = Boolean.TRUE;
            }
        });
        
        // Cancel running request.
        q.cancel(false);
        
        Assert.assertTrue(cancelCalled[0]);
        Assert.assertTrue(rmHolder[0].isCanceled());
        Assert.assertTrue(q.isCancelled());
        Assert.assertTrue(q.isDone());
        
        // Retrieve data
        try {
            q.get();
        } catch (CancellationException e) {
            return; // Success
        } finally {
            Assert.assertTrue(q.isDone());
            Assert.assertTrue(q.isCancelled());            
        }            
        
        // Complete rm and query.
        @SuppressWarnings("unchecked")
        DataCallback<Integer> drm = (DataCallback<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 {
            Assert.assertTrue(q.isDone());
            Assert.assertTrue(q.isCancelled());            
        }            

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

    
    public void testGetTimeout() throws InterruptedException, ExecutionException {
        final Query<Integer> q = new Query<Integer>() { 
            @Override
            protected void execute(final DataCallback<Integer> rm) {
                // Call done with a delay of 1 second, to avoid stalling the tests.
                Protocol.invokeLater(
                    60000,
                    new Runnable() {
                        public void run() { rm.done(); }
                    });
            }
        };

        q.invoke();

        // 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 {
            Assert.assertFalse("Query should not be done yet, it should have timed out first.", q.isDone()); //$NON-NLS-1$
        }            
        Assert.assertTrue("TimeoutException should have been thrown", false); //$NON-NLS-1$
    }

}

Back to the top