You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimeoutInputStreamTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. package org.eclipse.jgit.util.io;
  44. import static org.junit.Assert.assertArrayEquals;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.IOException;
  50. import java.io.InterruptedIOException;
  51. import java.io.PipedInputStream;
  52. import java.io.PipedOutputStream;
  53. import java.util.Arrays;
  54. import java.util.List;
  55. import org.eclipse.jgit.util.IO;
  56. import org.junit.After;
  57. import org.junit.Before;
  58. import org.junit.Test;
  59. public class TimeoutInputStreamTest {
  60. private static final int timeout = 250;
  61. private PipedOutputStream out;
  62. private PipedInputStream in;
  63. private InterruptTimer timer;
  64. private TimeoutInputStream is;
  65. private long start;
  66. @Before
  67. public void setUp() throws Exception {
  68. out = new PipedOutputStream();
  69. in = new PipedInputStream(out);
  70. timer = new InterruptTimer();
  71. is = new TimeoutInputStream(in, timer);
  72. is.setTimeout(timeout);
  73. }
  74. @After
  75. public void tearDown() throws Exception {
  76. timer.terminate();
  77. for (Thread t : active())
  78. assertFalse(t instanceof InterruptTimer.AlarmThread);
  79. }
  80. @Test
  81. public void testTimeout_readByte_Success1() throws IOException {
  82. out.write('a');
  83. assertEquals('a', is.read());
  84. }
  85. @Test
  86. public void testTimeout_readByte_Success2() throws IOException {
  87. final byte[] exp = new byte[] { 'a', 'b', 'c' };
  88. out.write(exp);
  89. assertEquals(exp[0], is.read());
  90. assertEquals(exp[1], is.read());
  91. assertEquals(exp[2], is.read());
  92. out.close();
  93. assertEquals(-1, is.read());
  94. }
  95. @Test
  96. public void testTimeout_readByte_Timeout() throws IOException {
  97. beginRead();
  98. try {
  99. is.read();
  100. fail("incorrectly read a byte");
  101. } catch (InterruptedIOException e) {
  102. // expected
  103. }
  104. assertTimeout();
  105. }
  106. @Test
  107. public void testTimeout_readBuffer_Success1() throws IOException {
  108. final byte[] exp = new byte[] { 'a', 'b', 'c' };
  109. final byte[] act = new byte[exp.length];
  110. out.write(exp);
  111. IO.readFully(is, act, 0, act.length);
  112. assertArrayEquals(exp, act);
  113. }
  114. @Test
  115. public void testTimeout_readBuffer_Success2() throws IOException {
  116. final byte[] exp = new byte[] { 'a', 'b', 'c' };
  117. final byte[] act = new byte[exp.length];
  118. out.write(exp);
  119. IO.readFully(is, act, 0, 1);
  120. IO.readFully(is, act, 1, 1);
  121. IO.readFully(is, act, 2, 1);
  122. assertArrayEquals(exp, act);
  123. }
  124. @Test
  125. public void testTimeout_readBuffer_Timeout() throws IOException {
  126. beginRead();
  127. try {
  128. IO.readFully(is, new byte[512], 0, 512);
  129. fail("incorrectly read bytes");
  130. } catch (InterruptedIOException e) {
  131. // expected
  132. }
  133. assertTimeout();
  134. }
  135. @Test
  136. public void testTimeout_skip_Success() throws IOException {
  137. final byte[] exp = new byte[] { 'a', 'b', 'c' };
  138. out.write(exp);
  139. assertEquals(2, is.skip(2));
  140. assertEquals('c', is.read());
  141. }
  142. @Test
  143. public void testTimeout_skip_Timeout() throws IOException {
  144. beginRead();
  145. try {
  146. is.skip(1024);
  147. fail("incorrectly skipped bytes");
  148. } catch (InterruptedIOException e) {
  149. // expected
  150. }
  151. assertTimeout();
  152. }
  153. private void beginRead() {
  154. start = now();
  155. }
  156. private void assertTimeout() {
  157. // Our timeout was supposed to be ~250 ms. Since this is a timing
  158. // test we can't assume we spent *exactly* the timeout period, as
  159. // there may be other activity going on in the system. Instead we
  160. // look for the delta between the start and end times to be within
  161. // 50 ms of the expected timeout.
  162. //
  163. final long wait = now() - start;
  164. assertTrue("waited only " + wait + " ms", timeout - wait < 50);
  165. }
  166. private static List<Thread> active() {
  167. Thread[] all = new Thread[16];
  168. int n = Thread.currentThread().getThreadGroup().enumerate(all);
  169. while (n == all.length) {
  170. all = new Thread[all.length * 2];
  171. n = Thread.currentThread().getThreadGroup().enumerate(all);
  172. }
  173. return Arrays.asList(all).subList(0, n);
  174. }
  175. private static long now() {
  176. return System.currentTimeMillis();
  177. }
  178. }