davsclaus commented on code in PR #27797: URL: https://github.com/apache/camel/pull/27797#discussion_r4266930318
########## components/camel-influxdb2/src/test/java/org/apache/camel/component/influxdb2/InfluxDb2ProducerWriteFailureTest.java: ########## @@ -0,0 +1,214 @@ +/* + * 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.camel.component.influxdb2; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.atomic.AtomicInteger; + +import com.influxdb.client.InfluxDBClient; +import com.influxdb.client.InfluxDBClientFactory; +import com.influxdb.client.write.Point; +import com.influxdb.exceptions.InfluxException; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.influxdb2.data.Record; +import org.apache.camel.component.influxdb2.data.Records; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * The producer writes with the real InfluxDB client against a local HTTP server that plays the write API of InfluxDB + * 2.x: a write that the server rejects, or that never reaches it, must fail the exchange, and an accepted write must be + * at the server when the exchange completes. + */ +class InfluxDb2ProducerWriteFailureTest extends CamelTestSupport { + + private static final String ORG_ID = "00000000000000a1"; + + private final AtomicInteger writeStatus = new AtomicInteger(204); + private final List<String> writes = new CopyOnWriteArrayList<>(); + private final List<String> writeQueries = new CopyOnWriteArrayList<>(); + + private HttpServer server; + private InfluxDBClient client; + + @Override + protected CamelContext createCamelContext() throws Exception { + server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); + server.createContext("/api/v2/orgs", + exchange -> respond(exchange, 200, "{\"orgs\":[{\"id\":\"" + ORG_ID + "\",\"name\":\"myOrg\"}]}")); + server.createContext("/api/v2/buckets", + exchange -> respond(exchange, 200, + "{\"buckets\":[{\"id\":\"00000000000000b1\",\"orgID\":\"" + ORG_ID + + "\",\"name\":\"myBucket\",\"retentionRules\":[]}]}")); + server.createContext("/api/v2/write", exchange -> { + int status = writeStatus.get(); + String lines = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8); + if (status == 204) { + writeQueries.add(String.valueOf(exchange.getRequestURI().getQuery())); + writes.add(lines); + respond(exchange, 204, null); + } else { + respond(exchange, status, "{\"code\":\"rejected\",\"message\":\"write rejected with " + status + "\"}"); + } + }); + server.start(); + + client = InfluxDBClientFactory.create("http://127.0.0.1:" + server.getAddress().getPort(), "token".toCharArray()); + + CamelContext context = super.createCamelContext(); + context.getRegistry().bind("influxDbBean", client); + return context; + } + + @Override + protected void cleanupResources() { + // the server goes first, so that a client that still has something to send cannot wait for an answer Review Comment: Nit: with the blocking API the client no longer has anything buffered to send at teardown, so the reason in this comment no longer applies. Either order works now. Maybe drop or reword the comment so it does not suggest a background flush that is gone. -- 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]
