ServerSocketTest.java revision 49ff8b8582bca20a1adbda1d957220526332a8ca
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.net;
18
19import java.io.IOException;
20import java.net.ServerSocket;
21import java.net.Socket;
22
23public class ServerSocketTest extends junit.framework.TestCase {
24    public void testTimeoutAfterAccept() throws Exception {
25        final ServerSocket ss = new ServerSocket(0);
26        ss.setReuseAddress(true);
27        // On Unix, the receive timeout is inherited by the result of accept(2).
28        // Java specifies that it should always be 0 instead.
29        ss.setSoTimeout(1234);
30        final Socket[] result = new Socket[1];
31        Thread t = new Thread(new Runnable() {
32            public void run() {
33                try {
34                    result[0] = ss.accept();
35                } catch (IOException ex) {
36                    ex.printStackTrace();
37                    fail();
38                }
39            }
40        });
41        t.start();
42        new Socket(ss.getInetAddress(), ss.getLocalPort());
43        t.join();
44        assertEquals(0, result[0].getSoTimeout());
45    }
46}
47