Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9cff567b3465813ffb1c19a5acd789a8a5923167 (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
/*******************************************************************************
 * Copyright (c) 2009, 2016 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ua.tests.help.webapp;

import static org.junit.Assert.fail;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.eclipse.help.internal.server.WebappManager;
import org.junit.Assert;
import org.junit.Test;

/**
 * Test to see if the help server is interruptable
 */

public class HelpServerInterrupt {

	private static boolean enableTimeout = true;
	private int iterations;
	private int sleepTime = 10;
	private class ServerStarter extends Thread {

		private Exception exception = null;

		@Override
		public synchronized void run() {
			try {
				WebappManager.start("help");
			} catch (Exception e) {
				exception = e;
			}
		}

		public Exception getException() {
			return exception;
		}
	}

	@Test
	public void testServerWithoutInterrupt() throws Exception {
		WebappManager.stop("help");
		startServerWithoutInterrupt();
		checkServer();
		WebappManager.stop("help");
	}

	@Test
	public void testServerWithInterrupt() throws Exception {
		WebappManager.stop("help");
		startServerWithInterrupt();
		checkServer();
		WebappManager.stop("help");
	}

	private void startServerWithoutInterrupt() throws Exception {
		ServerStarter starter = new ServerStarter();
		starter.start();
		// Now wait for the thread to complete
		iterations = 0;
		do {
			iterations++;
			if (enableTimeout && sleepTime * iterations > 10000) {
				fail("Test did not complete within 10 seconds");
			}
			Thread.sleep(sleepTime);
		} while (starter.isAlive());
		Exception exception = starter.getException();
		if (exception != null) {
			throw exception;
		}
	}

	private void startServerWithInterrupt() throws Exception {
		ServerStarter starter = new ServerStarter();
		starter.start();
		// Now wait for the thread to complete
		iterations = 0;
		do {
			iterations++;
			if (enableTimeout && sleepTime * iterations > 10000) {
				fail("Test did not complete within 10 seconds");
			}
			starter.interrupt();
			Thread.sleep(sleepTime);
		} while (starter.isAlive());
		Exception exception = starter.getException();
		if (exception != null) {
			throw exception;
		}
	}

	private void checkServer() throws Exception {
		long start = System.currentTimeMillis();
		try {
			int port = WebappManager.getPort();
			URL url = new URL("http", "localhost", port, "/help/index.jsp");
			URLConnection connection = url.openConnection();
			setTimeout(connection, 5000);
			try (InputStream input = connection.getInputStream()) {
				int firstbyte = input.read();
				Assert.assertTrue(firstbyte > 0);
			}
		} catch (Exception e) {
			long elapsed = System.currentTimeMillis() - start;
			System.out.println("Fail, milliseconds = " + elapsed);
			throw e;
		}
	}

	private static void setTimeout(URLConnection conn, int milliseconds) {
		conn.setConnectTimeout(milliseconds);
		conn.setReadTimeout(milliseconds);
	}

}

Back to the top