Skip to content

Commit 4058779

Browse files
committed
fix: publishing runtime error as a WriteErrorEvent (#291)
1 parent c592324 commit 4058779

3 files changed

Lines changed: 75 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
1. [#285](https://github.com/influxdata/influxdb-client-java/pull/285): Default dialect for Query APIs
99
1. [#294](https://github.com/influxdata/influxdb-client-java/pull/294): Mapping measurement with primitive `float`
1010
1. [#297](https://github.com/influxdata/influxdb-client-java/pull/297): Transient dependency of `okhttp`, `retrofit` and `rxjava`
11+
1. [#292](https://github.com/influxdata/influxdb-client-java/pull/292): Publishing runtime error as a WriteErrorEvent
1112

1213
## 4.0.0 [2021-11-26]
1314

client/src/main/java/com/influxdb/client/internal/AbstractWriteClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public AbstractWriteClient(@Nonnull final WriteOptions writeOptions,
181181
if (responseNotification.isOnError()) {
182182
publish(new WriteErrorEvent(toInfluxException(responseNotification.getError())));
183183
}
184-
}, throwable -> new WriteErrorEvent(toInfluxException(throwable)));
184+
}, throwable -> publish(new WriteErrorEvent(toInfluxException(throwable))));
185185

186186
autoCloseables.add(this);
187187
}
@@ -594,4 +594,4 @@ static void waitToCondition(final Supplier<Boolean> condition, final int millis)
594594
}
595595
}
596596
}
597-
}
597+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
package com.influxdb.client.internal;
23+
24+
import static org.junit.jupiter.api.Assertions.assertNotNull;
25+
import static org.junit.jupiter.api.Assertions.fail;
26+
27+
import java.time.Instant;
28+
import java.util.concurrent.CompletableFuture;
29+
import java.util.concurrent.ExecutionException;
30+
import java.util.concurrent.TimeUnit;
31+
import java.util.concurrent.TimeoutException;
32+
33+
import com.influxdb.client.WriteApi;
34+
import com.influxdb.client.domain.WritePrecision;
35+
import com.influxdb.client.write.Point;
36+
import com.influxdb.client.write.events.WriteErrorEvent;
37+
38+
import org.junit.jupiter.api.Test;
39+
import org.junit.platform.runner.JUnitPlatform;
40+
import org.junit.runner.RunWith;
41+
42+
/**
43+
* This test ensures publish is called with WriteErrorEvent as described in
44+
* https://github.com/influxdata/influxdb-client-java/issues/291
45+
*/
46+
@RunWith(JUnitPlatform.class)
47+
public class PublishRuntimeErrorAsWriteErrorEvent extends AbstractInfluxDBClientTest {
48+
49+
@Test
50+
void publishRuntimeErrorAsWriteErrorEvent() {
51+
WriteApi writeApi = influxDBClient.makeWriteApi();
52+
53+
CompletableFuture<Throwable> errorEventTriggered = new CompletableFuture<>();
54+
writeApi.listenEvents(WriteErrorEvent.class, event -> {
55+
Throwable exception = event.getThrowable();
56+
errorEventTriggered.complete(exception);
57+
});
58+
CompletableFuture<Object> supplyAsync = CompletableFuture.supplyAsync(() -> {
59+
for (int i = 0; i < 100000; i++) {
60+
writeApi.writePoint("my-bucket", "my-org", new Point("foo" + i).time(Instant.now(), WritePrecision.MS).addField("value", i));
61+
}
62+
return null;
63+
});
64+
try {
65+
supplyAsync.get(1, TimeUnit.MINUTES);
66+
assertNotNull(errorEventTriggered.get(1, TimeUnit.MINUTES));
67+
} catch (InterruptedException | ExecutionException | TimeoutException e) {
68+
e.printStackTrace();
69+
fail(e);
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)