snuyanzin commented on code in PR #27:
URL: 
https://github.com/apache/flink-connector-gcp-pubsub/pull/27#discussion_r1601877099


##########
flink-connector-gcp-pubsub/src/test/java/org/apache/flink/connector/gcp/pubsub/sink/PubSubWriterTest.java:
##########
@@ -0,0 +1,245 @@
+package org.apache.flink.connector.gcp.pubsub.sink;
+
+import org.apache.flink.api.common.serialization.SimpleStringSchema;
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.connector.base.sink.writer.TestSinkInitContext;
+import 
org.apache.flink.connector.base.sink.writer.TestSinkInitContextAnyThreadMailbox;
+import org.apache.flink.connector.gcp.pubsub.sink.util.TestGcpWriterClient;
+import org.apache.flink.metrics.Counter;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import com.google.protobuf.ByteString;
+import com.google.pubsub.v1.PubsubMessage;
+import io.grpc.StatusRuntimeException;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.util.concurrent.CountDownLatch;
+
+/** Tests for {@link PubSubWriter}. */
+public class PubSubWriterTest {
+
+    private static final int MAXIMUM_INFLIGHT_MESSAGES = 3;
+
+    @Test
+    void writeMessageDeliversMessageUsingClient() throws IOException, 
InterruptedException {
+        TestGcpWriterClient client = new TestGcpWriterClient();
+        PubSubWriter<String> writer =
+                getDefaultWriter(client, new TestSinkInitContext(), 
MAXIMUM_INFLIGHT_MESSAGES);
+        String message = "test-message";
+        writer.write(message, null);
+        client.deliverMessage(message);
+        writer.flush(false);
+
+        
Assertions.assertThat(client.getDeliveredMessages()).containsExactly(message);
+    }
+
+    @Test
+    void writeMessageIncrementsMetricsOnDelivery() throws IOException, 
InterruptedException {
+        TestGcpWriterClient client = new TestGcpWriterClient();
+        WriterInitContext context = new TestSinkInitContext();
+
+        PubSubWriter<String> writer = getDefaultWriter(client, context, 
MAXIMUM_INFLIGHT_MESSAGES);
+        String message = "test-message";
+
+        // write message
+        writer.write(message, null);
+
+        // get metrics before delivery
+        Counter numBytesOutCounter = 
context.metricGroup().getNumBytesSendCounter();
+        Counter numRecordsSendCounter = 
context.metricGroup().getNumRecordsSendCounter();
+        long recordsSentBeforeDelivery = numRecordsSendCounter.getCount();
+        long bytesSentBeforeDelivery = numBytesOutCounter.getCount();
+
+        // deliver message
+        client.deliverMessage(message);
+        writer.flush(false);
+
+        long messageSize =
+                PubsubMessage.newBuilder()
+                        .setData(ByteString.copyFromUtf8(message))
+                        .build()
+                        .getSerializedSize();
+
+        Assertions.assertThat(recordsSentBeforeDelivery).isEqualTo(0);
+        Assertions.assertThat(bytesSentBeforeDelivery).isEqualTo(0);
+        Assertions.assertThat(numRecordsSendCounter.getCount()).isEqualTo(1);
+        
Assertions.assertThat(numBytesOutCounter.getCount()).isEqualTo(messageSize);

Review Comment:
   same here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to