Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c3d631b104219a3ba593a0af80136a47a29472f8 (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
/*******************************************************************************
 * Copyright (c) 2020 Paul Pazderski and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Paul Pazderski - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.tests.console;

import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IDebugEventSetListener;
import org.eclipse.debug.core.model.RuntimeProcess;
import org.eclipse.debug.tests.AbstractDebugTest;
import org.eclipse.debug.tests.TestUtil;

public class RuntimeProcessTests extends AbstractDebugTest {

	public RuntimeProcessTests() {
		super(RuntimeProcessTests.class.getSimpleName());
	}

	public RuntimeProcessTests(String name) {
		super(name);
	}

	/**
	 * Test behavior of {@link RuntimeProcess} if the wrapped process
	 * terminates.
	 */
	public void testProcessTerminated() throws Exception {
		AtomicInteger processTerminateEvents = new AtomicInteger();
		DebugPlugin.getDefault().addDebugEventListener(new IDebugEventSetListener() {
			@Override
			public void handleDebugEvents(DebugEvent[] events) {
				for (DebugEvent event : events) {
					if (event.getKind() == DebugEvent.TERMINATE) {
						processTerminateEvents.incrementAndGet();
					}
				}
			}
		});

		MockProcess mockProcess = new MockProcess(MockProcess.RUN_FOREVER);
		RuntimeProcess runtimeProcess = mockProcess.toRuntimeProcess();

		assertFalse("RuntimeProcess already terminated.", runtimeProcess.isTerminated());
		assertTrue(runtimeProcess.canTerminate());

		mockProcess.setExitValue(1);
		mockProcess.destroy();

		TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, 1000, p -> "RuntimePocess not terminated.");
		TestUtil.waitForJobs(getName(), 25, 500);
		assertEquals("Wrong number of terminate events.", 1, processTerminateEvents.get());
		assertEquals("RuntimeProcess reported wrong exit code.", 1, runtimeProcess.getExitValue());
	}

	/** Test {@link RuntimeProcess} terminating the wrapped process. */
	public void testTerminateProcess() throws Exception {
		AtomicInteger processTerminateEvents = new AtomicInteger();
		DebugPlugin.getDefault().addDebugEventListener(new IDebugEventSetListener() {
			@Override
			public void handleDebugEvents(DebugEvent[] events) {
				for (DebugEvent event : events) {
					if (event.getKind() == DebugEvent.TERMINATE) {
						processTerminateEvents.incrementAndGet();
					}
				}
			}
		});

		MockProcess mockProcess = new MockProcess(MockProcess.RUN_FOREVER);
		RuntimeProcess runtimeProcess = mockProcess.toRuntimeProcess();

		assertFalse("RuntimeProcess already terminated.", runtimeProcess.isTerminated());
		assertTrue(runtimeProcess.canTerminate());

		mockProcess.setExitValue(1);
		runtimeProcess.terminate();
		assertFalse("RuntimeProcess failed to terminated wrapped process.", mockProcess.isAlive());

		TestUtil.waitWhile(p -> !p.isTerminated(), runtimeProcess, 1000, p -> "RuntimePocess not terminated.");
		TestUtil.waitForJobs(getName(), 25, 500);
		assertEquals("Wrong number of terminate events.", 1, processTerminateEvents.get());
		assertEquals("RuntimeProcess reported wrong exit code.", 1, runtimeProcess.getExitValue());
	}
}

Back to the top