use of io.smallrye.reactive.messaging.kafka.impl.KafkaSink in project smallrye-reactive-messaging by smallrye.
the class KafkaSinkWithCloudEventsTest method testSendingBinaryCloudEventsWithConfiguredTypeAndSourceButNoMetadata.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingBinaryCloudEventsWithConfiguredTypeAndSourceButNoMetadata() {
KafkaMapBasedConfig config = newCommonConfig();
config.put("topic", topic);
config.put("value.serializer", StringSerializer.class.getName());
config.put("channel-name", topic);
config.put("key", "my-key");
config.put("cloud-events-type", "my type");
config.put("cloud-events-source", "http://acme.org");
KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
sink = new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance());
ConsumerTask<String, String> records = companion.consumeStrings().fromTopics(topic);
Message<?> message = Message.of("hello!");
Multi.createFrom().<Message<?>>item(message).subscribe().withSubscriber((Subscriber) sink.getSink().build());
await().until(() -> records.getRecords().size() == 1);
ConsumerRecord<String, String> record = records.getRecords().get(0);
assertThat(record.topic()).isEqualTo(topic);
assertThat(record.key()).isEqualTo("my-key");
assertThat(record.headers()).contains(new RecordHeader("ce_specversion", "1.0".getBytes()), new RecordHeader("ce_type", "my type".getBytes()), new RecordHeader("ce_source", "http://acme.org".getBytes()));
assertThat(record.headers().lastHeader("ce_id")).isNotNull();
assertThat(record.value()).isEqualTo("hello!");
}
use of io.smallrye.reactive.messaging.kafka.impl.KafkaSink in project smallrye-reactive-messaging by smallrye.
the class KafkaSinkWithCloudEventsTest method testSendingBinaryCloudEventsMissingMandatoryAttribute.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingBinaryCloudEventsMissingMandatoryAttribute() {
KafkaMapBasedConfig config = newCommonConfig();
config.put("topic", topic);
config.put("value.serializer", StringSerializer.class.getName());
config.put("channel-name", topic);
KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
sink = new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance());
Message<?> message = Message.of("hello").addMetadata(OutgoingCloudEventMetadata.builder().withSource(URI.create("test://test")).withId("some id").build());
await().until(() -> {
HealthReport.HealthReportBuilder builder = HealthReport.builder();
sink.isAlive(builder);
return builder.build().isOk();
});
Multi.createFrom().<Message<?>>item(message).subscribe().withSubscriber((Subscriber) sink.getSink().build());
await().until(() -> {
HealthReport.HealthReportBuilder builder = HealthReport.builder();
sink.isAlive(builder);
return !builder.build().isOk();
});
}
use of io.smallrye.reactive.messaging.kafka.impl.KafkaSink in project smallrye-reactive-messaging by smallrye.
the class KafkaSinkWithCloudEventsTest method testSendingStructuredCloudEventsWithTimestampAndSubject.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingStructuredCloudEventsWithTimestampAndSubject() {
KafkaMapBasedConfig config = newCommonConfig();
config.put("topic", topic);
config.put("value.serializer", StringSerializer.class.getName());
config.put("channel-name", topic);
config.put("cloud-events-mode", "structured");
KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
sink = new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance());
ConsumerTask<String, String> records = companion.consumeStrings().fromTopics(topic);
ZonedDateTime time = ZonedDateTime.now();
Message<?> message = Message.of("").addMetadata(OutgoingCloudEventMetadata.builder().withSource(URI.create("test://test")).withType("type").withId("some id").withSubject("subject").withTimestamp(time).build());
Multi.createFrom().<Message<?>>item(message).subscribe().withSubscriber((Subscriber) sink.getSink().build());
await().until(() -> records.count() == 1);
ConsumerRecord<String, String> record = records.getRecords().get(0);
assertThat(record.topic()).isEqualTo(topic);
assertThat(record.key()).isNull();
assertThat(record.headers()).contains(new RecordHeader("content-type", "application/cloudevents+json; charset=UTF-8".getBytes()));
JsonObject json = new JsonObject(record.value());
assertThat(json.getString("specversion")).isEqualTo("1.0");
assertThat(json.getString("type")).isEqualTo("type");
assertThat(json.getString("source")).isEqualTo("test://test");
assertThat(json.getString("id")).isEqualTo("some id");
assertThat(json.getString("subject")).isEqualTo("subject");
assertThat(json.getInstant("time")).isNotNull();
assertThat(json.getInstant("time").getEpochSecond()).isEqualTo(time.toEpochSecond());
}
use of io.smallrye.reactive.messaging.kafka.impl.KafkaSink in project smallrye-reactive-messaging by smallrye.
the class KafkaSinkWithCloudEventsTest method testSendingStructuredCloudEventsWithWrongSerializer.
@Test
public void testSendingStructuredCloudEventsWithWrongSerializer() {
KafkaMapBasedConfig config = newCommonConfig();
config.put("topic", topic);
config.put("value.serializer", DoubleSerializer.class.getName());
config.put("channel-name", topic);
config.put("cloud-events-mode", "structured");
KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
assertThatThrownBy(() -> new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance())).isInstanceOf(IllegalStateException.class);
}
use of io.smallrye.reactive.messaging.kafka.impl.KafkaSink in project smallrye-reactive-messaging by smallrye.
the class KafkaSinkTest method testInvalidTypeWithDefaultInflightMessages.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testInvalidTypeWithDefaultInflightMessages() {
ConsumerTask<String, Integer> consumed = companion.consumeIntegers().fromTopics(topic, 10, Duration.ofSeconds(10));
MapBasedConfig config = getBaseConfig().with("topic", topic).with("value.serializer", IntegerSerializer.class.getName()).with("partition", 0).with("retries", 0L).with("channel-name", "testInvalidTypeWithDefaultInflightMessages");
KafkaConnectorOutgoingConfiguration oc = new KafkaConnectorOutgoingConfiguration(config);
sink = new KafkaSink(oc, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance());
Subscriber subscriber = sink.getSink().build();
Multi.createFrom().range(0, 5).map(i -> {
if (i == 3 || i == 5) {
return Integer.toString(i);
}
return i;
}).map(Message::of).subscribe(subscriber);
await().until(() -> consumed.count() >= 3);
// Default inflight is 5
// 1, 2, 3, 4, 5 are sent at the same time.
// As 3 fails, the stream is stopped, but, 1, 2, and 4 are already sent and potentially 6
assertThat(consumed.count()).isGreaterThanOrEqualTo(3);
}
Aggregations