View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.util.io;
45  
46  import static org.junit.Assert.assertArrayEquals;
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.assertFalse;
49  import static org.junit.Assert.assertTrue;
50  import static org.junit.Assert.fail;
51  
52  import java.io.IOException;
53  import java.io.InterruptedIOException;
54  import java.io.PipedInputStream;
55  import java.io.PipedOutputStream;
56  import java.util.Arrays;
57  import java.util.List;
58  
59  import org.eclipse.jgit.util.IO;
60  import org.junit.After;
61  import org.junit.Before;
62  import org.junit.Test;
63  
64  public class TimeoutInputStreamTest {
65  	private static final int timeout = 250;
66  
67  	private PipedOutputStream out;
68  
69  	private PipedInputStream in;
70  
71  	private InterruptTimer timer;
72  
73  	private TimeoutInputStream is;
74  
75  	private long start;
76  
77  	@Before
78  	public void setUp() throws Exception {
79  		out = new PipedOutputStream();
80  		in = new PipedInputStream(out);
81  		timer = new InterruptTimer();
82  		is = new TimeoutInputStream(in, timer);
83  		is.setTimeout(timeout);
84  	}
85  
86  	@After
87  	public void tearDown() throws Exception {
88  		timer.terminate();
89  		for (Thread t : active())
90  			assertFalse(t instanceof InterruptTimer.AlarmThread);
91  	}
92  
93  	@Test
94  	public void testTimeout_readByte_Success1() throws IOException {
95  		out.write('a');
96  		assertEquals('a', is.read());
97  	}
98  
99  	@Test
100 	public void testTimeout_readByte_Success2() throws IOException {
101 		final byte[] exp = new byte[] { 'a', 'b', 'c' };
102 		out.write(exp);
103 		assertEquals(exp[0], is.read());
104 		assertEquals(exp[1], is.read());
105 		assertEquals(exp[2], is.read());
106 		out.close();
107 		assertEquals(-1, is.read());
108 	}
109 
110 	@Test
111 	public void testTimeout_readByte_Timeout() throws IOException {
112 		beginRead();
113 		try {
114 			is.read();
115 			fail("incorrectly read a byte");
116 		} catch (InterruptedIOException e) {
117 			// expected
118 		}
119 		assertTimeout();
120 	}
121 
122 	@Test
123 	public void testTimeout_readBuffer_Success1() throws IOException {
124 		final byte[] exp = new byte[] { 'a', 'b', 'c' };
125 		final byte[] act = new byte[exp.length];
126 		out.write(exp);
127 		IO.readFully(is, act, 0, act.length);
128 		assertArrayEquals(exp, act);
129 	}
130 
131 	@Test
132 	public void testTimeout_readBuffer_Success2() throws IOException {
133 		final byte[] exp = new byte[] { 'a', 'b', 'c' };
134 		final byte[] act = new byte[exp.length];
135 		out.write(exp);
136 		IO.readFully(is, act, 0, 1);
137 		IO.readFully(is, act, 1, 1);
138 		IO.readFully(is, act, 2, 1);
139 		assertArrayEquals(exp, act);
140 	}
141 
142 	@Test
143 	public void testTimeout_readBuffer_Timeout() throws IOException {
144 		beginRead();
145 		try {
146 			IO.readFully(is, new byte[512], 0, 512);
147 			fail("incorrectly read bytes");
148 		} catch (InterruptedIOException e) {
149 			// expected
150 		}
151 		assertTimeout();
152 	}
153 
154 	@Test
155 	public void testTimeout_skip_Success() throws IOException {
156 		final byte[] exp = new byte[] { 'a', 'b', 'c' };
157 		out.write(exp);
158 		assertEquals(2, is.skip(2));
159 		assertEquals('c', is.read());
160 	}
161 
162 	@Test
163 	public void testTimeout_skip_Timeout() throws IOException {
164 		beginRead();
165 		try {
166 			is.skip(1024);
167 			fail("incorrectly skipped bytes");
168 		} catch (InterruptedIOException e) {
169 			// expected
170 		}
171 		assertTimeout();
172 	}
173 
174 	private void beginRead() {
175 		start = now();
176 	}
177 
178 	private void assertTimeout() {
179 		// Our timeout was supposed to be ~250 ms. Since this is a timing
180 		// test we can't assume we spent *exactly* the timeout period, as
181 		// there may be other activity going on in the system. Instead we
182 		// look for the delta between the start and end times to be within
183 		// 50 ms of the expected timeout.
184 		//
185 		final long wait = now() - start;
186 		assertTrue("waited only " + wait + " ms", timeout - wait < 50);
187 	}
188 
189 	private static List<Thread> active() {
190 		Thread[] all = new Thread[16];
191 		int n = Thread.currentThread().getThreadGroup().enumerate(all);
192 		while (n == all.length) {
193 			all = new Thread[all.length * 2];
194 			n = Thread.currentThread().getThreadGroup().enumerate(all);
195 		}
196 		return Arrays.asList(all).subList(0, n);
197 	}
198 
199 	private static long now() {
200 		return System.currentTimeMillis();
201 	}
202 }