jdk/test/java/nio/channels/ServerSocketChannel/SocketOptionTests.java
changeset 36115 0676e37a0b9c
parent 14342 8435a30053c1
child 37676 24ef455da1b0
equal deleted inserted replaced
36114:a5ed9456c9be 36115:0676e37a0b9c
     1 /*
     1 /*
     2  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2007, 2016, 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.
    47     public static void main(String[] args) throws IOException {
    47     public static void main(String[] args) throws IOException {
    48         ServerSocketChannel ssc = ServerSocketChannel.open();
    48         ServerSocketChannel ssc = ServerSocketChannel.open();
    49 
    49 
    50         // check supported options
    50         // check supported options
    51         Set<SocketOption<?>> options = ssc.supportedOptions();
    51         Set<SocketOption<?>> options = ssc.supportedOptions();
       
    52         boolean reuseport = options.contains(SO_REUSEPORT);
    52         if (!options.contains(SO_REUSEADDR))
    53         if (!options.contains(SO_REUSEADDR))
    53             throw new RuntimeException("SO_REUSEADDR should be supported");
    54             throw new RuntimeException("SO_REUSEADDR should be supported");
       
    55         if (!options.contains(SO_REUSEPORT) && reuseport)
       
    56             throw new RuntimeException("SO_REUSEPORT should be supported");
    54         if (!options.contains(SO_RCVBUF))
    57         if (!options.contains(SO_RCVBUF))
    55             throw new RuntimeException("SO_RCVBUF should be supported");
    58             throw new RuntimeException("SO_RCVBUF should be supported");
    56 
    59 
    57         // allowed to change when not bound
    60         // allowed to change when not bound
    58         ssc.setOption(SO_RCVBUF, 256*1024);     // can't check
    61         ssc.setOption(SO_RCVBUF, 256*1024);     // can't check
    62             throw new RuntimeException("setOption caused SO_RCVBUF to decrease");
    65             throw new RuntimeException("setOption caused SO_RCVBUF to decrease");
    63         ssc.setOption(SO_REUSEADDR, true);
    66         ssc.setOption(SO_REUSEADDR, true);
    64         checkOption(ssc, SO_REUSEADDR, true);
    67         checkOption(ssc, SO_REUSEADDR, true);
    65         ssc.setOption(SO_REUSEADDR, false);
    68         ssc.setOption(SO_REUSEADDR, false);
    66         checkOption(ssc, SO_REUSEADDR, false);
    69         checkOption(ssc, SO_REUSEADDR, false);
       
    70         if (reuseport) {
       
    71             ssc.setOption(SO_REUSEPORT, true);
       
    72             checkOption(ssc, SO_REUSEPORT, true);
       
    73             ssc.setOption(SO_REUSEPORT, false);
       
    74             checkOption(ssc, SO_REUSEPORT, false);
       
    75         }
    67 
    76 
    68         // NullPointerException
    77         // NullPointerException
    69         try {
    78         try {
    70             ssc.setOption(null, "value");
    79             ssc.setOption(null, "value");
    71             throw new RuntimeException("NullPointerException not thrown");
    80             throw new RuntimeException("NullPointerException not thrown");