-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solace Read connector: integration tests with testcontainers #31543
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
188 changes: 188 additions & 0 deletions
188
...java/io/solace/src/test/java/org/apache/beam/sdk/io/solace/it/SolaceContainerManager.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,188 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* http://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.apache.beam.sdk.io.solace.it; | ||
|
||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testcontainers.containers.Container.ExecResult; | ||
import org.testcontainers.containers.output.Slf4jLogConsumer; | ||
import org.testcontainers.solace.Service; | ||
import org.testcontainers.solace.SolaceContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
public class SolaceContainerManager { | ||
|
||
public static final String VPN_NAME = "default"; | ||
public static final String PASSWORD = "password"; | ||
public static final String USERNAME = "username"; | ||
public static final String TOPIC_NAME = "test_topic"; | ||
private static final Logger LOG = LoggerFactory.getLogger(SolaceContainerManager.class); | ||
private final SolaceContainer container; | ||
int jcsmpPortMapped = findAvailablePort(); | ||
int sempPortMapped = findAvailablePort(); | ||
|
||
public SolaceContainerManager() throws IOException { | ||
this.container = | ||
new SolaceContainer(DockerImageName.parse("solace/solace-pubsub-standard:10.7")) { | ||
{ | ||
addFixedExposedPort(jcsmpPortMapped, 55555); | ||
addFixedExposedPort(sempPortMapped, 8080); | ||
} | ||
}.withVpn(VPN_NAME) | ||
.withCredentials(USERNAME, PASSWORD) | ||
.withTopic(TOPIC_NAME, Service.SMF) | ||
.withLogConsumer(new Slf4jLogConsumer(LOG)); | ||
} | ||
|
||
public void start() { | ||
container.start(); | ||
} | ||
|
||
void createQueueWithSubscriptionTopic(String queueName) { | ||
executeCommand( | ||
"curl", | ||
"http://localhost:8080/SEMP/v2/config/msgVpns/" + VPN_NAME + "/topicEndpoints", | ||
"-X", | ||
"GET", | ||
"-u", | ||
"admin:admin"); | ||
executeCommand( | ||
"curl", | ||
"http://localhost:8080/SEMP/v2/config/msgVpns/" + VPN_NAME + "/topicEndpoints", | ||
"-X", | ||
"POST", | ||
"-u", | ||
"admin:admin", | ||
"-H", | ||
"Content-Type:application/json", | ||
"-d", | ||
"{\"topicEndpointName\":\"" | ||
+ TOPIC_NAME | ||
+ "\",\"accessType\":\"exclusive\",\"permission\":\"modify-topic\",\"ingressEnabled\":true,\"egressEnabled\":true}"); | ||
executeCommand( | ||
"curl", | ||
"http://localhost:8080/SEMP/v2/config/msgVpns/" + VPN_NAME + "/queues", | ||
"-X", | ||
"POST", | ||
"-u", | ||
"admin:admin", | ||
"-H", | ||
"Content-Type:application/json", | ||
"-d", | ||
"{\"queueName\":\"" | ||
+ queueName | ||
+ "\",\"accessType\":\"non-exclusive\",\"maxMsgSpoolUsage\":200,\"permission\":\"consume\",\"ingressEnabled\":true,\"egressEnabled\":true}"); | ||
executeCommand( | ||
"curl", | ||
"http://localhost:8080/SEMP/v2/config/msgVpns/" | ||
+ VPN_NAME | ||
+ "/queues/" | ||
+ queueName | ||
+ "/subscriptions", | ||
"-X", | ||
"POST", | ||
"-u", | ||
"admin:admin", | ||
"-H", | ||
"Content-Type:application/json", | ||
"-d", | ||
"{\"subscriptionTopic\":\"" + TOPIC_NAME + "\"}"); | ||
} | ||
|
||
private void executeCommand(String... command) { | ||
try { | ||
ExecResult execResult = container.execInContainer(command); | ||
if (execResult.getExitCode() != 0) { | ||
logCommandError(execResult.getStderr(), command); | ||
} else { | ||
LOG.info(execResult.getStdout()); | ||
} | ||
} catch (IOException | InterruptedException e) { | ||
logCommandError(e.getMessage(), command); | ||
} | ||
} | ||
|
||
private void logCommandError(String error, String... command) { | ||
LOG.error("Could not execute command {}: {}", command, error); | ||
} | ||
|
||
public void stop() { | ||
if (container != null) { | ||
container.stop(); | ||
} | ||
} | ||
|
||
public void getQueueDetails(String queueName) { | ||
executeCommand( | ||
"curl", | ||
"http://localhost:8080/SEMP/v2/monitor/msgVpns/" | ||
+ VPN_NAME | ||
+ "/queues/" | ||
+ queueName | ||
+ "/msgs", | ||
"-X", | ||
"GET", | ||
"-u", | ||
"admin:admin"); | ||
} | ||
|
||
public void sendToTopic(String payload, List<String> additionalHeaders) { | ||
// https://docs.solace.com/API/RESTMessagingPrtl/Solace-REST-Message-Encoding.htm | ||
|
||
List<String> command = | ||
new ArrayList<>( | ||
Arrays.asList( | ||
"curl", | ||
"http://localhost:9000/TOPIC/" + TOPIC_NAME, | ||
"-X", | ||
"POST", | ||
"-u", | ||
USERNAME + ":" + PASSWORD, | ||
"--header", | ||
"Content-Type:application/json", | ||
"-d", | ||
payload)); | ||
|
||
for (String additionalHeader : additionalHeaders) { | ||
command.add("--header"); | ||
command.add(additionalHeader); | ||
} | ||
|
||
executeCommand(command.toArray(new String[0])); | ||
} | ||
|
||
private static int findAvailablePort() throws IOException { | ||
ServerSocket s = new ServerSocket(0); | ||
try { | ||
return s.getLocalPort(); | ||
} finally { | ||
s.close(); | ||
try { | ||
// Some systems don't free the port for future use immediately. | ||
Thread.sleep(100); | ||
} catch (InterruptedException exn) { | ||
// ignore | ||
} | ||
} | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
sdks/java/io/solace/src/test/java/org/apache/beam/sdk/io/solace/it/SolaceIOIT.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,129 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* http://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.apache.beam.sdk.io.solace.it; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.IOException; | ||
import org.apache.beam.sdk.PipelineResult; | ||
import org.apache.beam.sdk.io.solace.SolaceIO; | ||
import org.apache.beam.sdk.io.solace.broker.BasicAuthJcsmpSessionServiceFactory; | ||
import org.apache.beam.sdk.io.solace.broker.BasicAuthSempClientFactory; | ||
import org.apache.beam.sdk.io.solace.data.Solace.Queue; | ||
import org.apache.beam.sdk.metrics.Counter; | ||
import org.apache.beam.sdk.metrics.Metrics; | ||
import org.apache.beam.sdk.options.PipelineOptionsFactory; | ||
import org.apache.beam.sdk.options.StreamingOptions; | ||
import org.apache.beam.sdk.testing.TestPipeline; | ||
import org.apache.beam.sdk.testing.TestPipelineOptions; | ||
import org.apache.beam.sdk.testutils.metrics.MetricsReader; | ||
import org.apache.beam.sdk.transforms.DoFn; | ||
import org.apache.beam.sdk.transforms.ParDo; | ||
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList; | ||
import org.joda.time.Duration; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class SolaceIOIT { | ||
private static final String NAMESPACE = SolaceIOIT.class.getName(); | ||
private static final String READ_COUNT = "read_count"; | ||
private static SolaceContainerManager solaceContainerManager; | ||
private static final TestPipelineOptions readPipelineOptions; | ||
|
||
static { | ||
readPipelineOptions = PipelineOptionsFactory.create().as(TestPipelineOptions.class); | ||
readPipelineOptions.setBlockOnRun(false); | ||
readPipelineOptions.as(TestPipelineOptions.class).setBlockOnRun(false); | ||
readPipelineOptions.as(StreamingOptions.class).setStreaming(false); | ||
} | ||
|
||
@Rule public final TestPipeline readPipeline = TestPipeline.fromOptions(readPipelineOptions); | ||
|
||
@BeforeClass | ||
public static void setup() throws IOException { | ||
solaceContainerManager = new SolaceContainerManager(); | ||
solaceContainerManager.start(); | ||
} | ||
|
||
@AfterClass | ||
public static void afterClass() { | ||
if (solaceContainerManager != null) { | ||
solaceContainerManager.stop(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRead() { | ||
String queueName = "test_queue"; | ||
solaceContainerManager.createQueueWithSubscriptionTopic(queueName); | ||
|
||
// todo this is very slow, needs to be replaced with the SolaceIO.write connector. | ||
int publishMessagesCount = 20; | ||
for (int i = 0; i < publishMessagesCount; i++) { | ||
solaceContainerManager.sendToTopic( | ||
"{\"field_str\":\"value\",\"field_int\":123}", | ||
ImmutableList.of("Solace-Message-ID:m" + i)); | ||
} | ||
|
||
readPipeline | ||
.apply( | ||
"Read from Solace", | ||
SolaceIO.read() | ||
.from(Queue.fromName(queueName)) | ||
.withMaxNumConnections(1) | ||
.withSempClientFactory( | ||
BasicAuthSempClientFactory.builder() | ||
.host("http://localhost:" + solaceContainerManager.sempPortMapped) | ||
.username("admin") | ||
.password("admin") | ||
.vpnName(SolaceContainerManager.VPN_NAME) | ||
.build()) | ||
.withSessionServiceFactory( | ||
BasicAuthJcsmpSessionServiceFactory.builder() | ||
.host("localhost:" + solaceContainerManager.jcsmpPortMapped) | ||
.username(SolaceContainerManager.USERNAME) | ||
.password(SolaceContainerManager.PASSWORD) | ||
.vpnName(SolaceContainerManager.VPN_NAME) | ||
.build())) | ||
.apply("Count", ParDo.of(new CountingFn<>(NAMESPACE, READ_COUNT))); | ||
|
||
PipelineResult pipelineResult = readPipeline.run(); | ||
pipelineResult.waitUntilFinish(Duration.standardSeconds(15)); | ||
|
||
MetricsReader metricsReader = new MetricsReader(pipelineResult, NAMESPACE); | ||
long actualRecordsCount = metricsReader.getCounterMetric(READ_COUNT); | ||
assertEquals(publishMessagesCount, actualRecordsCount); | ||
} | ||
|
||
private static class CountingFn<T> extends DoFn<T, T> { | ||
|
||
private final Counter elementCounter; | ||
|
||
CountingFn(String namespace, String name) { | ||
elementCounter = Metrics.counter(namespace, name); | ||
} | ||
|
||
@ProcessElement | ||
public void processElement(@Element T record, OutputReceiver<T> c) { | ||
elementCounter.inc(1L); | ||
c.output(record); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this slow? if its because of syncronous work, you could spin up a parallel thread pool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I actually meant here is that this method may not be ideal for performance testing with a large number of messages, as it might prove too slow. Parallelization for thousands of messages would likely offer too little improvement. For performance testing we need the write connector, which will come later.
For the current use case with 20 messages, inserting messages takes 2-second processing time on my machine. However, considering the overall Testcontainers setup time, this delay is relatively minor. WDYT?