Add utility method to find and reserve allocated network ports.

This commit is contained in:
John Blum
2020-04-15 13:12:35 -07:00
parent 5137a8a3bd
commit 6dec5b8d73
2 changed files with 72 additions and 0 deletions

View File

@@ -24,9 +24,13 @@ import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.AfterClass;
import org.apache.geode.cache.server.CacheServer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -66,6 +70,13 @@ public abstract class ClientServerIntegrationTestsSupport extends IntegrationTes
protected static final String DEBUGGING_ENABLED_PROPERTY = "spring.data.gemfire.test.debugging.enabled";
protected static final String PROCESS_RUN_MANUAL_PROPERTY = "spring.data.gemfire.test.process.run-manual";
private static final Set<Integer> allocatedPorts = new ConcurrentSkipListSet<>();
@AfterClass
public static void deallocatePorts() {
allocatedPorts.clear();
}
protected static int findAvailablePort() throws IOException {
ServerSocket serverSocket = null;
@@ -82,6 +93,18 @@ public abstract class ClientServerIntegrationTestsSupport extends IntegrationTes
}
}
protected static int findAndReserveAvailablePort() throws IOException {
int availablePort = 0;
do {
availablePort = findAvailablePort();
}
while (!allocatedPorts.add(availablePort));
return availablePort;
}
protected static int intValue(Number number) {
return number != null ? number.intValue() : 0;
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.springframework.data.gemfire.tests.integration;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
/**
* Integration Tests for {@link ClientServerIntegrationTestsSupport}.
*
* @author John Blum
* @see org.junit.Test
* @see org.springframework.data.gemfire.tests.integration.ClientServerIntegrationTests
* @since 0.0.14.RELEASE
*/
public class ClientServerIntegrationTestsSupportTests extends ClientServerIntegrationTestsSupport {
@Test
public void allocatedPortsAreDifferent() throws IOException {
int expectedSize = 3;
Set<Integer> allocatedPorts = new HashSet<>();
for (int index = 0; index < expectedSize; index++) {
allocatedPorts.add(findAndReserveAvailablePort());
}
assertThat(allocatedPorts).hasSize(expectedSize);
}
}