[ 
https://issues.apache.org/jira/browse/BEAM-1240?focusedWorklogId=125189&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-125189
 ]

ASF GitHub Bot logged work on BEAM-1240:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 19/Jul/18 19:03
            Start Date: 19/Jul/18 19:03
    Worklog Time Spent: 10m 
      Work Description: jkff commented on a change in pull request #1729: 
[BEAM-1240] Create RabbitMqIO
URL: https://github.com/apache/beam/pull/1729#discussion_r203837965
 
 

 ##########
 File path: 
sdks/java/io/rabbitmq/src/test/java/org/apache/beam/sdk/io/rabbitmq/RabbitMqIOTest.java
 ##########
 @@ -0,0 +1,269 @@
+/*
+ * 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.rabbitmq;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import com.rabbitmq.client.AMQP;
+import com.rabbitmq.client.Channel;
+import com.rabbitmq.client.Connection;
+import com.rabbitmq.client.ConnectionFactory;
+import com.rabbitmq.client.Consumer;
+import com.rabbitmq.client.DefaultConsumer;
+import com.rabbitmq.client.Envelope;
+import java.io.IOException;
+import java.io.Serializable;
+import java.net.ServerSocket;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.qpid.server.Broker;
+import org.apache.qpid.server.BrokerOptions;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Test of {@link RabbitMqIO}. */
+@RunWith(JUnit4.class)
+public class RabbitMqIOTest implements Serializable {
+  private static final Logger LOG = 
LoggerFactory.getLogger(RabbitMqIOTest.class);
+
+  private static int port;
+  @ClassRule public static TemporaryFolder temporaryFolder = new 
TemporaryFolder();
+
+  @Rule public transient TestPipeline p = TestPipeline.create();
+
+  private static transient Broker broker;
+
+  @BeforeClass
+  public static void startBroker() throws Exception {
+    try (ServerSocket serverSocket = new ServerSocket(0)) {
+      port = serverSocket.getLocalPort();
+    }
+
+    System.setProperty("derby.stream.error.field", "MyApp.DEV_NULL");
+    broker = new Broker();
+    BrokerOptions options = new BrokerOptions();
+    options.setConfigProperty(BrokerOptions.QPID_AMQP_PORT, 
String.valueOf(port));
+    options.setConfigProperty(BrokerOptions.QPID_WORK_DIR, 
temporaryFolder.newFolder().toString());
+    options.setConfigProperty(BrokerOptions.QPID_HOME_DIR, "src/test/qpid");
+    broker.startup(options);
+  }
+
+  @AfterClass
+  public static void stopBroker() {
+    broker.shutdown();
+  }
+
+  @Test
+  public void testReadQueue() throws Exception {
+    final int maxNumRecords = 10;
+    PCollection<RabbitMqMessage> raw =
+        p.apply(
+            RabbitMqIO.read()
+                .withUri("amqp://guest:guest@localhost:" + port)
+                .withQueue("READ")
+                .withMaxNumRecords(maxNumRecords));
+    PCollection<byte[]> output = raw.apply(ParDo.of(new ConverterFn()));
+
+    List<byte[]> records = generateRecords(maxNumRecords);
+    PAssert.that(output).containsInAnyOrder(records);
+
+    ConnectionFactory connectionFactory = new ConnectionFactory();
+    connectionFactory.setUri("amqp://guest:guest@localhost:" + port);
+    Connection connection = connectionFactory.newConnection();
+    Channel channel = connection.createChannel();
+    channel.queueDeclare("READ", false, false, false, null);
+    for (byte[] record : records) {
+      channel.basicPublish("", "READ", null, record);
+    }
+
+    p.run();
+
+    channel.close();
 
 Review comment:
   Use try-with-resources, otherwise if the test fails the connection leaks

----------------------------------------------------------------
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:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 125189)
    Time Spent: 8h 40m  (was: 8.5h)

> Create RabbitMqIO
> -----------------
>
>                 Key: BEAM-1240
>                 URL: https://issues.apache.org/jira/browse/BEAM-1240
>             Project: Beam
>          Issue Type: New Feature
>          Components: io-ideas
>            Reporter: Jean-Baptiste Onofré
>            Assignee: Jean-Baptiste Onofré
>            Priority: Major
>             Fix For: 2.6.0
>
>          Time Spent: 8h 40m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to