-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-35548] Add E2E tests for PubSubSinkV2
- Loading branch information
1 parent
baafc39
commit 1e45ee3
Showing
10 changed files
with
219 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...e-tests/src/test/java/org/apache/flink/connector/gcp/pubsub/sink/PubSubSinkV2ITTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package org.apache.flink.connector.gcp.pubsub.sink; | ||
|
||
import org.apache.flink.api.common.eventtime.WatermarkStrategy; | ||
import org.apache.flink.api.common.serialization.SimpleStringSchema; | ||
import org.apache.flink.api.common.typeinfo.BasicTypeInfo; | ||
import org.apache.flink.api.common.typeinfo.TypeInformation; | ||
import org.apache.flink.connector.datagen.functions.FromElementsGeneratorFunction; | ||
import org.apache.flink.connector.datagen.source.DataGeneratorSource; | ||
import org.apache.flink.connector.gcp.pubsub.sink.config.GcpPublisherConfig; | ||
import org.apache.flink.connector.gcp.pubsub.sink.util.PubsubHelper; | ||
import org.apache.flink.connector.gcp.pubsub.sink.util.TestChannelProvider; | ||
import org.apache.flink.streaming.api.datastream.DataStreamSource; | ||
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; | ||
import org.apache.flink.streaming.connectors.gcp.pubsub.emulator.EmulatorCredentialsProvider; | ||
import org.apache.flink.streaming.connectors.gcp.pubsub.test.DockerImageVersions; | ||
import org.apache.flink.test.junit5.MiniClusterExtension; | ||
import org.apache.flink.util.TestLogger; | ||
|
||
import com.google.pubsub.v1.ReceivedMessage; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.parallel.Execution; | ||
import org.junit.jupiter.api.parallel.ExecutionMode; | ||
import org.testcontainers.containers.PubSubEmulatorContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** Integration tests for {@link PubSubSinkV2} using {@link PubSubEmulatorContainer}. */ | ||
@ExtendWith(MiniClusterExtension.class) | ||
@Execution(ExecutionMode.CONCURRENT) | ||
@Testcontainers | ||
class PubSubSinkV2ITTests extends TestLogger { | ||
|
||
private static final String PROJECT_ID = "test-project"; | ||
|
||
private static final String TOPIC_ID = "test-topic"; | ||
|
||
private static final String SUBSCRIPTION_ID = "test-subscription"; | ||
|
||
private StreamExecutionEnvironment env; | ||
|
||
@Container | ||
private static final PubSubEmulatorContainer PUB_SUB_EMULATOR_CONTAINER = | ||
new PubSubEmulatorContainer( | ||
DockerImageName.parse(DockerImageVersions.GOOGLE_CLOUD_PUBSUB_EMULATOR)); | ||
|
||
private PubsubHelper pubSubHelper; | ||
|
||
@BeforeEach | ||
void setUp() throws IOException { | ||
env = StreamExecutionEnvironment.getExecutionEnvironment(); | ||
env.setParallelism(1); | ||
pubSubHelper = new PubsubHelper(PUB_SUB_EMULATOR_CONTAINER.getEmulatorEndpoint()); | ||
|
||
pubSubHelper.createTopic(PROJECT_ID, TOPIC_ID); | ||
pubSubHelper.createSubscription(PROJECT_ID, SUBSCRIPTION_ID, PROJECT_ID, TOPIC_ID); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() throws IOException { | ||
pubSubHelper.deleteSubscription(PROJECT_ID, SUBSCRIPTION_ID); | ||
pubSubHelper.deleteTopic(PROJECT_ID, TOPIC_ID); | ||
pubSubHelper.close(); | ||
} | ||
|
||
@Test | ||
void pubSubSinkV2DeliversRecords() throws Exception { | ||
String[] elements = new String[] {"test1", "test2", "test3"}; | ||
DataStreamSource<String> stream = | ||
env.fromSource( | ||
new DataGeneratorSource<>( | ||
new FromElementsGeneratorFunction<>( | ||
BasicTypeInfo.STRING_TYPE_INFO, elements), | ||
elements.length, | ||
TypeInformation.of(String.class)), | ||
WatermarkStrategy.noWatermarks(), | ||
"DataGeneratorSource"); | ||
|
||
GcpPublisherConfig gcpPublisherConfig = | ||
GcpPublisherConfig.builder() | ||
.setCredentialsProvider(EmulatorCredentialsProvider.create()) | ||
.setTransportChannelProvider( | ||
new TestChannelProvider( | ||
PUB_SUB_EMULATOR_CONTAINER.getEmulatorEndpoint())) | ||
.build(); | ||
|
||
PubSubSinkV2<String> sink = | ||
PubSubSinkV2.<String>builder() | ||
.setProjectId(PROJECT_ID) | ||
.setTopicId(TOPIC_ID) | ||
.setSerializationSchema(new SimpleStringSchema()) | ||
.setGcpPublisherConfig(gcpPublisherConfig) | ||
.setFailOnError(true) | ||
.build(); | ||
|
||
stream.sinkTo(sink); | ||
|
||
env.execute("PubSubSinkV2ITTests"); | ||
List<ReceivedMessage> receivedMessages = | ||
pubSubHelper.pullMessages(PROJECT_ID, SUBSCRIPTION_ID, 100); | ||
|
||
Assertions.assertThat(receivedMessages).hasSameSizeAs(elements); | ||
Assertions.assertThat(receivedMessages) | ||
.extracting(ReceivedMessage::getMessage) | ||
.extracting(message -> message.getData().toStringUtf8()) | ||
.containsExactlyInAnyOrder(elements); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ts/src/test/java/org/apache/flink/connector/gcp/pubsub/sink/util/TestChannelProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.apache.flink.connector.gcp.pubsub.sink.util; | ||
|
||
import org.apache.flink.connector.gcp.pubsub.sink.config.SerializableTransportChannelProvider; | ||
|
||
import com.google.api.gax.grpc.GrpcTransportChannel; | ||
import com.google.api.gax.rpc.FixedTransportChannelProvider; | ||
import io.grpc.ManagedChannel; | ||
import io.grpc.ManagedChannelBuilder; | ||
|
||
/** A test channel provider for {@link GrpcTransportChannel}. */ | ||
public class TestChannelProvider extends SerializableTransportChannelProvider { | ||
|
||
private final String endpoint; | ||
|
||
public TestChannelProvider(String endpoint) { | ||
this.endpoint = endpoint; | ||
} | ||
|
||
@Override | ||
protected void open() { | ||
ManagedChannel managedChannel = | ||
ManagedChannelBuilder.forTarget(endpoint).usePlaintext().build(); | ||
this.transportChannelProvider = | ||
FixedTransportChannelProvider.create(GrpcTransportChannel.create(managedChannel)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters