Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/nio/sctp/SctpMultiChannel/SendFailed.java
38867 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 806784625* @summary Test for send failed notification26*/2728import com.sun.nio.sctp.*;29import java.io.IOException;30import java.net.InetSocketAddress;31import java.net.SocketAddress;32import java.nio.ByteBuffer;33import static java.lang.System.out;34import static java.nio.ByteBuffer.*;3536public class SendFailed {3738static final SocketAddress remoteAddress = new InetSocketAddress("127.0.0.1", 3000);3940static final int[] bufferSizes =41{ 20, 49, 50, 51, 100, 101, 1024, 1025, 4095, 4096, 4097, 8191, 8192, 8193};4243void test(String[] args) throws IOException {44SocketAddress address = null;45String os = System.getProperty("os.name").toLowerCase();4647if (!Util.isSCTPSupported()) {48out.println("SCTP protocol is not supported");49out.println("Test cannot be run");50return;51} else if (os.startsWith("sunos")) {52out.println("Test not supported on Solaris");53out.println("Test cannot be run");54return;55}5657System.out.println("remote address: " + remoteAddress);58System.out.println("Note, remote address should not be up");5960/* combinations with various buffer sizes, and offsets */61for (int send=0; send < bufferSizes.length; send++) {62for (int recv=0; recv < bufferSizes.length; recv++) {63for (boolean direct : new boolean[] {true, false})64runWithManyOffsets(bufferSizes[send], bufferSizes[recv], direct);65}66}67}6869void runWithManyOffsets(int sendBufferSize, int recvBufferSize, boolean direct)70throws IOException71{72doTest(sendBufferSize, recvBufferSize, direct, 0);73doTest(sendBufferSize, recvBufferSize, direct, 1);74doTest(sendBufferSize, recvBufferSize, direct, 3);75doTest(sendBufferSize, recvBufferSize, direct, 7);76doTest(sendBufferSize, recvBufferSize, direct, 9);77doTest(sendBufferSize, recvBufferSize, direct, 13);78doTest(sendBufferSize, recvBufferSize, direct, 15);79}8081void doTest(int sendBufferSize, int recvBufferSize, boolean direct, int offset)82throws IOException83{84debug("%n--- Testing with send size:[%d], recv size:[%d], offset:[%d] "85+ ", direct [%s]. ", sendBufferSize, recvBufferSize, offset, direct);8687try (SctpMultiChannel channel = SctpMultiChannel.open()) {88MessageInfo messageInfo = MessageInfo.createOutgoing(remoteAddress, 0);89ByteBuffer sendBuffer = filledBuffer(sendBufferSize, direct);9091debug("%nAttempting to send to %s. ", remoteAddress);92int sent = channel.send(sendBuffer, messageInfo);93sendBuffer.flip();9495SendFailedNotificationHandler handler =96new SendFailedNotificationHandler();97ByteBuffer recvBuffer = direct ? allocateDirect(recvBufferSize)98: allocate((recvBufferSize));99channel.receive(recvBuffer, null, handler);100101// verify sent buffer received by send failed notification102ByteBuffer buffer = handler.getSendFailedByteBuffer();103check(buffer.remaining() == sent);104check(buffer.position() == 0);105check(buffer.limit() == sent);106assertSameContent(sendBuffer, handler.getSendFailedByteBuffer());107}108}109110class SendFailedNotificationHandler extends AbstractNotificationHandler<Object>111{112/** Reference to the buffer captured in send failed notification */113private ByteBuffer sentBuffer;114115@Override116public HandlerResult handleNotification(117Notification notification, Object attachment) {118fail("Unknown notification type");119return HandlerResult.CONTINUE;120}121122@Override123public HandlerResult handleNotification(124AssociationChangeNotification notification, Object attachment) {125AssociationChangeNotification.AssocChangeEvent event = notification.event();126debug("%nAssociationChangeNotification");127debug("%n Association: %s. ", notification.association());128debug("%n Event: %s. ", event);129return HandlerResult.CONTINUE;130}131132@Override133public HandlerResult handleNotification(134SendFailedNotification notification, Object attachment) {135debug("%nSendFailedNotification: %s. ", notification);136sentBuffer = notification.buffer();137return HandlerResult.RETURN;138}139140public ByteBuffer getSendFailedByteBuffer() {141return sentBuffer;142}143144@Override145public HandlerResult handleNotification(146PeerAddressChangeNotification pacn, Object attachment)147{148debug("%nPeerAddressChangeNotification: %s", pacn);149return HandlerResult.CONTINUE;150}151152@Override153public HandlerResult handleNotification(154ShutdownNotification notification, Object attachment) {155debug("%nShutdownNotification");156debug("%n Association: %s. ", notification.association());157return HandlerResult.CONTINUE;158}159}160161static ByteBuffer filledBuffer(int size, boolean direct) {162ByteBuffer buffer = direct ? allocateDirect(size) : allocate((size));163for (int i=0; i< size; i++)164buffer.put((byte)i);165buffer.flip();166return buffer;167}168169static void assertSameContent(ByteBuffer bb1, ByteBuffer bb2) {170if (!bb1.equals(bb2))171throw new RuntimeException("Buffers are not equal; bb1: " + bb1 + ", bb2: " + bb2);172}173174//--------------------- Infrastructure ---------------------------175boolean debug = true;176volatile int passed = 0, failed = 0;177void pass() {passed++;}178void fail() {failed++; Thread.dumpStack();}179void fail(String msg) {System.err.println(msg); fail();}180void unexpected(Throwable t) {failed++; t.printStackTrace();}181void check(boolean cond) {if (cond) pass(); else fail();}182void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}183void debug(String message, Object... args) {if(debug) { out.printf(message, args); } }184public static void main(String[] args) throws Throwable {185Class<?> k = new Object(){}.getClass().getEnclosingClass();186try {k.getMethod("instanceMain",String[].class)187.invoke( k.newInstance(), (Object) args);}188catch (Throwable e) {throw e.getCause();}}189public void instanceMain(String[] args) throws Throwable {190try {test(args);} catch (Throwable t) {unexpected(t);}191out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);192if (failed > 0) throw new AssertionError("Some tests failed");}193}194195196