8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3

Only count datagrams received from the client within the test.

Reviewed-by: alanb, chegar
This commit is contained in:
Brian Burkhalter 2015-06-16 14:38:51 -07:00
parent ded46b191f
commit 0bc308ad38

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -27,39 +27,50 @@
* @author Mike McCloskey * @author Mike McCloskey
*/ */
import java.io.*; import java.io.IOException;
import java.net.*; import java.io.PrintStream;
import java.nio.*; import java.net.InetAddress;
import java.nio.channels.*; import java.net.InetSocketAddress;
import java.nio.charset.*; import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.DatagramChannel;
public class EmptyBuffer { public class EmptyBuffer {
static PrintStream log = System.err; private static final PrintStream log = System.err;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
test(); test();
} }
static void test() throws Exception { private static void test() throws Exception {
Server server = new Server(); DatagramChannel dc = DatagramChannel.open();
InetAddress localHost = InetAddress.getLocalHost();
dc.bind(new InetSocketAddress(localHost, 0));
Server server = new Server(dc.getLocalAddress());
Thread serverThread = new Thread(server); Thread serverThread = new Thread(server);
serverThread.start(); serverThread.start();
DatagramChannel dc = DatagramChannel.open();
try { try {
InetSocketAddress isa = new InetSocketAddress(localHost, server.port());
dc.connect(isa);
ByteBuffer bb = ByteBuffer.allocateDirect(12); ByteBuffer bb = ByteBuffer.allocateDirect(12);
bb.order(ByteOrder.BIG_ENDIAN); bb.order(ByteOrder.BIG_ENDIAN);
bb.putInt(1).putLong(1); bb.putInt(1).putLong(1);
bb.flip(); bb.flip();
InetAddress address = InetAddress.getLocalHost();
InetSocketAddress isa = new InetSocketAddress(address, server.port());
dc.connect(isa);
dc.write(bb); dc.write(bb);
bb.rewind(); bb.rewind();
dc.write(bb); dc.write(bb);
bb.rewind(); bb.rewind();
dc.write(bb); dc.write(bb);
Thread.sleep(2000); Thread.sleep(2000);
serverThread.interrupt(); serverThread.interrupt();
server.throwException(); server.throwException();
} finally { } finally {
@ -67,12 +78,14 @@ public class EmptyBuffer {
} }
} }
public static class Server implements Runnable { private static class Server implements Runnable {
final DatagramChannel dc; private final DatagramChannel dc;
Exception e = null; private final SocketAddress clientAddress;
private Exception e = null;
Server() throws IOException { Server(SocketAddress clientAddress) throws IOException {
this.dc = DatagramChannel.open().bind(new InetSocketAddress(0)); this.dc = DatagramChannel.open().bind(new InetSocketAddress(0));
this.clientAddress = clientAddress;
} }
int port() { int port() {
@ -94,30 +107,37 @@ public class EmptyBuffer {
log.println(); log.println();
} }
@Override
public void run() { public void run() {
SocketAddress sa = null;
int numberReceived = 0;
try { try {
ByteBuffer bb = ByteBuffer.allocateDirect(12); ByteBuffer bb = ByteBuffer.allocateDirect(12);
bb.clear(); bb.clear();
// Only one clear. The buffer will be full after // Only one clear. The buffer will be full after
// the first receive, but it should still block // the first receive, but it should still block
// and receive and discard the next two // and receive and discard the next two
int numberReceived = 0;
while (!Thread.interrupted()) { while (!Thread.interrupted()) {
SocketAddress sa;
try { try {
sa = dc.receive(bb); sa = dc.receive(bb);
} catch (ClosedByInterruptException cbie) { } catch (ClosedByInterruptException cbie) {
// Expected // Expected
log.println("Took expected exit"); log.println("Took expected exit");
// Verify that enough packets were received
if (numberReceived != 3)
throw new RuntimeException("Failed: Too few datagrams");
break; break;
} }
if (sa != null) { if (sa != null) {
log.println("Client: " + sa); log.println("Client: " + sa);
showBuffer("RECV", bb); // Check client address so as not to count stray packets
sa = null; if (sa.equals(clientAddress)) {
numberReceived++; showBuffer("RECV", bb);
numberReceived++;
}
if (numberReceived > 3) if (numberReceived > 3)
throw new RuntimeException("Test failed"); throw new RuntimeException("Failed: Too many datagrams");
sa = null;
} }
} }
} catch (Exception ex) { } catch (Exception ex) {
@ -127,5 +147,4 @@ public class EmptyBuffer {
} }
} }
} }
} }