test/java/nio/channels/DatagramChannel/EmptyBuffer.java

Print this page
rev 12172 : 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
Summary: Only count datagrams received from the client within the test.
Reviewed-by: XXX

*** 1,7 **** /* ! * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 20,39 **** * or visit www.oracle.com if you need additional information or have any * questions. */ /* @test ! * @bug 4669040 * @summary Test DatagramChannel receive with empty buffer * @author Mike McCloskey */ import java.io.*; import java.net.*; import java.nio.*; import java.nio.channels.*; - import java.nio.charset.*; public class EmptyBuffer { static PrintStream log = System.err; --- 20,38 ---- * or visit www.oracle.com if you need additional information or have any * questions. */ /* @test ! * @bug 4669040 8087304 * @summary Test DatagramChannel receive with empty buffer * @author Mike McCloskey */ import java.io.*; import java.net.*; import java.nio.*; import java.nio.channels.*; public class EmptyBuffer { static PrintStream log = System.err;
*** 52,61 **** --- 51,61 ---- bb.putInt(1).putLong(1); bb.flip(); InetAddress address = InetAddress.getLocalHost(); InetSocketAddress isa = new InetSocketAddress(address, server.port()); dc.connect(isa); + server.setClientAddress(dc.getLocalAddress()); dc.write(bb); bb.rewind(); dc.write(bb); bb.rewind(); dc.write(bb);
*** 67,82 **** --- 67,87 ---- } } public static class Server implements Runnable { final DatagramChannel dc; + private SocketAddress clientAddress; Exception e = null; Server() throws IOException { this.dc = DatagramChannel.open().bind(new InetSocketAddress(0)); } + void setClientAddress(SocketAddress clientAddress) { + this.clientAddress = clientAddress; + } + int port() { return dc.socket().getLocalPort(); } void throwException() throws Exception {
*** 92,103 **** log.print(element); } log.println(); } public void run() { ! SocketAddress sa = null; int numberReceived = 0; try { ByteBuffer bb = ByteBuffer.allocateDirect(12); bb.clear(); // Only one clear. The buffer will be full after --- 97,109 ---- log.print(element); } log.println(); } + @Override public void run() { ! SocketAddress sa; int numberReceived = 0; try { ByteBuffer bb = ByteBuffer.allocateDirect(12); bb.clear(); // Only one clear. The buffer will be full after
*** 107,125 **** try { sa = dc.receive(bb); } catch (ClosedByInterruptException cbie) { // Expected log.println("Took expected exit"); break; } if (sa != null) { log.println("Client: " + sa); showBuffer("RECV", bb); - sa = null; numberReceived++; if (numberReceived > 3) ! throw new RuntimeException("Test failed"); } } } catch (Exception ex) { e = ex; } finally { --- 113,137 ---- try { sa = dc.receive(bb); } catch (ClosedByInterruptException cbie) { // Expected log.println("Took expected exit"); + // Verify that enough packets were received + if (numberReceived != 3) + throw new RuntimeException("Failed: Too few datagrams"); break; } if (sa != null) { log.println("Client: " + sa); + // Check client address so as not to count stray packets + if (sa.equals(clientAddress)) { showBuffer("RECV", bb); numberReceived++; + } if (numberReceived > 3) ! throw new RuntimeException("Failed: Too many datagrams"); ! sa = null; } } } catch (Exception ex) { e = ex; } finally {