Skip to content

Commit

Permalink
chore: Wrapping local test Server starts within a retry to reduce tes…
Browse files Browse the repository at this point in the history
…t flakiness (#3041)

* Wrapping local test Server starts within a retry to reduce test flakiness.

* Wrapping local test Server starts within a retry to reduce test flakiness.

* Wrapping local test Server starts within a retry to reduce test flakiness.

* Wrapping local test Server starts within a retry to reduce test flakiness.
  • Loading branch information
shantstepanian authored Jun 30, 2021
1 parent 48951ab commit 6b3e524
Show file tree
Hide file tree
Showing 21 changed files with 318 additions and 146 deletions.
7 changes: 7 additions & 0 deletions bigtable-client-core-parent/bigtable-client-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ limitations under the License.
</dependency>

<!-- Test -->
<dependency>
<groupId>com.google.cloud.bigtable</groupId>
<artifactId>bigtable-internal-test-helper</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.grpc.async.BulkMutation;
import com.google.cloud.bigtable.grpc.async.BulkRead;
import com.google.cloud.bigtable.test.helper.TestServerBuilder;
import com.google.common.base.Preconditions;
import com.google.common.collect.Queues;
import com.google.protobuf.ByteString;
import com.google.rpc.Code;
import com.google.rpc.Status;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import org.junit.After;
Expand All @@ -65,18 +64,13 @@ public class TestAppProfile {
public void setUp() throws IOException {
fakeDataService = new FakeDataService();

final int port;
try (ServerSocket s = new ServerSocket(0)) {
port = s.getLocalPort();
}
server = ServerBuilder.forPort(port).addService(fakeDataService).build();
server.start();
server = TestServerBuilder.newInstance().addService(fakeDataService).buildAndStart();

BigtableOptions opts =
BigtableOptions.builder()
.setDataHost("localhost")
.setAdminHost("localhost")
.setPort(port)
.setPort(server.getPort())
.setProjectId("fake-project")
.setInstanceId("fake-instance")
.setUserAgent("fake-agent")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@
import com.google.cloud.bigtable.config.CredentialOptions;
import com.google.cloud.bigtable.config.Logger;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.test.helper.TestServerBuilder;
import io.grpc.ForwardingServerCall;
import io.grpc.Metadata;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.grpc.ServerInterceptors;
import io.grpc.stub.StreamObserver;
import java.net.ServerSocket;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
import org.junit.After;
Expand Down Expand Up @@ -87,12 +86,8 @@ public void tearDown() throws Exception {
*/
@Test
public void testCBC_UserAgentUsingPlainTextNegotiation() throws Exception {
ServerSocket serverSocket = new ServerSocket(0);
final int availablePort = serverSocket.getLocalPort();
serverSocket.close();

// Creates non-ssl server.
createServer(availablePort);
createServer();

BigtableOptions bigtableOptions =
BigtableOptions.builder()
Expand All @@ -103,7 +98,7 @@ public void testCBC_UserAgentUsingPlainTextNegotiation() throws Exception {
.setUserAgent(TEST_USER_AGENT)
.setUsePlaintextNegotiation(true)
.setCredentialOptions(CredentialOptions.nullCredential())
.setPort(availablePort)
.setPort(server.getPort())
.build();

xGoogApiPattern = Pattern.compile(".* cbt/.*");
Expand All @@ -115,12 +110,8 @@ public void testCBC_UserAgentUsingPlainTextNegotiation() throws Exception {

@Test
public void testCBC_tracingCookie() throws Exception {
ServerSocket serverSocket = new ServerSocket(0);
final int availablePort = serverSocket.getLocalPort();
serverSocket.close();

// Creates non-ssl server.
createServer(availablePort);
createServer();

BigtableOptions bigtableOptions =
BigtableOptions.builder()
Expand All @@ -131,7 +122,7 @@ public void testCBC_tracingCookie() throws Exception {
.setUserAgent(TEST_USER_AGENT)
.setUsePlaintextNegotiation(true)
.setCredentialOptions(CredentialOptions.nullCredential())
.setPort(availablePort)
.setPort(server.getPort())
.setTracingCookie(TEST_TRACING_COOKIE)
.build();
testTracingCookie.set(true);
Expand All @@ -144,17 +135,17 @@ public void testCBC_tracingCookie() throws Exception {
}

/** Creates simple server to intercept plainText Negotiation RPCs. */
private void createServer(int port) throws Exception {
private void createServer() throws Exception {

server =
ServerBuilder.forPort(port)
TestServerBuilder.newInstance()
.addService(
ServerInterceptors.intercept(
new BigtableExtendedImpl(), new HeaderServerInterceptor()))
.addService(
ServerInterceptors.intercept(
new BigtableAdminExtendedImpl(), new HeaderServerInterceptor()))
.build();
server.start();
.buildAndStart();
}

/**
Expand Down
6 changes: 6 additions & 0 deletions bigtable-client-core-parent/bigtable-hbase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ limitations under the License.
</dependency>

<!-- Test -->
<dependency>
<groupId>com.google.cloud.bigtable</groupId>
<artifactId>bigtable-internal-test-helper</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>grpc-google-common-protos</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@
import com.google.cloud.bigtable.hbase.BigtableOptionsFactory;
import com.google.cloud.bigtable.hbase.wrappers.BigtableApi;
import com.google.cloud.bigtable.hbase.wrappers.BigtableHBaseSettings;
import com.google.cloud.bigtable.test.helper.TestServerBuilder;
import com.google.common.collect.Queues;
import com.google.protobuf.Empty;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import org.apache.hadoop.conf.Configuration;
Expand All @@ -59,22 +58,17 @@ public class TestBigtableClassicApi {
private static FakeDataService fakeDataService = new FakeDataService();
private static FakeAdminService fakeAdminService = new FakeAdminService();
private static Server server;
private static int port;

private BigtableHBaseSettings bigtableHBaseSettings;
private BigtableApi bigtableApi;

@BeforeClass
public static void setUpServer() throws IOException {
try (ServerSocket s = new ServerSocket(0)) {
port = s.getLocalPort();
}
server =
ServerBuilder.forPort(port)
TestServerBuilder.newInstance()
.addService(fakeDataService)
.addService(fakeAdminService)
.build();
server.start();
.buildAndStart();
}

@AfterClass
Expand All @@ -92,7 +86,8 @@ public void setUp() throws IOException {
configuration.set(BigtableOptionsFactory.INSTANCE_ID_KEY, TEST_INSTANCE_ID);
configuration.set(BigtableOptionsFactory.BIGTABLE_NULL_CREDENTIAL_ENABLE_KEY, "true");
configuration.set(BigtableOptionsFactory.BIGTABLE_DATA_CHANNEL_COUNT_KEY, "1");
configuration.set(BigtableOptionsFactory.BIGTABLE_EMULATOR_HOST_KEY, "localhost:" + port);
configuration.set(
BigtableOptionsFactory.BIGTABLE_EMULATOR_HOST_KEY, "localhost:" + server.getPort());
configuration.setBoolean(BigtableOptionsFactory.BIGTABLE_USE_GCJ_CLIENT, false);
bigtableHBaseSettings = BigtableHBaseClassicSettings.create(configuration);
bigtableApi = BigtableApi.create(bigtableHBaseSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.hbase.BigtableOptionsFactory;
import com.google.cloud.bigtable.hbase.wrappers.DataClientWrapper;
import com.google.cloud.bigtable.test.helper.TestServerBuilder;
import io.grpc.Grpc;
import io.grpc.Metadata;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.ServerCall;
import io.grpc.ServerCall.Listener;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -58,23 +57,17 @@
@RunWith(JUnit4.class)
public class SharedDataClientWrapperFactoryTest {
private Server server;
private int port;
private List<SocketAddress> remoteCallers;

@Before
public void setUp() throws IOException {
try (ServerSocket ss = new ServerSocket(0)) {
port = ss.getLocalPort();
}

remoteCallers = Collections.synchronizedList(new ArrayList<SocketAddress>());

server =
ServerBuilder.forPort(port)
TestServerBuilder.newInstance()
.intercept(new RemoteCallerInterceptor(remoteCallers))
.addService(new FakeBigtable())
.build();
server.start();
.buildAndStart();
}

@After
Expand All @@ -94,7 +87,7 @@ public void testChannelsAreShared() throws Exception {

// Manually expand BIGTABLE_EMULATOR_HOST_KEY so that the channel settings are known
configuration.set(BigtableOptionsFactory.BIGTABLE_HOST_KEY, "localhost");
configuration.setInt(BigtableOptionsFactory.BIGTABLE_PORT_KEY, port);
configuration.setInt(BigtableOptionsFactory.BIGTABLE_PORT_KEY, server.getPort());
configuration.setBoolean(BigtableOptionsFactory.BIGTABLE_NULL_CREDENTIAL_ENABLE_KEY, true);
configuration.setBoolean(BigtableOptionsFactory.BIGTABLE_USE_PLAINTEXT_NEGOTIATION, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.bigtable.admin.v2.models.Table;
import com.google.cloud.bigtable.grpc.BigtableClusterName;
import com.google.cloud.bigtable.grpc.BigtableInstanceName;
import com.google.cloud.bigtable.test.helper.TestServerBuilder;
import com.google.common.collect.Queues;
import com.google.longrunning.GetOperationRequest;
import com.google.longrunning.Operation;
Expand All @@ -34,9 +35,7 @@
import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.net.ServerSocket;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.BlockingQueue;
Expand Down Expand Up @@ -75,20 +74,14 @@ public class TestAdminClientVeneerApi {

@Before
public void setUp() throws Exception {
final int port;
try (ServerSocket serverSocket = new ServerSocket(0)) {
port = serverSocket.getLocalPort();
}

server =
ServerBuilder.forPort(port)
TestServerBuilder.newInstance()
.addService(fakeOperationsGrpc)
.addService(fakeAdminService)
.build();
server.start();
.buildAndStart();
adminClientV2 =
BigtableTableAdminClient.create(
BigtableTableAdminSettings.newBuilderForEmulator(port)
BigtableTableAdminSettings.newBuilderForEmulator(server.getPort())
.setProjectId(PROJECT_ID)
.setInstanceId(INSTANCE_ID)
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
import com.google.common.base.Optional;
import io.grpc.internal.GrpcUtil;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Map;
import org.apache.hadoop.conf.Configuration;
import org.junit.Before;
Expand Down Expand Up @@ -396,9 +395,9 @@ public void testDefaultThrottlingDisabled() throws IOException {

@Test
public void testDataSettingsWithEmulator() throws IOException {
ServerSocket serverSocket = new ServerSocket(0);
final int availablePort = serverSocket.getLocalPort();
serverSocket.close();
// A real port isn't required for this test; only verifying the configs can be stored and
// retrieved.
final int availablePort = 987654321;
String emulatorHost = "localhost:" + availablePort;
configuration.set(BIGTABLE_EMULATOR_HOST_KEY, emulatorHost);

Expand All @@ -413,9 +412,9 @@ public void testDataSettingsWithEmulator() throws IOException {

@Test
public void testAdminSettingsWithEmulator() throws IOException {
ServerSocket serverSocket = new ServerSocket(0);
final int availablePort = serverSocket.getLocalPort();
serverSocket.close();
// A real port isn't required for this test; only verifying the configs can be stored and
// retrieved.
final int availablePort = 987654321;
String emulatorHost = "localhost:" + availablePort;
configuration.set(BIGTABLE_EMULATOR_HOST_KEY, emulatorHost);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@
import com.google.cloud.bigtable.hbase.wrappers.BigtableApi;
import com.google.cloud.bigtable.hbase.wrappers.BigtableHBaseSettings;
import com.google.cloud.bigtable.hbase.wrappers.classic.BigtableHBaseClassicSettings;
import com.google.cloud.bigtable.test.helper.TestServerBuilder;
import com.google.common.collect.Queues;
import com.google.protobuf.Empty;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import org.apache.hadoop.conf.Configuration;
Expand All @@ -62,23 +61,19 @@ public class TestBigtableVeneerApi {

@Before
public void setUp() throws IOException {
final int port;
try (ServerSocket s = new ServerSocket(0)) {
port = s.getLocalPort();
}
server =
ServerBuilder.forPort(port)
TestServerBuilder.newInstance()
.addService(fakeDataService)
.addService(fakeAdminService)
.build();
server.start();
.buildAndStart();

Configuration configuration = new Configuration(false);
configuration.set(BigtableOptionsFactory.PROJECT_ID_KEY, TEST_PROJECT_ID);
configuration.set(BigtableOptionsFactory.INSTANCE_ID_KEY, TEST_INSTANCE_ID);
configuration.set(BigtableOptionsFactory.BIGTABLE_NULL_CREDENTIAL_ENABLE_KEY, "true");
configuration.set(BigtableOptionsFactory.BIGTABLE_DATA_CHANNEL_COUNT_KEY, "1");
configuration.set(BigtableOptionsFactory.BIGTABLE_EMULATOR_HOST_KEY, "localhost:" + port);
configuration.set(
BigtableOptionsFactory.BIGTABLE_EMULATOR_HOST_KEY, "localhost:" + server.getPort());
configuration.set(BigtableOptionsFactory.BIGTABLE_USE_GCJ_CLIENT, "true");
bigtableHBaseSettings = BigtableHBaseClassicSettings.create(configuration);
bigtableApi = BigtableApi.create(bigtableHBaseSettings);
Expand Down
Loading

0 comments on commit 6b3e524

Please sign in to comment.