Code Review Request: 7044870: TEST_BUG: java/nio/channels/DatagramChannel/SelectWhenRefused.java failed on SUSE Linux 10

Kurchi Hazra kurchi.subhra.hazra at oracle.com
Thu Aug 25 06:57:44 PDT 2011


Hi,

  The update code with some more styling/comment changes:

diff --git 
a/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java 
b/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java
--- a/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java
+++ b/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java
@@ -22,7 +22,7 @@
   */

  /* @test
- * @bug 6935563
+ * @bug 6935563 7044870
   * @summary Test that Selector does not select an unconnected 
DatagramChannel when
   *    ICMP port unreachable received
   */
@@ -35,14 +35,15 @@ public class SelectWhenRefused {
  public class SelectWhenRefused {

      public static void main(String[] args) throws IOException {
-        DatagramChannel dc = DatagramChannel.open().bind(new 
InetSocketAddress(0));
-        int port = dc.socket().getLocalPort();
-        dc.close();
+        DatagramChannel dc1 = DatagramChannel.open().bind(new 
InetSocketAddress(0));
+        int port = dc1.socket().getLocalPort();

          // datagram sent to this address should be refused
          SocketAddress refuser = new 
InetSocketAddress(InetAddress.getLocalHost(), port);

-        dc = DatagramChannel.open().bind(new InetSocketAddress(0));
+        DatagramChannel dc = DatagramChannel.open().bind(new 
InetSocketAddress(0));
+        dc1.close();
+
          Selector sel = Selector.open();
          try {
              dc.configureBlocking(false);
@@ -52,6 +53,11 @@ public class SelectWhenRefused {
              sendDatagram(dc, refuser);
              int n = sel.select(2000);
              if (n > 0) {
+                sel.selectedKeys().clear();
+                //BindException will be thrown if another service is using
+                //our expected refuser port, cannot run just exit.
+                DatagramSocket sock=new DatagramSocket(port);
+                sock.close();
                  throw new RuntimeException("Unexpected wakeup");
              }

@@ -80,6 +86,8 @@ public class SelectWhenRefused {
                  throw new RuntimeException("Unexpected wakeup after 
disconnect");
              }

+        } catch(BindException e) {
+            //Do nothing, some other test has used this port
          } finally {
              sel.close();
              dc.close();


Thanks,
Kurchi


On 8/23/2011 9:04 PM, Kurchi Hazra wrote:
> Hi,
>
>   The updated code:
>
> -bash-3.00$ hg diff
> *** failed to import extension rebase: No module named rebase
> diff --git 
> a/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java 
> b/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java
> --- a/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java
> +++ b/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java
> @@ -35,14 +35,15 @@ public class SelectWhenRefused {
>  public class SelectWhenRefused {
>
>      public static void main(String[] args) throws IOException {
> -        DatagramChannel dc = DatagramChannel.open().bind(new 
> InetSocketAddress(0));
> -        int port = dc.socket().getLocalPort();
> -        dc.close();
> +        DatagramChannel dc1 = DatagramChannel.open().bind(new 
> InetSocketAddress(0));
> +        int port = dc1.socket().getLocalPort();
>
>          // datagram sent to this address should be refused
>          SocketAddress refuser = new 
> InetSocketAddress(InetAddress.getLocalHost(), port);
>
> -        dc = DatagramChannel.open().bind(new InetSocketAddress(0));
> +        DatagramChannel dc = DatagramChannel.open().bind(new 
> InetSocketAddress(0));
> +       dc1.close();
> +
>          Selector sel = Selector.open();
>          try {
>              dc.configureBlocking(false);
> @@ -52,6 +53,9 @@ public class SelectWhenRefused {
>              sendDatagram(dc, refuser);
>              int n = sel.select(2000);
>              if (n > 0) {
> +               sel.selectedKeys().clear();
> +                DatagramSocket sock=new DatagramSocket(port);
> +                sock.close();
>                  throw new RuntimeException("Unexpected wakeup");
>              }
>
> @@ -80,7 +84,11 @@ public class SelectWhenRefused {
>                  throw new RuntimeException("Unexpected wakeup after 
> disconnect");
>              }
>
> -        } finally {
> +        } catch(BindException e) {
> +
> +           //Do nothing, some other test has used this port
> +
> +       } finally {
>              sel.close();
>              dc.close();
>          }
>
>
>
> Thanks,
>
> Kurchi
>
> On 8/23/2011 1:36 PM, chris hegarty wrote:
>> I'm not sure if it is the output of hg diff or your editor, but there 
>> appears to be stylistic issues.
>>
>>   - removal of four space indentation of main method and several 
>> following declarations
>>   - open curly brace for finally/catch should be on same line
>>   - finally clause should start on the same line as the closing curly 
>> brace
>>
>> -Chris.
>>
>> There seems to be
>>
>> On 23/08/2011 19:38, Kurchi Hazra wrote:
>>> hg diff for the updated code:
>>>
>>> -bash-3.00$ hg diff
>>> diff --git 
>>> a/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java 
>>> b/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java
>>> --- a/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java
>>> +++ b/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java
>>> @@ -34,17 +34,18 @@ import java.io.IOException;
>>>
>>>  public class SelectWhenRefused {
>>>
>>> -    public static void main(String[] args) throws IOException {
>>> -        DatagramChannel dc = DatagramChannel.open().bind(new 
>>> InetSocketAddress(0));
>>> -        int port = dc.socket().getLocalPort();
>>> -        dc.close();
>>> +  public static void main(String[] args) throws IOException {
>>> +     DatagramChannel dc1 = DatagramChannel.open().bind(new 
>>> InetSocketAddress(0));
>>> +     int port = dc1.socket().getLocalPort();
>>> +     DatagramChannel dc = DatagramChannel.open().bind(new 
>>> InetSocketAddress(0));
>>> +     dc1.close();
>>>
>>> -        // datagram sent to this address should be refused
>>> -        SocketAddress refuser = new 
>>> InetSocketAddress(InetAddress.getLocalHost(), port);
>>> +     // datagram sent to this address should be refused
>>>
>>> -        dc = DatagramChannel.open().bind(new InetSocketAddress(0));
>>> -        Selector sel = Selector.open();
>>> -        try {
>>> +     SocketAddress refuser = new 
>>> InetSocketAddress(InetAddress.getLocalHost(), port);
>>> +     Selector sel = Selector.open();
>>> +
>>> +    try {
>>>              dc.configureBlocking(false);
>>>              dc.register(sel, SelectionKey.OP_READ);
>>>
>>> @@ -52,12 +53,14 @@ public class SelectWhenRefused {
>>>              sendDatagram(dc, refuser);
>>>              int n = sel.select(2000);
>>>              if (n > 0) {
>>> +                sel.selectedKeys().clear();
>>> +                DatagramSocket sock=new DatagramSocket(port);
>>> +                sock.close();
>>>                  throw new RuntimeException("Unexpected wakeup");
>>> -            }
>>> -
>>> -            /* Test 2: connected so ICMP port unreachable may be 
>>> received */
>>> -            dc.connect(refuser);
>>> -            try {
>>> +                }
>>> +                           /* Test 2: connected so ICMP port 
>>> unreachable may be received */
>>> +             try {
>>> +                dc.connect(refuser);
>>>                  sendDatagram(dc, refuser);
>>>                  n = sel.select(2000);
>>>                  if (n > 0) {
>>> @@ -80,11 +83,18 @@ public class SelectWhenRefused {
>>>                  throw new RuntimeException("Unexpected wakeup after 
>>> disconnect");
>>>              }
>>>
>>> -        } finally {
>>> +        }
>>> +        catch (BindException e)
>>> +        {
>>> +                //Do nothing, someone else has bound to this port
>>> +        }
>>> +         finally {
>>>              sel.close();
>>>              dc.close();
>>>          }
>>> +
>>>      }
>>> +
>>>
>>>      static void sendDatagram(DatagramChannel dc, SocketAddress remote)
>>>          throws IOException
>>>
>>>
>>> On 8/23/2011 11:04 AM, Kurchi Hazra wrote:
>>>>
>>>>
>>>> On 8/23/2011 8:08 AM, Chris Hegarty wrote:
>>>>> Hi Kurchi,
>>>>>
>>>>> I see your change is checking if another service is using the 
>>>>> expected refuser port, and then ignoring any data since the test 
>>>>> cannot be successfully run. I think this is good, but it may also 
>>>>> be useful to defer closing of dc1 until after creation of dc. That 
>>>>> way this issue shouldn't happen as often since dc will not reuse 
>>>>> the refusers port. Make sense? Also, doesn't this impact on the 
>>>>> rest of the test too?
>>>>>
>>>>> Would just catch BindException be sufficient? That's what you're 
>>>>> expecting, right, "Address already in use"?
>>>>
>>>>
>>>>
>>>> - I agree, it should be a BindException that I catch. Also, I will 
>>>> incorporate deferring of closing of dc1 until after dc is allocated 
>>>> a port.
>>>>
>>>> -Kurchi
>>>>
>>>>
>>>>>
>>>>> -Chris.
>>>>>
>>>>> On 23/08/2011 01:29, Kurchi Hazra wrote:
>>>>>> Hi,
>>>>>>
>>>>>>
>>>>>> Test 1 in 
>>>>>> java/nio/channels/DatagramChannel/SelectWhenRefused.java was
>>>>>> failing on some Linux kernels. This is because the test assumes 
>>>>>> that the
>>>>>> kernel will not bind a socket to a port that has been closed 
>>>>>> immediately.
>>>>>> However, some linux kernels (for example, 2.6.16.60-0.62.1-smp ) are
>>>>>> precisely doing this and the test throws a RunTime Exception.
>>>>>>
>>>>>> The code has been changed to not to try binding the same socket 
>>>>>> that has
>>>>>> been immediately closed. In addition, we make sure that a port is 
>>>>>> not
>>>>>> bound to a service already (in between the port being closed 
>>>>>> before and
>>>>>> connected to later in the code) by checking before throwing the 
>>>>>> Exception.
>>>>>>
>>>>>>
>>>>>> Submitting hg diff since I do not have an openjdk account:
>>>>>>
>>>>>> -bash-3.00$ hg diff
>>>>>> diff --git
>>>>>> a/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java
>>>>>> b/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java
>>>>>> --- a/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java
>>>>>> +++ b/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java
>>>>>> @@ -35,14 +35,14 @@ public class SelectWhenRefused {
>>>>>> public class SelectWhenRefused {
>>>>>>
>>>>>> public static void main(String[] args) throws IOException {
>>>>>> - DatagramChannel dc = DatagramChannel.open().bind(new
>>>>>> InetSocketAddress(0));
>>>>>> - int port = dc.socket().getLocalPort();
>>>>>> - dc.close();
>>>>>> + DatagramChannel dc1 = DatagramChannel.open().bind(new
>>>>>> InetSocketAddress(0));
>>>>>> + int port = dc1.socket().getLocalPort();
>>>>>> + dc1.close();
>>>>>>
>>>>>> // datagram sent to this address should be refused
>>>>>> SocketAddress refuser = new
>>>>>> InetSocketAddress(InetAddress.getLocalHost(), port);
>>>>>>
>>>>>> - dc = DatagramChannel.open().bind(new InetSocketAddress(0));
>>>>>> + DatagramChannel dc = DatagramChannel.open().bind(new
>>>>>> InetSocketAddress(0));
>>>>>> Selector sel = Selector.open();
>>>>>> try {
>>>>>> dc.configureBlocking(false);
>>>>>> @@ -52,7 +52,15 @@ public class SelectWhenRefused {
>>>>>> sendDatagram(dc, refuser);
>>>>>> int n = sel.select(2000);
>>>>>> if (n > 0) {
>>>>>> + try {
>>>>>> + DatagramSocket dsBindCheck=new DatagramSocket(port);
>>>>>> + dsBindCheck.close();
>>>>>> throw new RuntimeException("Unexpected wakeup");
>>>>>> + }
>>>>>> + catch(SocketException e)
>>>>>> + {
>>>>>> + //Do nothing,some other test may have used the port
>>>>>> + }
>>>>>> }
>>>>>>
>>>>>> /* Test 2: connected so ICMP port unreachable may be received */
>>>>>>
>>>>>>
>>>>>> Thanks,
>>>>>>
>>>>
>>>
>
> -- 
> -Kurchi

-- 
-Kurchi

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/nio-dev/attachments/20110825/21ee06ee/attachment-0001.html 


More information about the nio-dev mailing list