jdk/test/java/nio/channels/AsynchronousDatagramChannel/Basic.java
changeset 3327 82e069ae54ab
parent 2057 3acf8e5e2ca0
child 3632 399359a027de
equal deleted inserted replaced
3326:31a5302fe6d4 3327:82e069ae54ab
    64             throw new RuntimeException("Unexpected number of bytes read");
    64             throw new RuntimeException("Unexpected number of bytes read");
    65 
    65 
    66         // Test: datagram packet not received immediately
    66         // Test: datagram packet not received immediately
    67         dst.clear();
    67         dst.clear();
    68         final CountDownLatch latch = new CountDownLatch(1);
    68         final CountDownLatch latch = new CountDownLatch(1);
    69         ch.receive(dst, null, new CompletionHandler<SocketAddress,Void>() {
    69         ch.receive(dst, (Void)null, new CompletionHandler<SocketAddress,Void>() {
    70             public void completed(SocketAddress source, Void att) {
    70             public void completed(SocketAddress source, Void att) {
    71                 latch.countDown();
    71                 latch.countDown();
    72             }
    72             }
    73             public void failed (Throwable exc, Void att) {
    73             public void failed (Throwable exc, Void att) {
    74             }
    74             }
    80         latch.await(2, TimeUnit.SECONDS);  // wait for completion handler
    80         latch.await(2, TimeUnit.SECONDS);  // wait for completion handler
    81 
    81 
    82         // Test: timeout
    82         // Test: timeout
    83         dst.clear();
    83         dst.clear();
    84         final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
    84         final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
    85         ch.receive(dst, 2, TimeUnit.SECONDS, null, new CompletionHandler<SocketAddress,Void>() {
    85         ch.receive(dst, 2, TimeUnit.SECONDS, (Void)null, new CompletionHandler<SocketAddress,Void>() {
    86             public void completed(SocketAddress source, Void att) {
    86             public void completed(SocketAddress source, Void att) {
    87             }
    87             }
    88             public void failed (Throwable exc, Void att) {
    88             public void failed (Throwable exc, Void att) {
    89                 exception.set(exc);
    89                 exception.set(exc);
    90             }
    90             }
    99             throw new RuntimeException("InterruptedByTimeoutException expected");
    99             throw new RuntimeException("InterruptedByTimeoutException expected");
   100 
   100 
   101         // AsynchronousCloseException
   101         // AsynchronousCloseException
   102         dst = ByteBuffer.allocateDirect(100);
   102         dst = ByteBuffer.allocateDirect(100);
   103         exception.set(null);
   103         exception.set(null);
   104         ch.receive(dst, null, new CompletionHandler<SocketAddress,Void>() {
   104         ch.receive(dst, (Void)null, new CompletionHandler<SocketAddress,Void>() {
   105             public void completed(SocketAddress source, Void att) {
   105             public void completed(SocketAddress source, Void att) {
   106             }
   106             }
   107             public void failed (Throwable exc, Void att) {
   107             public void failed (Throwable exc, Void att) {
   108                 exception.set(exc);
   108                 exception.set(exc);
   109             }
   109             }
   154             throw new RuntimeException("Unexpected number of bytes read");
   154             throw new RuntimeException("Unexpected number of bytes read");
   155 
   155 
   156         // Test: datagram packet not received immediately
   156         // Test: datagram packet not received immediately
   157         dst.clear();
   157         dst.clear();
   158         final CountDownLatch l1 = new CountDownLatch(1);
   158         final CountDownLatch l1 = new CountDownLatch(1);
   159         ch.read(dst, null, new CompletionHandler<Integer,Void>() {
   159         ch.read(dst, (Void)null, new CompletionHandler<Integer,Void>() {
   160             public void completed(Integer bytesRead, Void att) {
   160             public void completed(Integer bytesRead, Void att) {
   161                 l1.countDown();
   161                 l1.countDown();
   162             }
   162             }
   163             public void failed (Throwable exc, Void att) {
   163             public void failed (Throwable exc, Void att) {
   164             }
   164             }
   170         l1.await(2, TimeUnit.SECONDS);
   170         l1.await(2, TimeUnit.SECONDS);
   171 
   171 
   172         // Test: timeout
   172         // Test: timeout
   173         dst.clear();
   173         dst.clear();
   174         final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
   174         final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
   175         ch.read(dst, 2, TimeUnit.SECONDS, null, new CompletionHandler<Integer,Void>() {
   175         ch.read(dst, 2, TimeUnit.SECONDS, (Void)null, new CompletionHandler<Integer,Void>() {
   176             public void completed(Integer bytesRead, Void att) {
   176             public void completed(Integer bytesRead, Void att) {
   177             }
   177             }
   178             public void failed (Throwable exc, Void att) {
   178             public void failed (Throwable exc, Void att) {
   179                 exception.set(exc);
   179                 exception.set(exc);
   180             }
   180             }
   189             throw new RuntimeException("InterruptedByTimeoutException expected");
   189             throw new RuntimeException("InterruptedByTimeoutException expected");
   190 
   190 
   191         // AsynchronousCloseException
   191         // AsynchronousCloseException
   192         dst.clear();
   192         dst.clear();
   193         exception.set(null);
   193         exception.set(null);
   194         ch.read(dst, null, new CompletionHandler<Integer,Void>() {
   194         ch.read(dst, (Void)null, new CompletionHandler<Integer,Void>() {
   195             public void completed(Integer bytesRead, Void att) {
   195             public void completed(Integer bytesRead, Void att) {
   196             }
   196             }
   197             public void failed (Throwable exc, Void att) {
   197             public void failed (Throwable exc, Void att) {
   198                 exception.set(exc);
   198                 exception.set(exc);
   199             }
   199             }
   236             throw new RuntimeException("Unexpected number of bytes received");
   236             throw new RuntimeException("Unexpected number of bytes received");
   237 
   237 
   238         // Test: send datagram packet to reader and check completion handler
   238         // Test: send datagram packet to reader and check completion handler
   239         // is invoked
   239         // is invoked
   240         final CountDownLatch l2 = new CountDownLatch(1);
   240         final CountDownLatch l2 = new CountDownLatch(1);
   241         ch.send(ByteBuffer.wrap(msg), sa, null, new CompletionHandler<Integer,Void>() {
   241         ch.send(ByteBuffer.wrap(msg), sa, (Void)null, new CompletionHandler<Integer,Void>() {
   242             public void completed(Integer bytesSent, Void att) {
   242             public void completed(Integer bytesSent, Void att) {
   243                 if (bytesSent != msg.length)
   243                 if (bytesSent != msg.length)
   244                     throw new RuntimeException("Unexpected number of bytes received");
   244                     throw new RuntimeException("Unexpected number of bytes received");
   245                 l2.countDown();
   245                 l2.countDown();
   246             }
   246             }
   259             throw new RuntimeException("Unexpected number of bytes received");
   259             throw new RuntimeException("Unexpected number of bytes received");
   260 
   260 
   261         // Test: check that failed method is invoked
   261         // Test: check that failed method is invoked
   262         ch.close();
   262         ch.close();
   263         final CountDownLatch l3 = new CountDownLatch(1);
   263         final CountDownLatch l3 = new CountDownLatch(1);
   264         ch.send(ByteBuffer.wrap(msg), sa, null, new CompletionHandler<Integer,Void>() {
   264         ch.send(ByteBuffer.wrap(msg), sa, (Void)null, new CompletionHandler<Integer,Void>() {
   265             public void completed(Integer bytesSent, Void att) {
   265             public void completed(Integer bytesSent, Void att) {
   266                 throw new RuntimeException("completed method invoked");
   266                 throw new RuntimeException("completed method invoked");
   267             }
   267             }
   268             public void failed (Throwable exc, Void att) {
   268             public void failed (Throwable exc, Void att) {
   269                 if (exc instanceof ClosedChannelException) {
   269                 if (exc instanceof ClosedChannelException) {
   313         if (dst.remaining() != msg.length)
   313         if (dst.remaining() != msg.length)
   314             throw new RuntimeException("Unexpected number of bytes received");
   314             throw new RuntimeException("Unexpected number of bytes received");
   315 
   315 
   316         // Test: write datagram and check completion handler is invoked
   316         // Test: write datagram and check completion handler is invoked
   317         final CountDownLatch l2 = new CountDownLatch(1);
   317         final CountDownLatch l2 = new CountDownLatch(1);
   318         ch.write(ByteBuffer.wrap(msg), null, new CompletionHandler<Integer,Void>() {
   318         ch.write(ByteBuffer.wrap(msg), (Void)null, new CompletionHandler<Integer,Void>() {
   319             public void completed(Integer bytesSent, Void att) {
   319             public void completed(Integer bytesSent, Void att) {
   320                 if (bytesSent != msg.length)
   320                 if (bytesSent != msg.length)
   321                     throw new RuntimeException("Unexpected number of bytes received");
   321                     throw new RuntimeException("Unexpected number of bytes received");
   322                 l2.countDown();
   322                 l2.countDown();
   323             }
   323             }
   370             AsynchronousDatagramChannel ch =
   370             AsynchronousDatagramChannel ch =
   371                 AsynchronousDatagramChannel.open().bind(new InetSocketAddress(0));
   371                 AsynchronousDatagramChannel.open().bind(new InetSocketAddress(0));
   372             final CountDownLatch latch = new CountDownLatch(1);
   372             final CountDownLatch latch = new CountDownLatch(1);
   373             long timeout = (i == 0) ? 0L : 60L;
   373             long timeout = (i == 0) ? 0L : 60L;
   374             Future<SocketAddress> remote = ch
   374             Future<SocketAddress> remote = ch
   375                 .receive(ByteBuffer.allocate(100), timeout, TimeUnit.SECONDS, null,
   375                 .receive(ByteBuffer.allocate(100), timeout, TimeUnit.SECONDS, (Void)null,
   376                     new CompletionHandler<SocketAddress,Void>() {
   376                     new CompletionHandler<SocketAddress,Void>() {
   377                         public void completed(SocketAddress source, Void att) {
   377                         public void completed(SocketAddress source, Void att) {
   378                         }
   378                         }
   379                         public void failed (Throwable exc, Void att) {
   379                         public void failed (Throwable exc, Void att) {
   380                         }
   380                         }
   393              ch.connect(new InetSocketAddress(lh,
   393              ch.connect(new InetSocketAddress(lh,
   394                 ((InetSocketAddress)(ch.getLocalAddress())).getPort()));
   394                 ((InetSocketAddress)(ch.getLocalAddress())).getPort()));
   395             final CountDownLatch latch = new CountDownLatch(1);
   395             final CountDownLatch latch = new CountDownLatch(1);
   396             long timeout = (i == 0) ? 0L : 60L;
   396             long timeout = (i == 0) ? 0L : 60L;
   397             Future<Integer> result = ch
   397             Future<Integer> result = ch
   398                 .read(ByteBuffer.allocate(100), timeout, TimeUnit.SECONDS, null,
   398                 .read(ByteBuffer.allocate(100), timeout, TimeUnit.SECONDS, (Void)null,
   399                     new CompletionHandler<Integer,Void>() {
   399                     new CompletionHandler<Integer,Void>() {
   400                         public void completed(Integer bytesRead, Void att) {
   400                         public void completed(Integer bytesRead, Void att) {
   401                         }
   401                         }
   402                         public void failed (Throwable exc, Void att) {
   402                         public void failed (Throwable exc, Void att) {
   403                         }
   403                         }