View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.testsuite.transport.socket;
17  
18  import io.netty.bootstrap.Bootstrap;
19  import io.netty.bootstrap.ServerBootstrap;
20  import io.netty.buffer.ByteBuf;
21  import io.netty.buffer.Unpooled;
22  import io.netty.channel.Channel;
23  import io.netty.channel.ChannelHandlerContext;
24  import io.netty.channel.ChannelInboundHandlerAdapter;
25  import io.netty.channel.nio.NioEventLoopGroup;
26  import io.netty.channel.socket.SocketChannel;
27  import io.netty.channel.socket.nio.NioServerSocketChannel;
28  import io.netty.handler.codec.ByteToMessageDecoder;
29  import org.junit.jupiter.api.Test;
30  import org.junit.jupiter.api.TestInfo;
31  import org.junit.jupiter.api.Timeout;
32  import org.junit.jupiter.api.function.Executable;
33  
34  import java.net.SocketException;
35  import java.nio.channels.NotYetConnectedException;
36  import java.util.List;
37  import java.util.concurrent.CountDownLatch;
38  
39  import static org.assertj.core.api.Assertions.assertThat;
40  import static org.junit.jupiter.api.Assertions.assertFalse;
41  import static org.junit.jupiter.api.Assertions.assertThrows;
42  
43  public class SocketChannelNotYetConnectedTest extends AbstractClientSocketTest {
44      @Test
45      @Timeout(30)
46      public void testShutdownNotYetConnected(TestInfo testInfo) throws Throwable {
47          run(testInfo, new Runner<Bootstrap>() {
48              @Override
49              public void run(Bootstrap bootstrap) throws Throwable {
50                  testShutdownNotYetConnected(bootstrap);
51              }
52          });
53      }
54  
55      public void testShutdownNotYetConnected(Bootstrap cb) throws Throwable {
56          final SocketChannel ch = (SocketChannel) cb.handler(new ChannelInboundHandlerAdapter())
57                  .bind(newSocketAddress()).syncUninterruptibly().channel();
58          try {
59              Throwable cause = assertThrows(Throwable.class, new Executable() {
60                  @Override
61                  public void execute() throws Throwable {
62                      ch.shutdownInput().syncUninterruptibly();
63                  }
64              });
65              checkThrowable(cause);
66  
67              cause = assertThrows(Throwable.class, new Executable() {
68                  @Override
69                  public void execute() throws Throwable {
70                      ch.shutdownOutput().syncUninterruptibly();
71                  }
72              });
73              checkThrowable(cause);
74          } finally {
75              ch.close().syncUninterruptibly();
76          }
77      }
78  
79      private static void checkThrowable(Throwable cause) throws Throwable {
80          // Depending on OIO / NIO both are ok
81          if (!(cause instanceof NotYetConnectedException) && !(cause instanceof SocketException)) {
82              throw cause;
83          }
84      }
85  
86      @Test
87      @Timeout(60)
88      public void readMustBePendingUntilChannelIsActive(TestInfo info) throws Throwable {
89          run(info, new Runner<Bootstrap>() {
90              @Override
91              public void run(Bootstrap bootstrap) throws Throwable {
92                  NioEventLoopGroup group = new NioEventLoopGroup(1);
93                  ServerBootstrap sb = new ServerBootstrap().group(group);
94                  Channel serverChannel = sb.childHandler(new ChannelInboundHandlerAdapter() {
95                      @Override
96                      public void channelActive(ChannelHandlerContext ctx) throws Exception {
97                          ctx.writeAndFlush(Unpooled.copyInt(42));
98                      }
99                  }).channel(NioServerSocketChannel.class).bind(0).sync().channel();
100 
101                 final CountDownLatch readLatch = new CountDownLatch(1);
102                 bootstrap.handler(new ByteToMessageDecoder() {
103                     @Override
104                     public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
105                         assertFalse(ctx.channel().isActive());
106                         ctx.read();
107                     }
108 
109                     @Override
110                     protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
111                         assertThat(in.readableBytes()).isLessThanOrEqualTo(Integer.BYTES);
112                         if (in.readableBytes() == Integer.BYTES) {
113                             assertThat(in.readInt()).isEqualTo(42);
114                             readLatch.countDown();
115                         }
116                     }
117                 });
118                 bootstrap.connect(serverChannel.localAddress()).sync();
119 
120                 readLatch.await();
121                 group.shutdownGracefully().await();
122             }
123         });
124     }
125 }