J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @bug 4669040 |
| 26 | * @summary Test DatagramChannel receive with empty buffer |
| 27 | * @author Mike McCloskey |
| 28 | */ |
| 29 | |
| 30 | import java.io.*; |
| 31 | import java.net.*; |
| 32 | import java.nio.*; |
| 33 | import java.nio.channels.*; |
| 34 | import java.nio.charset.*; |
| 35 | |
| 36 | public class EmptyBuffer { |
| 37 | |
| 38 | static PrintStream log = System.err; |
| 39 | |
| 40 | public static void main(String[] args) throws Exception { |
| 41 | test(); |
| 42 | } |
| 43 | |
| 44 | static void test() throws Exception { |
| 45 | Sprintable server = new Server(); |
| 46 | Thread serverThread = new Thread(server); |
| 47 | serverThread.start(); |
| 48 | while (!server.ready()) |
| 49 | Thread.sleep(50); |
| 50 | DatagramChannel dc = DatagramChannel.open(); |
| 51 | ByteBuffer bb = ByteBuffer.allocateDirect(12); |
| 52 | bb.order(ByteOrder.BIG_ENDIAN); |
| 53 | bb.putInt(1).putLong(1); |
| 54 | bb.flip(); |
| 55 | InetAddress address = InetAddress.getLocalHost(); |
| 56 | InetSocketAddress isa = new InetSocketAddress(address, 8888); |
| 57 | dc.connect(isa); |
| 58 | dc.write(bb); |
| 59 | bb.rewind(); |
| 60 | dc.write(bb); |
| 61 | bb.rewind(); |
| 62 | dc.write(bb); |
| 63 | Thread.sleep(2000); |
| 64 | serverThread.interrupt(); |
| 65 | server.throwException(); |
| 66 | } |
| 67 | |
| 68 | public interface Sprintable extends Runnable { |
| 69 | public void throwException() throws Exception; |
| 70 | public boolean ready(); |
| 71 | } |
| 72 | |
| 73 | public static class Server implements Sprintable { |
| 74 | Exception e = null; |
| 75 | private volatile boolean ready = false; |
| 76 | |
| 77 | public void throwException() throws Exception { |
| 78 | if (e != null) |
| 79 | throw e; |
| 80 | } |
| 81 | |
| 82 | public boolean ready() { |
| 83 | return ready; |
| 84 | } |
| 85 | |
| 86 | void showBuffer(String s, ByteBuffer bb) { |
| 87 | log.println(s); |
| 88 | bb.rewind(); |
| 89 | for (int i=0; i<bb.limit(); i++) { |
| 90 | byte element = bb.get(); |
| 91 | log.print(element); |
| 92 | } |
| 93 | log.println(); |
| 94 | } |
| 95 | |
| 96 | public void run() { |
| 97 | SocketAddress sa = null; |
| 98 | int numberReceived = 0; |
| 99 | try { |
| 100 | DatagramChannel dc = DatagramChannel.open(); |
| 101 | dc.socket().bind(new InetSocketAddress(8888)); |
| 102 | ready = true; |
| 103 | ByteBuffer bb = ByteBuffer.allocateDirect(12); |
| 104 | bb.clear(); |
| 105 | // Only one clear. The buffer will be full after |
| 106 | // the first receive, but it should still block |
| 107 | // and receive and discard the next two |
| 108 | while (!Thread.interrupted()) { |
| 109 | try { |
| 110 | sa = dc.receive(bb); |
| 111 | } catch (ClosedByInterruptException cbie) { |
| 112 | // Expected |
| 113 | log.println("Took expected exit"); |
| 114 | break; |
| 115 | } |
| 116 | if (sa != null) { |
| 117 | log.println("Client: " + sa); |
| 118 | showBuffer("RECV", bb); |
| 119 | sa = null; |
| 120 | numberReceived++; |
| 121 | if (numberReceived > 3) |
| 122 | throw new RuntimeException("Test failed"); |
| 123 | } |
| 124 | } |
| 125 | } catch (Exception ex) { |
| 126 | e = ex; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | } |