Committed by
Gerrit Code Review
Use TestTools.findAvailablePort
- Use TestTools.findAvailablePort when selecting unused port during unit tests Change-Id: I155781db8830fc3d5238bda0b23cbbf065ba7b51
Showing
2 changed files
with
8 additions
and
20 deletions
... | @@ -19,8 +19,6 @@ import static org.junit.Assert.*; | ... | @@ -19,8 +19,6 @@ import static org.junit.Assert.*; |
19 | import static org.onosproject.net.DeviceId.deviceId; | 19 | import static org.onosproject.net.DeviceId.deviceId; |
20 | import static org.onosproject.net.PortNumber.portNumber; | 20 | import static org.onosproject.net.PortNumber.portNumber; |
21 | 21 | ||
22 | -import java.io.IOException; | ||
23 | -import java.net.ServerSocket; | ||
24 | import java.net.URI; | 22 | import java.net.URI; |
25 | import java.util.Collection; | 23 | import java.util.Collection; |
26 | import java.util.Collections; | 24 | import java.util.Collections; |
... | @@ -29,10 +27,10 @@ import java.util.Set; | ... | @@ -29,10 +27,10 @@ import java.util.Set; |
29 | import java.util.concurrent.CountDownLatch; | 27 | import java.util.concurrent.CountDownLatch; |
30 | import java.util.concurrent.TimeUnit; | 28 | import java.util.concurrent.TimeUnit; |
31 | 29 | ||
32 | -import org.apache.commons.lang3.RandomUtils; | ||
33 | import org.junit.After; | 30 | import org.junit.After; |
34 | import org.junit.Before; | 31 | import org.junit.Before; |
35 | import org.junit.Test; | 32 | import org.junit.Test; |
33 | +import org.onlab.junit.TestTools; | ||
36 | import org.onlab.packet.ChassisId; | 34 | import org.onlab.packet.ChassisId; |
37 | import org.onosproject.incubator.rpc.RemoteServiceContext; | 35 | import org.onosproject.incubator.rpc.RemoteServiceContext; |
38 | import org.onosproject.incubator.rpc.RemoteServiceContextProvider; | 36 | import org.onosproject.incubator.rpc.RemoteServiceContextProvider; |
... | @@ -114,28 +112,15 @@ public class GrpcRemoteServiceTest { | ... | @@ -114,28 +112,15 @@ public class GrpcRemoteServiceTest { |
114 | 112 | ||
115 | private URI uri; | 113 | private URI uri; |
116 | 114 | ||
117 | - public static int pickListenPort() { | ||
118 | - try { | ||
119 | - // pick unused port | ||
120 | - ServerSocket socket = new ServerSocket(0); | ||
121 | - int port = socket.getLocalPort(); | ||
122 | - socket.close(); | ||
123 | - return port; | ||
124 | - } catch (IOException e) { | ||
125 | - // something went wrong, try picking randomly | ||
126 | - return RandomUtils.nextInt(49152, 0xFFFF + 1); | ||
127 | - } | ||
128 | - } | ||
129 | - | ||
130 | @Before | 115 | @Before |
131 | public void setUp() throws Exception { | 116 | public void setUp() throws Exception { |
132 | serverReady = new CountDownLatch(1); | 117 | serverReady = new CountDownLatch(1); |
133 | server = new GrpcRemoteServiceServer(); | 118 | server = new GrpcRemoteServiceServer(); |
134 | server.deviceProviderRegistry = new MTestDeviceProviderRegistry(); | 119 | server.deviceProviderRegistry = new MTestDeviceProviderRegistry(); |
135 | server.linkProviderRegistry = new ServerSideLinkProviderRegistry(); | 120 | server.linkProviderRegistry = new ServerSideLinkProviderRegistry(); |
136 | - // todo: pass proper ComponentContext | 121 | + server.listenPort = TestTools.findAvailablePort(11984); |
137 | - server.listenPort = pickListenPort(); | ||
138 | uri = URI.create("grpc://localhost:" + server.listenPort); | 122 | uri = URI.create("grpc://localhost:" + server.listenPort); |
123 | + // todo: pass proper ComponentContext | ||
139 | server.activate(null); | 124 | server.activate(null); |
140 | 125 | ||
141 | client = new GrpcRemoteServiceProvider(); | 126 | client = new GrpcRemoteServiceProvider(); | ... | ... |
... | @@ -19,6 +19,7 @@ import com.google.common.util.concurrent.MoreExecutors; | ... | @@ -19,6 +19,7 @@ import com.google.common.util.concurrent.MoreExecutors; |
19 | import com.google.common.util.concurrent.Uninterruptibles; | 19 | import com.google.common.util.concurrent.Uninterruptibles; |
20 | 20 | ||
21 | import static org.junit.Assert.*; | 21 | import static org.junit.Assert.*; |
22 | +import static org.onlab.junit.TestTools.findAvailablePort; | ||
22 | 23 | ||
23 | /** | 24 | /** |
24 | * Unit tests for NettyMessaging. | 25 | * Unit tests for NettyMessaging. |
... | @@ -34,10 +35,12 @@ public class NettyMessagingTest { | ... | @@ -34,10 +35,12 @@ public class NettyMessagingTest { |
34 | 35 | ||
35 | @Before | 36 | @Before |
36 | public void setUp() throws Exception { | 37 | public void setUp() throws Exception { |
38 | + ep1 = new Endpoint(IpAddress.valueOf("127.0.0.1"), findAvailablePort(5001)); | ||
37 | netty1 = new NettyMessaging(); | 39 | netty1 = new NettyMessaging(); |
38 | - netty2 = new NettyMessaging(); | ||
39 | - | ||
40 | netty1.start(12, ep1); | 40 | netty1.start(12, ep1); |
41 | + | ||
42 | + ep2 = new Endpoint(IpAddress.valueOf("127.0.0.1"), findAvailablePort(5003)); | ||
43 | + netty2 = new NettyMessaging(); | ||
41 | netty2.start(12, ep2); | 44 | netty2.start(12, ep2); |
42 | } | 45 | } |
43 | 46 | ... | ... |
-
Please register or login to post a comment