test/jdk/java/nio/channels/spi/SelectorProvider/inheritedChannel/EchoTest.java
changeset 51117 c96c7d08ae49
parent 47216 71c04702a3d5
equal deleted inserted replaced
51116:7e34f3da2293 51117:c96c7d08ae49
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    42 import java.nio.channels.SelectionKey;
    42 import java.nio.channels.SelectionKey;
    43 import java.nio.channels.Selector;
    43 import java.nio.channels.Selector;
    44 import java.nio.channels.SocketChannel;
    44 import java.nio.channels.SocketChannel;
    45 import java.util.Random;
    45 import java.util.Random;
    46 
    46 
       
    47 import jdk.test.lib.Utils;
       
    48 
    47 public class EchoTest {
    49 public class EchoTest {
    48 
    50 
    49     private static int failures = 0;
    51     private static int failures = 0;
    50 
    52 
    51     private static String ECHO_SERVICE = "EchoService";
    53     private static String ECHO_SERVICE = "EchoService";
    78         ByteBuffer bb2 = ByteBuffer.allocate(size+100);
    80         ByteBuffer bb2 = ByteBuffer.allocate(size+100);
    79         sc.configureBlocking(false);
    81         sc.configureBlocking(false);
    80         Selector sel = sc.provider().openSelector();
    82         Selector sel = sc.provider().openSelector();
    81         SelectionKey sk = sc.register(sel, SelectionKey.OP_READ);
    83         SelectionKey sk = sc.register(sel, SelectionKey.OP_READ);
    82         int nread = 0;
    84         int nread = 0;
    83         long to = 5000;
    85         long to = Utils.adjustTimeout(5000);
    84         while (nread < size) {
    86         while (nread < size) {
    85             long st = System.currentTimeMillis();
    87             long st = System.currentTimeMillis();
    86             sel.select(to);
    88             sel.select(to);
    87             if (sk.isReadable()) {
    89             if (sk.isReadable()) {
    88                 int n = sc.read(bb2);
    90                 int n = sc.read(bb2);
   142         dc.write(bb);
   144         dc.write(bb);
   143 
   145 
   144         // and receive the echo
   146         // and receive the echo
   145         byte b[] = new byte[msg.length() + 100];
   147         byte b[] = new byte[msg.length() + 100];
   146         DatagramPacket pkt2 = new DatagramPacket(b, b.length);
   148         DatagramPacket pkt2 = new DatagramPacket(b, b.length);
   147         dc.socket().setSoTimeout(5000);
   149         dc.socket().setSoTimeout((int)Utils.adjustTimeout(5000));
   148         dc.socket().receive(pkt2);
   150         dc.socket().receive(pkt2);
   149 
   151 
   150         if (pkt2.getLength() != msg.length()) {
   152         if (pkt2.getLength() != msg.length()) {
   151             throw new RuntimeException("Received packet of incorrect length");
   153             throw new RuntimeException("Received packet of incorrect length");
   152         }
   154         }