From 4058779f357476aeb438083967a4db4059744b6c Mon Sep 17 00:00:00 2001 From: Jani Simomaa Date: Thu, 16 Dec 2021 11:51:03 +0200 Subject: [PATCH 1/2] fix: publishing runtime error as a WriteErrorEvent (#291) --- CHANGELOG.md | 1 + .../client/internal/AbstractWriteClient.java | 4 +- ...lishRuntimeErrorAsWriteErrorEventTest.java | 72 +++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 client/src/test/java/com/influxdb/client/internal/PublishRuntimeErrorAsWriteErrorEventTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d39da41479..fb8fadfa196 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ 1. [#285](https://github.com/influxdata/influxdb-client-java/pull/285): Default dialect for Query APIs 1. [#294](https://github.com/influxdata/influxdb-client-java/pull/294): Mapping measurement with primitive `float` 1. [#297](https://github.com/influxdata/influxdb-client-java/pull/297): Transient dependency of `okhttp`, `retrofit` and `rxjava` +1. [#292](https://github.com/influxdata/influxdb-client-java/pull/292): Publishing runtime error as a WriteErrorEvent ## 4.0.0 [2021-11-26] diff --git a/client/src/main/java/com/influxdb/client/internal/AbstractWriteClient.java b/client/src/main/java/com/influxdb/client/internal/AbstractWriteClient.java index e14232bc995..0c60a52e6e8 100644 --- a/client/src/main/java/com/influxdb/client/internal/AbstractWriteClient.java +++ b/client/src/main/java/com/influxdb/client/internal/AbstractWriteClient.java @@ -181,7 +181,7 @@ public AbstractWriteClient(@Nonnull final WriteOptions writeOptions, if (responseNotification.isOnError()) { publish(new WriteErrorEvent(toInfluxException(responseNotification.getError()))); } - }, throwable -> new WriteErrorEvent(toInfluxException(throwable))); + }, throwable -> publish(new WriteErrorEvent(toInfluxException(throwable)))); autoCloseables.add(this); } @@ -594,4 +594,4 @@ static void waitToCondition(final Supplier condition, final int millis) } } } -} \ No newline at end of file +} diff --git a/client/src/test/java/com/influxdb/client/internal/PublishRuntimeErrorAsWriteErrorEventTest.java b/client/src/test/java/com/influxdb/client/internal/PublishRuntimeErrorAsWriteErrorEventTest.java new file mode 100644 index 00000000000..f48bc1560d5 --- /dev/null +++ b/client/src/test/java/com/influxdb/client/internal/PublishRuntimeErrorAsWriteErrorEventTest.java @@ -0,0 +1,72 @@ +/* + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.influxdb.client.internal; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; + +import java.time.Instant; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import com.influxdb.client.WriteApi; +import com.influxdb.client.domain.WritePrecision; +import com.influxdb.client.write.Point; +import com.influxdb.client.write.events.WriteErrorEvent; + +import org.junit.jupiter.api.Test; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; + +/** + * This test ensures publish is called with WriteErrorEvent as described in + * https://github.com/influxdata/influxdb-client-java/issues/291 + */ +@RunWith(JUnitPlatform.class) +public class PublishRuntimeErrorAsWriteErrorEvent extends AbstractInfluxDBClientTest { + + @Test + void publishRuntimeErrorAsWriteErrorEvent() { + WriteApi writeApi = influxDBClient.makeWriteApi(); + + CompletableFuture errorEventTriggered = new CompletableFuture<>(); + writeApi.listenEvents(WriteErrorEvent.class, event -> { + Throwable exception = event.getThrowable(); + errorEventTriggered.complete(exception); + }); + CompletableFuture supplyAsync = CompletableFuture.supplyAsync(() -> { + for (int i = 0; i < 100000; i++) { + writeApi.writePoint("my-bucket", "my-org", new Point("foo" + i).time(Instant.now(), WritePrecision.MS).addField("value", i)); + } + return null; + }); + try { + supplyAsync.get(1, TimeUnit.MINUTES); + assertNotNull(errorEventTriggered.get(1, TimeUnit.MINUTES)); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + e.printStackTrace(); + fail(e); + } + } +} From 23c7fff718afd9236a921f7b9cd47af45e655910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bedn=C3=A1=C5=99?= Date: Wed, 19 Jan 2022 08:31:42 +0100 Subject: [PATCH 2/2] chore: rename class to PublishRuntimeErrorAsWriteErrorEventTest.java --- .../internal/PublishRuntimeErrorAsWriteErrorEventTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/test/java/com/influxdb/client/internal/PublishRuntimeErrorAsWriteErrorEventTest.java b/client/src/test/java/com/influxdb/client/internal/PublishRuntimeErrorAsWriteErrorEventTest.java index f48bc1560d5..6c3e95364bc 100644 --- a/client/src/test/java/com/influxdb/client/internal/PublishRuntimeErrorAsWriteErrorEventTest.java +++ b/client/src/test/java/com/influxdb/client/internal/PublishRuntimeErrorAsWriteErrorEventTest.java @@ -44,7 +44,7 @@ * https://github.com/influxdata/influxdb-client-java/issues/291 */ @RunWith(JUnitPlatform.class) -public class PublishRuntimeErrorAsWriteErrorEvent extends AbstractInfluxDBClientTest { +public class PublishRuntimeErrorAsWriteErrorEventTest extends AbstractInfluxDBClientTest { @Test void publishRuntimeErrorAsWriteErrorEvent() {