aboutsummaryrefslogtreecommitdiffstats
path: root/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/internal/output/MockSocketConnectionTest.java
blob: 814946d8e897a8b665f4c4cf8b9182ecf83d50c8 (plain)
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
/*******************************************************************************
 * Copyright (c) 2009, 2015 Mountainminds GmbH & Co. KG and Contributors
 * 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:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.agent.rt.internal.output;

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

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for {@link MockSocketConnection}.
 */
public class MockSocketConnectionTest extends ExecutorTestBase {

	/**
	 * To verify that the tests reflect the behavior of real TCP sockets this
	 * flag can be set to <code>true</code>.
	 */
	private static final boolean REAL_SOCKETS = Boolean
			.getBoolean("MockSocketConnectionTest.realSockets");

	private Socket a;

	private Socket b;

	@Before
	@Override
	public void setup() throws Exception {
		super.setup();
		if (REAL_SOCKETS) {
			createRealSockets();
		} else {
			createMockSockets();
		}
	}

	private void createMockSockets() throws Exception {
		final MockSocketConnection con = new MockSocketConnection();
		a = con.getSocketA();
		b = con.getSocketB();
	}

	private void createRealSockets() throws Exception {
		System.err.println("Using Real Sockets!");
		final InetAddress addr = InetAddress.getByName("localhost");
		ServerSocket ssocket = new ServerSocket(16300, 1, addr);
		a = new Socket(addr, 16300);
		b = ssocket.accept();
		ssocket.close();
	}

	@Test
	public void testIsClosed() throws Exception {
		assertFalse(a.isClosed());
		a.close();
		assertTrue(a.isClosed());
	}

	@Test(expected = SocketException.class)
	public void testGetInputStreamOnClosedSocket() throws Exception {
		a.close();
		a.getInputStream();
	}

	@Test(expected = SocketException.class)
	public void testReadOnClosedSocket() throws Exception {
		final InputStream in = a.getInputStream();
		a.close();
		in.read();
	}

	@Test(expected = SocketException.class)
	public void testReadOnClosedSocketAsync() throws Throwable {
		final InputStream in = a.getInputStream();
		final Future<Void> f = executor.submit(new Callable<Void>() {
			public Void call() throws Exception {
				in.read();
				return null;
			}
		});

		assertBlocks(f);

		a.close();
		try {
			f.get();
		} catch (ExecutionException e) {
			throw e.getCause();
		}
	}

	@Test(expected = SocketException.class)
	public void testGetOutputStreamOnClosedSocket() throws Exception {
		a.close();
		a.getOutputStream();
	}

	@Test(expected = SocketException.class)
	public void testWriteOnClosedSocket() throws Exception {
		final OutputStream out = a.getOutputStream();
		a.close();
		out.write(123);
	}

	@Test
	public void testContents() throws Exception {
		final InputStream in = a.getInputStream();
		final OutputStream out = b.getOutputStream();

		assertEquals(0, in.available());

		out.write(0);
		out.write(1);
		out.write(2);
		out.write(255);

		if (!REAL_SOCKETS) {
			// Real sockets will have a delay
			assertEquals(4, in.available());
		}

		assertEquals(0, in.read());
		assertEquals(1, in.read());
		assertEquals(2, in.read());
		assertEquals(255, in.read());
	}

	@Test
	public void testWaitForContents() throws Exception {
		final InputStream in = a.getInputStream();
		final OutputStream out = b.getOutputStream();

		final Future<Byte> f = executor.submit(new Callable<Byte>() {
			public Byte call() throws Exception {
				return Byte.valueOf((byte) in.read());
			}
		});

		assertBlocks(f);
		out.write(123);
		assertEquals(123, f.get().byteValue());
	}

	@Test
	public void testCloseOtherSocket() throws Exception {
		b.close();
		final InputStream in = a.getInputStream();
		assertEquals(-1, in.read());
	}

	@Test
	public void testCloseOtherSocketAsync() throws Exception {
		final InputStream in = a.getInputStream();

		final Future<Byte> f = executor.submit(new Callable<Byte>() {
			public Byte call() throws Exception {
				return Byte.valueOf((byte) in.read());
			}
		});

		assertBlocks(f);
		b.close();

		assertEquals(-1, f.get().byteValue());
	}

}