Skip to main content
summaryrefslogtreecommitdiffstats
blob: f4dc47b6ec22b7570aa34eb94338056413c1840b (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
/*******************************************************************************
 * Copyright (c) 2009, 2013 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

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

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

import junit.framework.TestCase;

import org.junit.Assert;

import org.eclipse.help.internal.server.WebappManager;

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

public class HelpServerInterrupt extends TestCase {
	
	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;
		}
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
	}

	public void testServerWithoutInterrupt() throws Exception {
		WebappManager.stop("help");
		startServerWithoutInterrupt();
		checkServer();
		WebappManager.stop("help");
	}	
	
	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 {
		InputStream input;
		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);
			input = connection.getInputStream();
			int firstbyte = input.read();
			Assert.assertTrue(firstbyte > 0);
			input.close();
		} 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