1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  @test
  26  @summary Test to make sure kqueues work in 64-bit. We create two socket channels and write to them.
  27  @summary We then use a Selector on those two channels and make sure select() returns us a non-zero value.
  28  @summary com.apple.junit.java.nio.Selector
  29  @run main Kqueues64bitTest_R4258155
  30  */
  31 
  32 import junit.framework.*;
  33 
  34 import java.io.IOException;
  35 import java.net.*;
  36 import java.nio.channels.*;
  37 import java.util.Set;
  38 
  39 public class Kqueues64bitTest_R4258155 extends TestCase
  40 {
  41 public static Test suite() {
  42 return new TestSuite( Kqueues64bitTest_R4258155.class);
  43 }
  44 
  45 public static void main( String[] args ) {
  46 junit.textui.TestRunner.run( suite() );
  47 }
  48 
  49 public static final int PORT = 10000;
  50 
  51 static Object lock = new Object();
  52 static volatile boolean startTest = false;
  53 static volatile boolean parttwo = false;
  54 
  55 public void test64bitKqueues() throws Exception {
  56 String preferSelect = System.getProperty("java.nio.preferSelect", "false");
  57 if (preferSelect.equals("true")) {
  58 System.err.println("WARNING! java.nio.preferSelect=true! We're not using kqueues. Test results could be incorrect!");
  59 }
  60 
  61 Selector selector = Selector.open();
  62 
  63 ServerThread st = new ServerThread();
  64 st.start();
  65 
  66 synchronized (Kqueues64bitTest_R4258155.lock) {
  67 while (startTest == false)
  68 Kqueues64bitTest_R4258155.lock.wait();
  69 }
  70 
  71 SocketChannel channel1 = SocketChannel.open(new InetSocketAddress(InetAddress.getLocalHost(), Kqueues64bitTest_R4258155.PORT));
  72 SocketChannel channel2 = SocketChannel.open(new InetSocketAddress(InetAddress.getLocalHost(), Kqueues64bitTest_R4258155.PORT));
  73 channel1.configureBlocking(false);
  74 channel2.configureBlocking(false);
  75 
  76 SelectionKey channel1Key = channel1.register(selector, channel1.validOps());
  77 SelectionKey channel2Key = channel2.register(selector, channel2.validOps());
  78 
  79 synchronized (Kqueues64bitTest_R4258155.lock) {
  80 while (Kqueues64bitTest_R4258155.parttwo == false)
  81 Kqueues64bitTest_R4258155.lock.wait();
  82 }
  83 
  84 int count = selector.select();
  85 assertFalse("Should get at least one selected channel", (count==0));
  86 // System.out.println("selector.select() returned : " + count);
  87 Set <SelectionKey> selectedKeys = selector.selectedKeys();
  88 for (SelectionKey key : selectedKeys) {
  89 boolean IKnowTheKey = key.equals(channel1Key) || key.equals(channel2Key);
  90 assertTrue("Got a key I know nothing about", IKnowTheKey);
  91 
  92 }
  93 selector.close();
  94 }
  95 }
  96 
  97 class ServerThread extends Thread
  98 {
  99 public void run() {
 100 try {
 101 ServerSocket server = new ServerSocket(Kqueues64bitTest_R4258155.PORT);
 102 
 103 synchronized (Kqueues64bitTest_R4258155.lock) {
 104 Kqueues64bitTest_R4258155.startTest = true;
 105 Kqueues64bitTest_R4258155.lock.notify();
 106 }
 107 
 108 // Wait for two connections
 109 Socket conn1 = server.accept();
 110 Socket conn2 = server.accept();
 111 
 112 
 113 conn1.getOutputStream().write(61);
 114 conn2.getOutputStream().write(62);
 115 
 116 synchronized (Kqueues64bitTest_R4258155.lock) {
 117 Kqueues64bitTest_R4258155.parttwo = true;
 118 Kqueues64bitTest_R4258155.lock.notify();
 119 }
 120 
 121 server.close();
 122 } catch (IOException ioe) {
 123 ioe.printStackTrace();
 124 }
 125 }
 126 }