Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 220a412fd4042925fc04eca3d57133931dd44d6b (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
314
315
316
317
318
319
320
321
/*******************************************************************************
 * Copyright (c) 2012 Wind River Systems, Inc. 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.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.eclipse.tcf.debug.test.services.IWaitForEventCache;
import org.eclipse.tcf.debug.test.services.RunControlCM;
import org.eclipse.tcf.debug.test.services.RunControlCM.ContextState;
import org.eclipse.tcf.debug.test.util.AbstractCache;
import org.eclipse.tcf.debug.test.util.ICache;
import org.eclipse.tcf.debug.test.util.Transaction;
import org.eclipse.tcf.services.IRunControl;
import org.eclipse.tcf.services.IRunControl.RunControlContext;
import org.junit.Assert;

public class RunControlCMTest extends AbstractCMTest {

    public void testStateResetOnResumeSuspend() throws Exception {
        final TestProcessInfo processInfo = startProcess("tcf_test_func0");

        createBreakpoint("test", "tcf_test_func0");

        // Create and validate cache
        final ICache<ContextState> stateCache = new Transaction<ICache<ContextState>>() {
            public ICache<ContextState> process() throws InvalidCacheException, ExecutionException  {
                ICache<ContextState> cache = fRunControlCM.getState(processInfo.fThreadId);
                validate(cache);
                Assert.assertTrue(cache.getData().suspended == true);
                return cache;
            };
        }.get();

        // Resume thread and wait for suspend
        new Transaction<Object>() {
            @Override
            protected Object process() throws InvalidCacheException, ExecutionException {
                IWaitForEventCache<Object> waitResume = fRunControlCM.waitForContextResumed(processInfo.fThreadId, this);
                IWaitForEventCache<Object> waitSuspend = fRunControlCM.waitForContextSuspended(processInfo.fThreadId, this);
                validate(fRunControlCM.resume(processInfo.fThreadCtx, this, IRunControl.RM_RESUME, 1));
                validate(waitResume);
                if (!waitSuspend.isValid()) {
                    // The state cache should either be invalid, or it should contain updated (running) state.
                    Assert.assertTrue(stateCache.isValid() == false || stateCache.getData().suspended == false);
                    validate(stateCache);
                    Assert.assertTrue(stateCache.getData().suspended == false);                    
                }
                validate(waitSuspend);
                // The state cache should either be invalid again, or it should contain updated (suspended) state.
                Assert.assertTrue(stateCache.isValid() == false || stateCache.getData().suspended == true);
                validate(stateCache);
                Assert.assertTrue(stateCache.getData().suspended == true);
                
                return null;
            }
        }.get();
    }

    public void testStateResetOnTerminate() throws Exception {
        final TestProcessInfo processInfo = startProcess("tcf_test_func0");

        // Create and validate cache
        final ICache<ContextState> stateCache = new Transaction<ICache<ContextState>>() {
            public ICache<ContextState> process() throws InvalidCacheException, ExecutionException  {
                ICache<ContextState> cache = fRunControlCM.getState(processInfo.fThreadId);
                validate(cache);
                Assert.assertTrue(cache.getData().suspended == true);
                return cache;
            };
        }.get();

        // Terminate process and check state
        new Transaction<Object>() {
            @Override
            protected Object process() throws InvalidCacheException, ExecutionException {
                IWaitForEventCache<String[]> wait = fRunControlCM.waitForContextRemoved(processInfo.fProcessId, this);
                validate(fRunControlCM.terminate(processInfo.fTestCtx, this));
                validate(wait);
                
                // The state cache should either be invalid again, or it should contain updated (suspended) state.
                Assert.assertTrue(stateCache.isValid() == false || stateCache.getError() != null);
                if (!validateUnchecked(stateCache)) throw new InvalidCacheException();
                Assert.assertTrue(stateCache.getError() != null);
                
                return null;
            }
        }.get();
    }
   
    public void testRunControlCMChildrenInvalidation() throws Exception {
        final TestProcessInfo processInfo = startProcess("tcf_test_func0");

        createBreakpoint("testRunControlCMChildrenInvalidation", "tcf_test_func0");

        // Wait for each threads to start.
        final String[] threads = new Transaction<String[]>() {
            List<String> fThreads = new ArrayList<String>();
            @Override
            protected String[] process() throws InvalidCacheException, ExecutionException {
                IWaitForEventCache<RunControlContext[]> waitCache = fRunControlCM.waitForContextAdded(processInfo.fProcessId, this);
                validate(fRunControlCM.resume(processInfo.fTestCtx, this, IRunControl.RM_RESUME, 1));
                RunControlContext[] addedContexts = validate(waitCache);
                for (RunControlContext addedContext : addedContexts) {
                    fThreads.add(addedContext.getID());
                }
                if (fThreads.size() < 4) {
                    waitCache.reset();
                    validate(waitCache);
                }
                // Validate children cache
                String[] children = validate (fRunControlCM.getChildren(processInfo.fProcessId));
                Assert.assertTrue(
                    "Expected children array to contain added ids",
                    Arrays.asList(children).containsAll(fThreads));

                return fThreads.toArray(new String[fThreads.size()]);
            }
        }.get();

        // Wait for each thread to suspend, update caches
        for (final String thread : threads) {
            new Transaction<Object>() {
                @Override
                protected Object process() throws InvalidCacheException, ExecutionException {
                    RunControlCM.ContextState state = validate(fRunControlCM.getState(thread));
                    if (!state.suspended) {
                        validate( fRunControlCM.waitForContextSuspended(thread, this) );
                    }
                    String symId = validate( fSymbolsCM.find(thread, new BigInteger(state.pc), "tcf_test_func0") );
                    Number symAddr = validate( fSymbolsCM.getContext(symId) ).getAddress();
                    Assert.assertEquals("Expected thread to suspend at breakpoint address", symAddr.toString(), state.pc);
                    String[] children = validate( fRunControlCM.getChildren(thread));
                    Assert.assertEquals("Expected thread to have no children contexts", 0, children.length);
                    return null;
                }
            }.get();
        }

        // End test, check for removed events and that state caches were cleared
        new Transaction<String>() {
            @Override
            protected String process() throws InvalidCacheException, ExecutionException {
                // Create wait caches
                fRunControlCM.waitForContextRemoved(processInfo.fProcessId, this);
                IWaitForEventCache<?>[] waitCaches = new IWaitForEventCache<?>[threads.length];
                for (int i = 0; i < threads.length; i++) {
                    waitCaches[i] = fRunControlCM.waitForContextRemoved(threads[i], this);
                }
                validate( fDiagnosticsCM.cancelTest(processInfo.fTestId, this) );
                validate(waitCaches);
                validate(fRunControlCM.waitForContextRemoved(processInfo.fProcessId, this));

                try {
                    validate( fRunControlCM.getContext(processInfo.fProcessId) );
                    Assert.fail("Expected error");
                } catch (ExecutionException e) {}
                try {
                    validate( fRunControlCM.getState(processInfo.fProcessId) );
                    Assert.fail("Expected error");
                } catch (ExecutionException e) {}
                try {
                    String children[] = validate( fRunControlCM.getChildren(processInfo.fProcessId) );
                    Assert.assertEquals("Expected no children", 0, children.length);
                } catch (ExecutionException e) {}

                for (String thread : threads) {
                    try {
                        validate( fRunControlCM.getContext(thread) );
                        Assert.fail("Expected error");
                    } catch (ExecutionException e) {}
                    try {
                        validate( fRunControlCM.getState(thread) );
                        Assert.fail("Expected error");
                    } catch (ExecutionException e) {}
                }

                return null;
            }
        }.get();

        removeBreakpoint("testRunControlCMChildrenInvalidation");
    }
    
    public void testChildrenResetOnAddedRemoved() throws Exception {
        final TestProcessInfo processInfo = startProcess("tcf_test_func0");

        createBreakpoint("test", "tcf_test_func0");

        // Create and validate cache
        final ICache<String[]> childrenCache = new Transaction<ICache<String[]>>() {
            public ICache<String[]> process() throws InvalidCacheException, ExecutionException  {
                ICache<String[]> cache = fRunControlCM.getChildren(processInfo.fProcessId);
                validate(cache);
                Assert.assertTrue(cache.getData().length == 1);
                return cache;
            };
        }.get();

        // Wait for each threads to start.
        final String[] threads = new Transaction<String[]>() {
            List<String> fThreads = new ArrayList<String>();
            @Override
            protected String[] process() throws InvalidCacheException, ExecutionException {
                IWaitForEventCache<RunControlContext[]> waitCache = fRunControlCM.waitForContextAdded(processInfo.fProcessId, this);
                validate(fRunControlCM.resume(processInfo.fTestCtx, this, IRunControl.RM_RESUME, 1));
                RunControlContext[] addedContexts = validate(waitCache);
                for (RunControlContext addedContext : addedContexts) {
                    fThreads.add(addedContext.getID());
                }
                if (fThreads.size() < 4) {
                    waitCache.reset();
                    validate(waitCache);
                }
                
                // Validate children cache
                String[] children = validate (childrenCache);

                Assert.assertTrue(
                    "Expected children array to contain added ids",
                    Arrays.asList(children).containsAll(fThreads));

                return fThreads.toArray(new String[fThreads.size()]);
            }
        }.get();

        // Wait for each thread to suspend, 
        for (final String thread : threads) {
            new Transaction<Object>() {
                @Override
                protected Object process() throws InvalidCacheException, ExecutionException {
                    RunControlCM.ContextState state = validate(fRunControlCM.getState(thread));
                    if (!state.suspended) {
                        validate( fRunControlCM.waitForContextSuspended(thread, this) );
                    }
                    String[] children = validate( fRunControlCM.getChildren(thread));
                    Assert.assertEquals("Expected thread to have no children contexts", 0, children.length);
                    return null;
                }
            }.get();
        }

        // End test, check for removed events and that state caches were cleared
        new Transaction<String>() {
            @Override
            protected String process() throws InvalidCacheException, ExecutionException {
                // Create wait caches
                fRunControlCM.waitForContextRemoved(processInfo.fProcessId, this);
                IWaitForEventCache<?>[] waitCaches = new IWaitForEventCache<?>[threads.length];
                for (int i = 0; i < threads.length; i++) {
                    waitCaches[i] = fRunControlCM.waitForContextRemoved(threads[i], this);
                }
                validate( fDiagnosticsCM.cancelTest(processInfo.fTestId, this) );
                validate(waitCaches);
                validate(fRunControlCM.waitForContextRemoved(processInfo.fProcessId, this));

                try {
                    validate( fRunControlCM.getContext(processInfo.fProcessId) );
                    Assert.fail("Expected error");
                } catch (ExecutionException e) {}
                try {
                    validate( fRunControlCM.getState(processInfo.fProcessId) );
                    Assert.fail("Expected error");
                } catch (ExecutionException e) {}
                try {
                    String children[] = validate( fRunControlCM.getChildren(processInfo.fProcessId) );
                    Assert.assertEquals("Expected no children", 0, children.length);
                } catch (ExecutionException e) {}

                for (String thread : threads) {
                    try {
                        validate( fRunControlCM.getContext(thread) );
                        Assert.fail("Expected error");
                    } catch (ExecutionException e) {}
                    try {
                        validate( fRunControlCM.getState(thread) );
                        Assert.fail("Expected error");
                    } catch (ExecutionException e) {}
                }

                return null;
            }
        }.get();
    }

    public void testMappingCommandCaches() throws Exception {
        final TestProcessInfo processInfo = startProcess("tcf_test_func0");

        // Wait for each threads to start.
        new Transaction<Object>() {
            ICache<Object> fFirstCommandCache;
            
            @Override
            protected Object process() throws InvalidCacheException, ExecutionException {
                ICache<RunControlContext> contextCache = fRunControlCM.getContext(processInfo.fThreadId);
                validate(contextCache);
                
                ICache<Object> resumeCache = fRunControlCM.resume(contextCache.getData(), this, IRunControl.RM_RESUME, 1);
                if (fFirstCommandCache == null) {
                    // Reset context objet cache to force a new context object to be created and retry.
                    fFirstCommandCache = resumeCache;
                    ((AbstractCache<?>)contextCache).reset();
                    validate(contextCache);
                }
                Assert.assertTrue(resumeCache == fFirstCommandCache);
                return null;
            }
        }.get();

    }
}

Back to the top