summaryrefslogtreecommitdiffstats
path: root/junit4/src/test/java/org/junit/tests/running/methods/TimeoutTest.java
blob: 777b0bdf89192071866e9238c1f181e3eba5e969 (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
package org.junit.tests.running.methods;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.matchers.JUnitMatchers.containsString;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import junit.framework.JUnit4TestAdapter;
import junit.framework.TestResult;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

public class TimeoutTest {
	
	static public class FailureWithTimeoutTest {
		@Test(timeout= 1000) public void failure() {
			fail();
		}
	}
	
	@Test public void failureWithTimeout() throws Exception {
		JUnitCore core= new JUnitCore();
		Result result= core.run(FailureWithTimeoutTest.class);
		assertEquals(1, result.getRunCount());
		assertEquals(1, result.getFailureCount());
		assertEquals(AssertionError.class, result.getFailures().get(0).getException().getClass());
	}

	static public class FailureWithTimeoutRunTimeExceptionTest {
		@Test(timeout= 1000) public void failure() {
			throw new NullPointerException();
		}
	}
	
	@Test public void failureWithTimeoutRunTimeException() throws Exception {
		JUnitCore core= new JUnitCore();
		Result result= core.run(FailureWithTimeoutRunTimeExceptionTest.class);
		assertEquals(1, result.getRunCount());
		assertEquals(1, result.getFailureCount());
		assertEquals(NullPointerException.class, result.getFailures().get(0).getException().getClass());
	}

	static public class SuccessWithTimeoutTest {
		@Test(timeout= 1000) public void success() {			
		}
	}
		
	@Test public void successWithTimeout() throws Exception {
		JUnitCore core= new JUnitCore();
		Result result= core.run(SuccessWithTimeoutTest.class);
		assertEquals(1, result.getRunCount());
		assertEquals(0, result.getFailureCount());
	}

	static public class TimeoutFailureTest {
		@Test(timeout= 100) public void success() throws InterruptedException {			
			Thread.sleep(40000);
		}
	}
	
	@Ignore("was breaking gump")
	@Test public void timeoutFailure() throws Exception {
		JUnitCore core= new JUnitCore();
		Result result= core.run(TimeoutFailureTest.class);
		assertEquals(1, result.getRunCount());
		assertEquals(1, result.getFailureCount());
		assertEquals(InterruptedException.class, result.getFailures().get(0).getException().getClass());
	}
	
	static public class InfiniteLoopTest {
		@Test(timeout= 100) public void failure() {
			infiniteLoop();
		}

		private void infiniteLoop() {
			for(;;)
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
				}
		}
	}
	
	@Test public void infiniteLoop() throws Exception {
		JUnitCore core= new JUnitCore();
		Result result= core.run(InfiniteLoopTest.class);
		assertEquals(1, result.getRunCount());
		assertEquals(1, result.getFailureCount());
		Throwable exception= result.getFailures().get(0).getException();
		assertTrue(exception.getMessage().contains("test timed out after 100 milliseconds"));
	}
	
	static public class ImpatientLoopTest {
		@Test(timeout= 1) public void failure() {
			infiniteLoop();
		}

		private void infiniteLoop() {
			for(;;);
		}
	}
	
	@Ignore("This breaks sporadically with time differences just slightly more than 200ms")
	@Test public void infiniteLoopRunsForApproximatelyLengthOfTimeout() throws Exception {
		// "prime the pump": running these beforehand makes the runtimes more predictable
		//                   (because of class loading?)
		JUnitCore.runClasses(InfiniteLoopTest.class, ImpatientLoopTest.class);
		long longTime= runAndTime(InfiniteLoopTest.class);
		long shortTime= runAndTime(ImpatientLoopTest.class);
		long difference= longTime - shortTime;
		assertTrue(String.format("Difference was %sms", difference), difference < 200);
	}

	private long runAndTime(Class<?> clazz) {
		JUnitCore core= new JUnitCore();
		long startTime= System.currentTimeMillis();
		core.run(clazz);
		long totalTime = System.currentTimeMillis() - startTime;
		return totalTime;
	}

	@Test public void stalledThreadAppearsInStackTrace() throws Exception {
		JUnitCore core= new JUnitCore();
		Result result= core.run(InfiniteLoopTest.class);
		assertEquals(1, result.getRunCount());
		assertEquals(1, result.getFailureCount());
		Throwable exception= result.getFailures().get(0).getException();
		Writer buffer= new StringWriter();
		PrintWriter writer= new PrintWriter(buffer);
		exception.printStackTrace(writer);
		assertThat(buffer.toString(), containsString("infiniteLoop")); // Make sure we have the stalled frame on the stack somewhere
	}

	@Test public void compatibility() {
		TestResult result= new TestResult();
		new JUnit4TestAdapter(InfiniteLoopTest.class).run(result);
		assertEquals(1, result.errorCount());
	}
	
	public static class WillTimeOut {
		static boolean afterWasCalled= false;
		
		@Test(timeout=1) public void test() {
			for(;;)
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					// ok, tests are over
				}
		}
		
		@After public void after() {
			afterWasCalled= true;
		}
	}
	
	@Test public void makeSureAfterIsCalledAfterATimeout() {
		JUnitCore.runClasses(WillTimeOut.class);
		assertThat(WillTimeOut.afterWasCalled, is(true));
	}
}