|
18 | 18 | package org.apache.commons.io.input; |
19 | 19 |
|
20 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
21 | 22 | import static org.junit.jupiter.api.Assertions.assertThrowsExactly; |
22 | 23 | import static org.junit.jupiter.api.Assertions.assertTrue; |
23 | 24 |
|
24 | 25 | import java.io.IOException; |
25 | 26 | import java.io.InputStream; |
26 | 27 | import java.time.Duration; |
| 28 | +import java.time.temporal.ChronoUnit; |
27 | 29 | import java.util.concurrent.atomic.AtomicBoolean; |
28 | 30 |
|
29 | 31 | import org.apache.commons.io.IOUtils; |
| 32 | +import org.apache.commons.io.input.ThrottledInputStream.Builder; |
30 | 33 | import org.apache.commons.io.test.CustomIOException; |
31 | 34 | import org.junit.jupiter.api.Test; |
32 | 35 |
|
@@ -56,35 +59,115 @@ public void testAfterReadConsumer() throws Exception { |
56 | 59 | // Throwing |
57 | 60 | final String message = "test exception message"; |
58 | 61 | // @formatter:off |
59 | | - try (InputStream bounded = ThrottledInputStream.builder() |
| 62 | + try (InputStream inputStream = ThrottledInputStream.builder() |
60 | 63 | .setCharSequence("Hi") |
61 | 64 | .setAfterRead(i -> { |
62 | 65 | throw new CustomIOException(message); |
63 | 66 | }) |
64 | 67 | .get()) { |
65 | | - assertEquals(message, assertThrowsExactly(CustomIOException.class, () -> IOUtils.consume(bounded)).getMessage()); |
| 68 | + assertEquals(message, assertThrowsExactly(CustomIOException.class, () -> IOUtils.consume(inputStream)).getMessage()); |
| 69 | + } |
| 70 | + // @formatter:on |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testBuilder() throws IOException { |
| 75 | + final Builder builder = ThrottledInputStream.builder(); |
| 76 | + assertThrows(IllegalArgumentException.class, () -> builder.setMaxBytesPerSecond(-1)); |
| 77 | + assertThrows(IllegalArgumentException.class, () -> builder.setMaxBytesPerSecond(0)); |
| 78 | + assertThrows(IllegalArgumentException.class, () -> builder.setMaxBytes(1, Duration.ZERO.minusMillis(1))); |
| 79 | + assertThrows(IllegalArgumentException.class, () -> builder.setMaxBytes(1, Duration.ZERO)); |
| 80 | + assertThrows(NullPointerException.class, () -> builder.setMaxBytes(1, (Duration) null)); |
| 81 | + assertThrows(NullPointerException.class, () -> builder.setMaxBytes(1, (ChronoUnit) null)); |
| 82 | + // |
| 83 | + // 2 bytes per second |
| 84 | + builder.setMaxBytesPerSecond(2); |
| 85 | + assertEquals(2.0, builder.getMaxBytesPerSecond()); |
| 86 | + // @formatter:off |
| 87 | + try (ThrottledInputStream inputStream = builder |
| 88 | + .setInputStream(createOriginInputStream()) |
| 89 | + .get()) { |
| 90 | + assertEquals(2.0, builder.getMaxBytesPerSecond()); |
| 91 | + assertEquals(2.0, inputStream.getMaxBytesPerSecond()); |
| 92 | + } |
| 93 | + try (ThrottledInputStream inputStream = builder |
| 94 | + .setInputStream(createOriginInputStream()) |
| 95 | + .setMaxBytes(2, ChronoUnit.SECONDS) |
| 96 | + .get()) { |
| 97 | + assertEquals(2.0, builder.getMaxBytesPerSecond()); |
| 98 | + assertEquals(2.0, inputStream.getMaxBytesPerSecond()); |
| 99 | + } |
| 100 | + // @formatter:on |
| 101 | + Duration maxBytesPer = Duration.ofSeconds(1); |
| 102 | + // @formatter:off |
| 103 | + try (ThrottledInputStream inputStream = builder |
| 104 | + .setInputStream(createOriginInputStream()) |
| 105 | + .setMaxBytes(2, maxBytesPer) |
| 106 | + .get()) { |
| 107 | + assertEquals(2.0, builder.getMaxBytesPerSecond()); |
| 108 | + assertEquals(2.0, inputStream.getMaxBytesPerSecond()); |
| 109 | + } |
| 110 | + // |
| 111 | + // 1 bytes per 1/2 second (30_000 millis) |
| 112 | + // @formatter:on |
| 113 | + maxBytesPer = maxBytesPer.dividedBy(2); |
| 114 | + // @formatter:off |
| 115 | + try (ThrottledInputStream inputStream = builder |
| 116 | + .setInputStream(createOriginInputStream()) |
| 117 | + .setMaxBytes(1, maxBytesPer) |
| 118 | + .get()) { |
| 119 | + assertEquals(0.5, inputStream.getMaxBytesPerSecond()); |
| 120 | + } |
| 121 | + // 1 byte/millis |
| 122 | + try (ThrottledInputStream inputStream = builder |
| 123 | + .setInputStream(createOriginInputStream()) |
| 124 | + .setMaxBytes(1, ChronoUnit.MILLIS) |
| 125 | + .get()) { |
| 126 | + assertEquals(0.001, inputStream.getMaxBytesPerSecond()); |
| 127 | + } |
| 128 | + // @formatter:on |
| 129 | + // 1 byte per 10_0011 millis. |
| 130 | + maxBytesPer = Duration.ofSeconds(20).plusMillis(11); |
| 131 | + // @formatter:off |
| 132 | + try (ThrottledInputStream inputStream = builder |
| 133 | + .setInputStream(createOriginInputStream()) |
| 134 | + .setMaxBytes(1, maxBytesPer) |
| 135 | + .get()) { |
| 136 | + assertEquals(20.011, inputStream.getMaxBytesPerSecond()); |
| 137 | + } |
| 138 | + // @formatter:on |
| 139 | + // Javadoc example |
| 140 | + // @formatter:off |
| 141 | + try (ThrottledInputStream inputStream = builder |
| 142 | + .setInputStream(createOriginInputStream()) |
| 143 | + .setMaxBytes(100_000, ChronoUnit.SECONDS) |
| 144 | + .get()) { |
| 145 | + assertEquals(100_000.0, inputStream.getMaxBytesPerSecond()); |
66 | 146 | } |
67 | 147 | // @formatter:on |
68 | 148 | } |
69 | 149 |
|
70 | 150 | @Test |
71 | 151 | public void testCalSleepTimeMs() { |
72 | 152 | // case 0: initial - no read, no sleep |
73 | | - assertEquals(0, ThrottledInputStream.toSleepMillis(0, 10_000, 1_000)); |
74 | | - |
| 153 | + assertEquals(0, ThrottledInputStream.toSleepMillis(0, 1_000, 10_000)); |
75 | 154 | // case 1: no threshold |
76 | | - assertEquals(0, ThrottledInputStream.toSleepMillis(Long.MAX_VALUE, 0, 1_000)); |
77 | | - assertEquals(0, ThrottledInputStream.toSleepMillis(Long.MAX_VALUE, -1, 1_000)); |
78 | | - |
| 155 | + assertEquals(0, ThrottledInputStream.toSleepMillis(Long.MAX_VALUE, 1_000, 0)); |
| 156 | + assertEquals(0, ThrottledInputStream.toSleepMillis(Long.MAX_VALUE, 1_000, -1)); |
79 | 157 | // case 2: too fast |
80 | | - assertEquals(1500, ThrottledInputStream.toSleepMillis(5, 2, 1_000)); |
81 | | - assertEquals(500, ThrottledInputStream.toSleepMillis(5, 2, 2_000)); |
82 | | - assertEquals(6500, ThrottledInputStream.toSleepMillis(15, 2, 1_000)); |
83 | | - |
84 | | - // case 3: too slow |
85 | | - assertEquals(0, ThrottledInputStream.toSleepMillis(1, 2, 1_000)); |
86 | | - assertEquals(0, ThrottledInputStream.toSleepMillis(2, 2, 2_000)); |
87 | | - assertEquals(0, ThrottledInputStream.toSleepMillis(1, 2, 1_000)); |
| 158 | + assertEquals(1500, ThrottledInputStream.toSleepMillis(5, 1_000, 2)); |
| 159 | + assertEquals(500, ThrottledInputStream.toSleepMillis(5, 2_000, 2)); |
| 160 | + assertEquals(6500, ThrottledInputStream.toSleepMillis(15, 1_000, 2)); |
| 161 | + assertEquals(4000, ThrottledInputStream.toSleepMillis(5, 1_000, 1)); |
| 162 | + assertEquals(9000, ThrottledInputStream.toSleepMillis(5, 1_000, 0.5)); |
| 163 | + assertEquals(99000, ThrottledInputStream.toSleepMillis(5, 1_000, 0.05)); |
| 164 | + // case 3: too slow, no sleep needed |
| 165 | + assertEquals(0, ThrottledInputStream.toSleepMillis(1, 1_000, 2)); |
| 166 | + assertEquals(0, ThrottledInputStream.toSleepMillis(2, 2_000, 2)); |
| 167 | + assertEquals(0, ThrottledInputStream.toSleepMillis(1, 1_000, 2)); |
| 168 | + assertEquals(0, ThrottledInputStream.toSleepMillis(1, 1_000, 2.0)); |
| 169 | + assertEquals(0, ThrottledInputStream.toSleepMillis(1, 1_000, 1)); |
| 170 | + assertEquals(0, ThrottledInputStream.toSleepMillis(1, 1_000, 1.0)); |
88 | 171 | } |
89 | 172 |
|
90 | 173 | @Test |
|
0 commit comments