summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/java/io/InterruptedStreamTest.java
blob: e46df5dfdc09c6ccde596b12df90d76def05a555 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.java.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.Pipe;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;
import junit.framework.TestCase;

/**
 * Test that interrupting a thread blocked on I/O causes that thread to throw
 * an InterruptedIOException.
 */
public final class InterruptedStreamTest extends TestCase {

    private static final int BUFFER_SIZE = 1024 * 1024;

    private Socket[] sockets;

    @Override protected void setUp() throws Exception {
        Thread.interrupted(); // clear interrupted bit
        super.tearDown();
    }

    @Override protected void tearDown() throws Exception {
        if (sockets != null) {
            sockets[0].close();
            sockets[1].close();
            sockets = null;
        }
        Thread.interrupted(); // clear interrupted bit
        super.tearDown();
    }

    public void testInterruptPipedInputStream() throws Exception {
        PipedOutputStream out = new PipedOutputStream();
        PipedInputStream in = new PipedInputStream(out);
        testInterruptInputStream(in);
    }

    public void testInterruptPipedOutputStream() throws Exception {
        PipedOutputStream out = new PipedOutputStream();
        new PipedInputStream(out);
        testInterruptOutputStream(out);
    }

    public void testInterruptPipedReader() throws Exception {
        PipedWriter writer = new PipedWriter();
        PipedReader reader = new PipedReader(writer);
        testInterruptReader(reader);
    }

    public void testInterruptPipedWriter() throws Exception {
        final PipedWriter writer = new PipedWriter();
        new PipedReader(writer);
        testInterruptWriter(writer);
    }

    public void testInterruptReadablePipeChannel() throws Exception {
        testInterruptReadableChannel(Pipe.open().source());
    }

    public void testInterruptWritablePipeChannel() throws Exception {
        testInterruptWritableChannel(Pipe.open().sink());
    }

    public void testInterruptReadableSocketChannel() throws Exception {
        sockets = newSocketChannelPair();
        testInterruptReadableChannel(sockets[0].getChannel());
    }

    public void testInterruptWritableSocketChannel() throws Exception {
        sockets = newSocketChannelPair();
        testInterruptWritableChannel(sockets[0].getChannel());
    }

    /**
     * Returns a pair of connected sockets backed by NIO socket channels.
     */
    private Socket[] newSocketChannelPair() throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(0));
        SocketChannel clientSocketChannel = SocketChannel.open();
        clientSocketChannel.connect(serverSocketChannel.socket().getLocalSocketAddress());
        SocketChannel server = serverSocketChannel.accept();
        serverSocketChannel.close();
        return new Socket[] { clientSocketChannel.socket(), server.socket() };
    }

    private void testInterruptInputStream(final InputStream in) throws Exception {
        Thread thread = interruptMeLater();
        try {
            in.read();
            fail();
        } catch (InterruptedIOException expected) {
        } finally {
            waitForInterrupt(thread);
        }
    }

    private void testInterruptReader(final PipedReader reader) throws Exception {
        Thread thread = interruptMeLater();
        try {
            reader.read();
            fail();
        } catch (InterruptedIOException expected) {
        } finally {
            waitForInterrupt(thread);
        }
    }

    private void testInterruptReadableChannel(final ReadableByteChannel channel) throws Exception {
        Thread thread = interruptMeLater();
        try {
            channel.read(ByteBuffer.allocate(BUFFER_SIZE));
            fail();
        } catch (ClosedByInterruptException expected) {
        } finally {
            waitForInterrupt(thread);
        }
    }

    private void testInterruptOutputStream(final OutputStream out) throws Exception {
        Thread thread = interruptMeLater();
        try {
            // this will block when the receiving buffer fills up
            while (true) {
                out.write(new byte[BUFFER_SIZE]);
            }
        } catch (InterruptedIOException expected) {
        } finally {
            waitForInterrupt(thread);
        }
    }

    private void testInterruptWriter(final PipedWriter writer) throws Exception {
        Thread thread = interruptMeLater();
        try {
            // this will block when the receiving buffer fills up
            while (true) {
                writer.write(new char[BUFFER_SIZE]);
            }
        } catch (InterruptedIOException expected) {
        } finally {
            waitForInterrupt(thread);
        }
    }

    private void testInterruptWritableChannel(final WritableByteChannel channel) throws Exception {
        Thread thread = interruptMeLater();
        try {
            // this will block when the receiving buffer fills up
            while (true) {
                channel.write(ByteBuffer.allocate(BUFFER_SIZE));
            }
        } catch (ClosedByInterruptException expected) {
        } catch (ClosedChannelException expected) {
        } finally {
            waitForInterrupt(thread);
        }
    }

    private Thread interruptMeLater() throws Exception {
        final Thread toInterrupt = Thread.currentThread();
        Thread thread = new Thread(new Runnable () {
            @Override public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                }
                toInterrupt.interrupt();
            }
        });
        thread.start();
        return thread;
    }

    private static void waitForInterrupt(Thread thread) throws Exception {
        try {
            thread.join();
        } catch (InterruptedException ignore) {
            // There is currently a race between Thread.interrupt in
            // interruptMeLater and Thread.join here. Most of the time
            // we won't get an InterruptedException, but occasionally
            // we do, so for now ignore this exception.
            // http://b/6951157
        }
    }
}