• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  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 
17 package org.apache.harmony.tests.java.nio.channels;
18 
19 import java.net.InetSocketAddress;
20 import java.net.ServerSocket;
21 import java.nio.channels.SelectableChannel;
22 import java.nio.channels.SelectionKey;
23 import java.nio.channels.Selector;
24 import java.nio.channels.ServerSocketChannel;
25 import java.nio.channels.SocketChannel;
26 
27 import junit.framework.TestCase;
28 
29 public class UnixSelectorTest extends TestCase {
30     static class Server {
31         ServerSocketChannel serverChannel = ServerSocketChannel.open();
32         ServerSocket socket = null;
33 
Server()34         Server() throws Exception {
35             serverChannel.configureBlocking(false);
36         }
37 
initialize()38         public void initialize() throws Exception {
39             this.socket = serverChannel.socket();
40             socket.bind(null);
41         }
42 
accept()43         public void accept() {
44             Thread serverThread = new Thread(new Runnable() {
45                 public void run() {
46                     try {
47                         while (serverChannel.accept() == null) {
48                             Thread.sleep(1000);
49                         }
50                     } catch (Exception e) {}
51                 }
52             });
53             serverThread.start();
54         }
55 
close()56         public void close() throws Exception{
57             serverChannel.close();
58         }
59     }
60 
testSelectorAcceptAndRead()61     public void testSelectorAcceptAndRead() throws Exception {
62         Selector sel0 = Selector.open();
63         Selector sel1 = Selector.open();
64         Server server = new Server();
65         SelectionKey mkey0 = server.serverChannel.register(sel0, SelectionKey.OP_ACCEPT);
66         server.serverChannel.register(sel1, SelectionKey.OP_ACCEPT);
67 
68         // HUP is treating as acceptable
69         assertEquals(1, sel0.select(100));
70         assertEquals(true, sel0.selectedKeys().contains(mkey0));
71         server.initialize();
72         // after bind can not accept
73         assertEquals(0, sel1.select(100));
74         server.accept();
75         Thread.sleep(1000);
76         SocketChannel socketChannel = SocketChannel.open();
77         socketChannel.configureBlocking(false);
78         Selector sel2 = Selector.open();
79         socketChannel.register(sel2, SelectionKey.OP_WRITE);
80         boolean isConnected = socketChannel.connect(server.socket.getLocalSocketAddress());
81         if (!isConnected) {
82             socketChannel.finishConnect();
83         }
84 
85         assertEquals(true, socketChannel.isConnected());
86         server.close();
87         Thread.sleep(3000);
88         assertEquals(true, socketChannel.isConnected());
89         assertEquals(1, sel2.select(100));
90     }
91 
testSelectUnConnectedChannel()92     public void testSelectUnConnectedChannel() throws Exception {
93         SocketChannel socketChannel2 = SocketChannel.open();
94         socketChannel2.configureBlocking(false);
95         Selector sel3 = Selector.open();
96         SelectionKey mkey3 = socketChannel2.register(sel3, SelectionKey.OP_WRITE);
97         // HUP is also treating as writable
98         assertEquals(1, sel3.select(100));
99         assertEquals(false, mkey3.isConnectable());
100         // even the channel is not connected, the selector could be writable
101         assertEquals(false, socketChannel2.isConnected());
102         assertEquals(true, mkey3.isWritable());
103 
104         Selector sel4 = Selector.open();
105         SelectionKey mkey4 = socketChannel2.register(sel4, SelectionKey.OP_CONNECT);
106         assertEquals(1, sel4.select(100));
107         assertEquals(false, mkey4.isWritable());
108         assertEquals(true, mkey4.isConnectable());
109 
110         Selector sel5 = Selector.open();
111         SelectionKey mkey5 = socketChannel2.register(sel5, SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE);
112         assertEquals(1, sel5.select(100));
113         assertEquals(true, mkey5.isWritable());
114         assertEquals(true, mkey5.isConnectable());
115     }
116 }
117