查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2015 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.channel.unix.tests;
17  
18  import io.netty.channel.unix.Buffer;
19  import io.netty.channel.unix.Socket;
20  import org.junit.jupiter.api.AfterEach;
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  import org.opentest4j.TestAbortedException;
24  
25  import java.io.IOException;
26  import java.nio.ByteBuffer;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  import static org.junit.jupiter.api.Assertions.assertFalse;
30  import static org.junit.jupiter.api.Assertions.assertNotEquals;
31  import static org.junit.jupiter.api.Assertions.assertTrue;
32  
33  public abstract class SocketTest<T extends Socket> {
34      protected T socket;
35  
36      protected abstract T newSocket();
37  
38      @BeforeEach
39      public void setup() {
40          socket = newSocket();
41      }
42  
43      @AfterEach
44      public void tearDown() throws IOException {
45          socket.close();
46      }
47  
48      @Test
49      public void testKeepAlive() throws Exception {
50          assertFalse(socket.isKeepAlive());
51          socket.setKeepAlive(true);
52          assertTrue(socket.isKeepAlive());
53      }
54  
55      @Test
56      public void testTcpNoDelay() throws Exception {
57          assertFalse(socket.isTcpNoDelay());
58          socket.setTcpNoDelay(true);
59          assertTrue(socket.isTcpNoDelay());
60      }
61  
62      @Test
63      public void testReceivedBufferSize() throws Exception {
64          int size = socket.getReceiveBufferSize();
65          int newSize = 65535;
66          assertTrue(size > 0);
67          socket.setReceiveBufferSize(newSize);
68          // Linux usually set it to double what is specified
69          assertTrue(newSize <= socket.getReceiveBufferSize());
70      }
71  
72      @Test
73      public void testSendBufferSize() throws Exception {
74          int size = socket.getSendBufferSize();
75          int newSize = 65535;
76          assertTrue(size > 0);
77          socket.setSendBufferSize(newSize);
78          // Linux usually set it to double what is specified
79          assertTrue(newSize <= socket.getSendBufferSize());
80      }
81  
82      @Test
83      public void testSoLinger() throws Exception {
84          assertEquals(-1, socket.getSoLinger());
85          socket.setSoLinger(10);
86          assertEquals(10, socket.getSoLinger());
87      }
88  
89      @Test
90      public void testDoubleCloseDoesNotThrow() throws IOException {
91          Socket socket = Socket.newSocketStream();
92          socket.close();
93          socket.close();
94      }
95  
96      @Test
97      public void testTrafficClass() throws IOException {
98          // IPTOS_THROUGHPUT
99          final int value = 0x08;
100         socket.setTrafficClass(value);
101         assertEquals(value, socket.getTrafficClass());
102     }
103 
104     @Test
105     public void testIntOpt() throws IOException {
106         socket.setReuseAddress(false);
107         socket.setIntOpt(level(), optname(), 1);
108         // Anything which is != 0 is considered enabled
109         assertNotEquals(0, socket.getIntOpt(level(), optname()));
110         socket.setIntOpt(level(), optname(), 0);
111         // This should be disabled again
112         assertEquals(0, socket.getIntOpt(level(), optname()));
113     }
114 
115     @Test
116     public void testRawOpt() throws IOException {
117         ByteBuffer buffer = Buffer.allocateDirectWithNativeOrder(4);
118         buffer.putInt(1).flip();
119         socket.setRawOpt(level(), optname(), buffer);
120 
121         ByteBuffer out = ByteBuffer.allocate(4);
122         socket.getRawOpt(level(), optname(), out);
123         assertFalse(out.hasRemaining());
124 
125         out.flip();
126         assertNotEquals(ByteBuffer.allocate(0), out);
127     }
128 
129     protected int level() {
130         throw new TestAbortedException("Not supported");
131     }
132 
133     protected int optname() {
134         throw new TestAbortedException("Not supported");
135     }
136 }