[
https://issues.apache.org/jira/browse/BEAM-3336?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16324990#comment-16324990
]
ASF GitHub Bot commented on BEAM-3336:
--------------------------------------
lukecwik closed pull request #4406: [BEAM-3336] Fix thread safety issues of
MqttIOTest
URL: https://github.com/apache/beam/pull/4406
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/sdks/java/io/mqtt/src/test/java/org/apache/beam/sdk/io/mqtt/MqttIOTest.java
b/sdks/java/io/mqtt/src/test/java/org/apache/beam/sdk/io/mqtt/MqttIOTest.java
index d7baf3bf989..94ab0abc91f 100644
---
a/sdks/java/io/mqtt/src/test/java/org/apache/beam/sdk/io/mqtt/MqttIOTest.java
+++
b/sdks/java/io/mqtt/src/test/java/org/apache/beam/sdk/io/mqtt/MqttIOTest.java
@@ -22,9 +22,8 @@
import java.net.ServerSocket;
import java.util.ArrayList;
-import java.util.HashSet;
import java.util.Set;
-
+import java.util.concurrent.ConcurrentSkipListSet;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.Connection;
import org.apache.beam.sdk.io.mqtt.MqttIO.Read;
@@ -53,9 +52,9 @@
private static final Logger LOG = LoggerFactory.getLogger(MqttIOTest.class);
- private static transient BrokerService brokerService;
+ private BrokerService brokerService;
- private static int port;
+ private int port;
@Rule
public final transient TestPipeline pipeline = TestPipeline.create();
@@ -63,9 +62,9 @@
@Before
public void startBroker() throws Exception {
LOG.info("Finding free network port");
- ServerSocket socket = new ServerSocket(0);
- port = socket.getLocalPort();
- socket.close();
+ try (ServerSocket socket = new ServerSocket(0)) {
+ port = socket.getLocalPort();
+ }
LOG.info("Starting ActiveMQ brokerService on {}", port);
brokerService = new BrokerService();
@@ -81,11 +80,9 @@ public void startBroker() throws Exception {
public void testReadNoClientId() throws Exception {
final String topicName = "READ_TOPIC_NO_CLIENT_ID";
Read mqttReader = MqttIO.read()
- .withConnectionConfiguration(
- MqttIO.ConnectionConfiguration.create(
- "tcp://localhost:" + port,
- topicName))
- .withMaxNumRecords(10);
+ .withConnectionConfiguration(
+ MqttIO.ConnectionConfiguration.create("tcp://localhost:" + port,
topicName))
+ .withMaxNumRecords(10);
PCollection<byte[]> output = pipeline.apply(mqttReader);
PAssert.that(output).containsInAnyOrder(
"This is test 0".getBytes(),
@@ -122,7 +119,7 @@ public void run() {
}
for (int i = 0; i < 10; i++) {
publishConnection.publish(topicName, ("This is test " +
i).getBytes(),
- QoS.AT_LEAST_ONCE, false);
+ QoS.EXACTLY_ONCE, false);
}
} catch (Exception e) {
// nothing to do
@@ -136,7 +133,7 @@ public void run() {
publisherThread.join();
}
- @Test(timeout = 5 * 1000)
+ @Test(timeout = 30 * 1000)
public void testRead() throws Exception {
PCollection<byte[]> output = pipeline.apply(
MqttIO.read()
@@ -181,7 +178,7 @@ public void run() {
}
for (int i = 0; i < 10; i++) {
publishConnection.publish("READ_TOPIC", ("This is test " +
i).getBytes(),
- QoS.AT_LEAST_ONCE, false);
+ QoS.EXACTLY_ONCE, false);
}
} catch (Exception e) {
// nothing to do
@@ -191,14 +188,14 @@ public void run() {
publisherThread.start();
pipeline.run();
- publishConnection.disconnect();
publisherThread.join();
+ publishConnection.disconnect();
}
/**
* Test for BEAM-3282: this test should not timeout.
*/
- @Test(timeout = 5 * 1000)
+ @Test(timeout = 30 * 1000)
public void testReceiveWithTimeoutAndNoData() throws Exception {
pipeline.apply(MqttIO.read()
.withConnectionConfiguration(
@@ -213,18 +210,19 @@ public void testReceiveWithTimeoutAndNoData() throws
Exception {
@Test
public void testWrite() throws Exception {
+ final int numberOfTestMessages = 200;
MQTT client = new MQTT();
client.setHost("tcp://localhost:" + port);
final BlockingConnection connection = client.blockingConnection();
connection.connect();
- connection.subscribe(new Topic[]{new Topic(Buffer.utf8("WRITE_TOPIC"),
QoS.AT_LEAST_ONCE)});
+ connection.subscribe(new Topic[]{new Topic(Buffer.utf8("WRITE_TOPIC"),
QoS.EXACTLY_ONCE)});
- final Set<String> messages = new HashSet<>();
+ final Set<String> messages = new ConcurrentSkipListSet<>();
Thread subscriber = new Thread() {
public void run() {
try {
- for (int i = 0; i < 200; i++) {
+ for (int i = 0; i < numberOfTestMessages; i++) {
Message message = connection.receive();
messages.add(new String(message.getPayload()));
message.ack();
@@ -237,7 +235,7 @@ public void run() {
subscriber.start();
ArrayList<byte[]> data = new ArrayList<>();
- for (int i = 0; i < 200; i++) {
+ for (int i = 0; i < numberOfTestMessages; i++) {
data.add(("Test " + i).getBytes());
}
pipeline.apply(Create.of(data))
@@ -251,8 +249,8 @@ public void run() {
connection.disconnect();
- assertEquals(200, messages.size());
- for (int i = 0; i < 200; i++) {
+ assertEquals(numberOfTestMessages, messages.size());
+ for (int i = 0; i < numberOfTestMessages; i++) {
assertTrue(messages.contains("Test " + i));
}
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> MqttIO read tests are flaky
> ---------------------------
>
> Key: BEAM-3336
> URL: https://issues.apache.org/jira/browse/BEAM-3336
> Project: Beam
> Issue Type: Bug
> Components: sdk-java-extensions
> Reporter: Thomas Groh
> Assignee: Luke Cwik
> Fix For: 2.3.0
>
>
> example failure:
> https://builds.apache.org/job/beam_PreCommit_Java_GradleBuild/449/
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)