1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.io.input;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import java.io.IOException;
23 import java.time.Duration;
24
25 import org.junit.jupiter.api.Test;
26
27
28
29
30 public class ThrottledInputStreamTest extends ProxyInputStreamTest<ThrottledInputStream> {
31
32 @Override
33 @SuppressWarnings("resource")
34 protected ThrottledInputStream createFixture() throws IOException {
35 return ThrottledInputStream.builder().setInputStream(createProxySource()).get();
36 }
37
38 @Test
39 public void testCalSleepTimeMs() {
40
41 assertEquals(0, ThrottledInputStream.toSleepMillis(0, 10_000, 1_000));
42
43
44 assertEquals(0, ThrottledInputStream.toSleepMillis(Long.MAX_VALUE, 0, 1_000));
45 assertEquals(0, ThrottledInputStream.toSleepMillis(Long.MAX_VALUE, -1, 1_000));
46
47
48 assertEquals(1500, ThrottledInputStream.toSleepMillis(5, 2, 1_000));
49 assertEquals(500, ThrottledInputStream.toSleepMillis(5, 2, 2_000));
50 assertEquals(6500, ThrottledInputStream.toSleepMillis(15, 2, 1_000));
51
52
53 assertEquals(0, ThrottledInputStream.toSleepMillis(1, 2, 1_000));
54 assertEquals(0, ThrottledInputStream.toSleepMillis(2, 2, 2_000));
55 assertEquals(0, ThrottledInputStream.toSleepMillis(1, 2, 1_000));
56 }
57
58 @Override
59 protected void testEos(final ThrottledInputStream inputStream) {
60 assertEquals(3, inputStream.getByteCount());
61 }
62
63 @Test
64 public void testGet() throws IOException {
65 try (ThrottledInputStream inputStream = createFixture()) {
66 inputStream.read();
67 assertEquals(Duration.ZERO, inputStream.getTotalSleepDuration());
68 }
69 }
70
71 }