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 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InterruptedIOException;
31 import java.nio.file.StandardOpenOption;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Executors;
34 import java.util.concurrent.TimeUnit;
35
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.Timeout;
39
40
41
42
43
44 class ReadAheadInputStreamTest extends AbstractInputStreamTest {
45
46 @SuppressWarnings("resource")
47 @BeforeEach
48 void setUpInputStreams() throws IOException {
49 inputStreams = new InputStream[] {
50
51 new ReadAheadInputStream(new BufferedFileChannelInputStream(InputPath, 8 * 1024), 8 * 1024),
52
53 new ReadAheadInputStream(new BufferedFileChannelInputStream(InputPath, 3 * 1024), 2 * 1024),
54
55 new ReadAheadInputStream(new BufferedFileChannelInputStream(InputPath, 2 * 1024), 3 * 1024),
56
57 new ReadAheadInputStream(new BufferedFileChannelInputStream(InputPath, 321), 123),
58
59 new ReadAheadInputStream(new BufferedFileChannelInputStream(InputPath, 123), 321),
60
61
62 ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(InputPath, 8 * 1024)).setBufferSize(8 * 1024).get(),
63
64 ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(InputPath, 3 * 1024)).setBufferSize(2 * 1024).get(),
65
66 ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(InputPath, 2 * 1024)).setBufferSize(3 * 1024).get(),
67
68 ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(InputPath, 321)).setBufferSize(123).get(),
69
70 ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(InputPath, 123)).setBufferSize(321).get(),
71 ReadAheadInputStream.builder().setPath(InputPath).setOpenOptions(StandardOpenOption.READ).get() };
72 }
73
74 @Test
75 @Timeout(value = 30, unit = TimeUnit.SECONDS)
76 synchronized void testCloseInterrupt() throws IOException, InterruptedException {
77 try (ReadAheadInputStream inputStream = ReadAheadInputStream.builder()
78
79 .setPath(InputPath)
80 .get()) {
81
82 final ReadAheadInputStream spy = spy(inputStream);
83 when(spy.shutdownAwait()).thenThrow(InterruptedException.class);
84 Thread.currentThread().interrupt();
85 assertInstanceOf(InterruptedException.class, assertThrows(InterruptedIOException.class, spy::close).getCause());
86 assertTrue(Thread.interrupted());
87 }
88 }
89
90 @Test
91 void testClosePlusExecutorService() throws IOException {
92 final ExecutorService externalExecutor = Executors.newSingleThreadExecutor();
93
94 try (FileInputStream inputStream = new FileInputStream("src/test/resources/org/apache/commons/io/FileUtilsTestDataLF.bin")) {
95 try {
96 try (ReadAheadInputStream rais = ReadAheadInputStream.builder().setInputStream(inputStream).setExecutorService(externalExecutor).get()) {
97 assertEquals('1', rais.read());
98 }
99 Thread.yield();
100
101 assertThrows(IOException.class, inputStream::available);
102 assertThrows(IOException.class, inputStream::read);
103
104 } finally {
105
106 externalExecutor.shutdown();
107 }
108 }
109 }
110
111 }