Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b6a3b3c73bccf1bd18985323c339fc3887c957ee (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
/*******************************************************************************
 * 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 static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.charset.Charset;
import java.util.Set;

import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.internal.core.DebugCoreMessages;
import org.eclipse.debug.internal.core.InputStreamMonitor;
import org.eclipse.debug.tests.AbstractDebugTest;
import org.eclipse.debug.tests.TestUtil;
import org.eclipse.debug.tests.TestsPlugin;
import org.junit.Test;

/**
 * Tests the {@link InputStreamMonitor}.
 */
public class InputStreamMonitorTests extends AbstractDebugTest {

	/**
	 * Simple test for input stream monitor. Write some bytes before starting
	 * the monitor, some after and check if they are correctly transfered.
	 */
	@Test
	@SuppressWarnings("resource")
	public void testInputStreamMonitor() throws Exception {
		PipedInputStream sysin = new PipedInputStream();
		InputStreamMonitor monitor = new InputStreamMonitor(new PipedOutputStream(sysin));

		byte[] content = new byte[100];
		for (int i = 0; i < content.length; i++) {
			content[i] = (byte) (i % 255);
		}
		try {
			int half = content.length / 2;
			monitor.write(content, 0, half);
			monitor.startMonitoring();
			monitor.write(content, half, content.length - half);
			Thread.sleep(30);

			byte[] readBack = new byte[content.length];
			int read = sysin.read(readBack);
			assertEquals("Monitor wrote to few bytes.", read, content.length);
			assertEquals("Monitor wrote to much bytes.", 0, sysin.available());
			assertArrayEquals("Monitor wrote wrong content.", content, readBack);
		} finally {
			monitor.close();
		}
	}

	/**
	 * Test that passing <code>null</code> as charset does not raise exceptions.
	 */
	@Test
	@SuppressWarnings("resource")
	public void testNullCharset() throws Exception {
		PipedInputStream sysin = new PipedInputStream();
		InputStreamMonitor monitor = new InputStreamMonitor(new PipedOutputStream(sysin), (Charset) null);
		String text = "o\u00F6O\u00EFiI\u00D6\u00D8\u00F8";
		try {
			monitor.startMonitoring();
			monitor.write(text);
			Thread.sleep(30);

			byte[] readBack = new byte[1000];
			int len = sysin.read(readBack);
			assertEquals("Monitor wrote wrong content.", text, new String(readBack, 0, len));
		} finally {
			monitor.close();
		}
	}

	/**
	 * Test different combinations of stream closing.
	 */
	@Test
	@SuppressWarnings("resource")
	public void testClose() throws Exception {
		Set<Thread> allThreads = Thread.getAllStackTraces().keySet();
		long alreadyLeakedThreads = allThreads.stream().filter(t -> DebugCoreMessages.InputStreamMonitor_label.equals(t.getName())).count();
		if (alreadyLeakedThreads > 0) {
			Platform.getLog(TestsPlugin.class).warn("Test started with " + alreadyLeakedThreads + " leaked monitor threads.");
		}

		{
			ClosableTestOutputStream testStream = new ClosableTestOutputStream();
			InputStreamMonitor monitor = new InputStreamMonitor(testStream);
			assertEquals("Stream closed to early.", 0, testStream.numClosed);
			monitor.closeInputStream();
			TestUtil.waitWhile(() -> testStream.numClosed == 0, 100);
			assertEquals("Stream not closed.", 1, testStream.numClosed);
		}
		{
			ClosableTestOutputStream testStream = new ClosableTestOutputStream();
			InputStreamMonitor monitor = new InputStreamMonitor(testStream);
			monitor.startMonitoring();
			assertEquals("Stream closed to early.", 0, testStream.numClosed);
			monitor.close();
			TestUtil.waitWhile(() -> testStream.numClosed == 0, 200);
			assertEquals("Stream not closed.", 1, testStream.numClosed);
		}
		{
			ClosableTestOutputStream testStream = new ClosableTestOutputStream();
			InputStreamMonitor monitor = new InputStreamMonitor(testStream);
			monitor.startMonitoring();
			assertEquals("Stream closed to early.", 0, testStream.numClosed);
			monitor.closeInputStream();
			monitor.close();
			monitor.close();
			TestUtil.waitWhile(() -> testStream.numClosed == 0, 100);
			assertEquals("Stream not closed or to often.", 1, testStream.numClosed);
		}

		allThreads = Thread.getAllStackTraces().keySet();
		long numMonitorThreads = allThreads.stream().filter(t -> DebugCoreMessages.InputStreamMonitor_label.equals(t.getName())).count();
		assertEquals("Leaked monitor threads.", 0, numMonitorThreads);
	}

	/**
	 * Extension of output stream to log calls to {@link #close()}.
	 */
	public static class ClosableTestOutputStream extends OutputStream {
		public volatile int numClosed = 0;

		@Override
		public void close() throws IOException {
			numClosed++;
		}

		@Override
		public void write(int b) throws IOException {
		}
	}
}

Back to the top