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 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
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
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
170 }
171 assertTimeout();
172 }
173
174 private void beginRead() {
175 start = now();
176 }
177
178 private void assertTimeout() {
179
180
181
182
183
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 }