Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 52e59cd9f2802f0eaee586f23386619a45dc8b73 (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
/*******************************************************************************
 * Copyright (c) 2016, 2017 Andrey Loskutov 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:
 *     Andrey Loskutov <loskutov@gmx.de> - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.tests.console;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.debug.tests.AbstractDebugTest;
import org.eclipse.debug.tests.TestUtil;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.IConsoleView;
import org.eclipse.ui.part.IPageBookViewPage;
import org.eclipse.ui.part.MessagePage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Tests console manager
 */
public class ConsoleManagerTests extends AbstractDebugTest {

	private ExecutorService executorService;
	private IConsoleManager manager;
	private int count;
	private CountDownLatch latch;
	private ConsoleMock[] consoles;
	ConsoleMock firstConsole;

	@Override
	@Before
	public void setUp() throws Exception {
		super.setUp();
		assertNotNull("Must run in UI thread, but was in: " + Thread.currentThread().getName(), //$NON-NLS-1$
				Display.getCurrent());
		count = 20;
		latch = new CountDownLatch(count);
		executorService = Executors.newFixedThreadPool(count);
		manager = ConsolePlugin.getDefault().getConsoleManager();
		IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		TestUtil.processUIEvents(100);
		consoles = new ConsoleMock[count];
		for (int i = 0; i < count; i++) {
			final ConsoleMock console = new ConsoleMock(i + 1);
			consoles[i] = console;
		}
		// register consoles (this does *not* show anything)
		manager.addConsoles(consoles);

		IViewPart consoleView = activePage.showView("org.eclipse.ui.console.ConsoleView"); //$NON-NLS-1$
		activePage.activate(consoleView);
		TestUtil.processUIEvents(100);

		// The test is unstable ("show" event on the the first console seem to
		// be not always sent), so make sure console view has shown at least
		// one console for real before starting the main test
		firstConsole = new ConsoleMock(0);
		manager.addConsoles(new ConsoleMock[] { firstConsole });
		manager.showConsoleView(firstConsole);
		TestUtil.waitForJobs(name.getMethodName(), 200, 5000);
		TestUtil.processUIEvents(100);
		ConsoleMock.allShownConsoles.set(0);
	}

	@Override @After
	public void tearDown() throws Exception {
		executorService.shutdownNow();
		manager.removeConsoles(consoles);
		manager.removeConsoles(new ConsoleMock[] { firstConsole });
		TestUtil.processUIEvents(100);
		super.tearDown();
	}

	/**
	 * The test triggers {@link #count} simultaneous calls to the
	 * {@link IConsoleManager#showConsoleView(IConsole)} and checks if all of
	 * this calls were properly proceeded by the manager.
	 * <p>
	 * See bug 489546.
	 *
	 * @throws Exception
	 */
	@Test
	public void testShowAllConsoles() throws Exception {
		// Create a number of threads which will start and wait for the last one
		// created to call ConsoleManager.show.
		for (ConsoleMock console : consoles) {
			showConsole(console);
		}
		System.out.println("All tasks scheduled, processing UI events now..."); //$NON-NLS-1$
		TestUtil.processUIEvents(1000);

		// Console manager starts a job with delay, let wait for him a bit
		System.out.println("Waiting on jobs now..."); //$NON-NLS-1$
		TestUtil.waitForJobs(name.getMethodName(), 200, 5000);

		// Give UI a chance to proceed pending console manager jobs
		System.out.println("Done with jobs, processing UI events again..."); //$NON-NLS-1$
		TestUtil.processUIEvents(3000);

		executorService.shutdown();

		System.out.println("Waiting on execution service to finish..."); //$NON-NLS-1$
		boolean OK = waitForExecutorService();
		if (!OK) {
			System.out.println("Timed out..."); //$NON-NLS-1$
			TestUtil.processUIEvents(10000);

			// timeout?
			assertTrue("Timeout occurred while waiting on console to be shown", //$NON-NLS-1$
					waitForExecutorService());
		} else {
			System.out.println("Done waiting on execution service to finish"); //$NON-NLS-1$
		}
		int shown = ConsoleMock.allShownConsoles.intValue();
		assertEquals("Only " + shown + " consoles were shown from " + count, count, shown); //$NON-NLS-1$ //$NON-NLS-2$
	}

	private boolean waitForExecutorService() throws Exception {
		for (int i = 0; i < 60; i++) {
			if (executorService.awaitTermination(1, TimeUnit.SECONDS)) {
				return true;
			}
			TestUtil.processUIEvents(100);
		}
		return false;
	}

	private void showConsole(final ConsoleMock console) {
		executorService.execute(() -> {
			// last one arriving here triggers execution for all at same
			// time
			latch.countDown();
			try {
				latch.await(1, TimeUnit.MINUTES);
				System.out.println("Requesting to show: " + console); //$NON-NLS-1$
				manager.showConsoleView(console);
				TestUtil.waitForJobs(name.getMethodName(), 200, 5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
				Thread.interrupted();
			}
		});
	}

	/**
	 * Dummy console page showing mock number and counting the numbers its
	 * control was shown in the console view.
	 */
	static final class ConsoleMock implements IConsole {
		MessagePage page;
		final AtomicInteger showCalled;
		final int number;
		final static AtomicInteger allShownConsoles = new AtomicInteger();

		public ConsoleMock(int number) {
			this.number = number;
			showCalled = new AtomicInteger();
		}

		@Override
		public void removePropertyChangeListener(IPropertyChangeListener listener) {
		}

		@Override
		public String getType() {
			return null;
		}

		@Override
		public String getName() {
			return toString();
		}

		@Override
		public ImageDescriptor getImageDescriptor() {
			return null;
		}

		@Override
		public void addPropertyChangeListener(IPropertyChangeListener listener) {
		}

		/**
		 * Just a page showing the mock console name
		 */
		@Override
		public IPageBookViewPage createPage(IConsoleView view) {
			page = new MessagePage() {
				@Override
				public void createControl(Composite parent) {
					super.createControl(parent);
					// This listener is get called if the page is really shown
					// in the console view
					getControl().addListener(SWT.Show, event -> {
						int count = showCalled.incrementAndGet();
						if (count == 1) {
							count = allShownConsoles.incrementAndGet();
							System.out.println("Shown: " + ConsoleMock.this + ", overall: " + count); //$NON-NLS-1$ //$NON-NLS-2$
						}
					});
				}

			};
			page.setMessage(toString());
			return page;
		}

		@Override
		public String toString() {
			return "mock #" + number; //$NON-NLS-1$
		}
	}

}

Back to the top