-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
114 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
bridge/svix-bridge-plugin-kafka/tests/it/kafka_producer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::time::Duration; | ||
|
||
use rdkafka::{ | ||
consumer::{Consumer, StreamConsumer}, | ||
ClientConfig, Message, | ||
}; | ||
use serde_json::json; | ||
use svix_bridge_plugin_kafka::{KafkaOutputOpts, KafkaProducer}; | ||
use svix_bridge_types::{ForwardRequest, ReceiverOutput as _}; | ||
|
||
use crate::{create_topic, delete_topic, kafka_admin_client, BROKER_HOST}; | ||
|
||
/// Time to wait for the consumer to be properly subscriber. | ||
const SUBSCRIBER_WAIT_TIME: Duration = Duration::from_secs(5); | ||
|
||
#[tokio::test] | ||
async fn test_produce_ok() { | ||
let topic = unique_topic_name!(); | ||
let admin_client = kafka_admin_client(); | ||
create_topic(&admin_client, topic).await; | ||
|
||
let producer = KafkaProducer::new( | ||
"test".into(), | ||
KafkaOutputOpts { | ||
bootstrap_brokers: BROKER_HOST.to_owned(), | ||
topic: topic.to_owned(), | ||
security_protocol: svix_bridge_plugin_kafka::KafkaSecurityProtocol::Plaintext, | ||
debug_contexts: None, | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
let consumer: StreamConsumer = ClientConfig::new() | ||
.set("bootstrap.servers", BROKER_HOST) | ||
.set("group.id", "svix_bridge_test_group_id") | ||
.create() | ||
.unwrap(); | ||
|
||
consumer.subscribe(&[topic]).unwrap(); | ||
tokio::time::sleep(SUBSCRIBER_WAIT_TIME).await; | ||
dbg!(); | ||
|
||
let payload = json!({ "test": "payload" }); | ||
let payload_s = payload.to_string(); | ||
producer.handle(ForwardRequest { payload }).await.unwrap(); | ||
dbg!(); | ||
|
||
let msg = consumer.recv().await.unwrap(); | ||
dbg!(); | ||
assert_eq!(msg.payload(), Some(payload_s.as_bytes())); | ||
|
||
tokio::time::timeout(Duration::from_secs(1), consumer.recv()) | ||
.await | ||
.expect_err("there must be no further messages"); | ||
|
||
delete_topic(&admin_client, topic).await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,54 @@ | ||
use rdkafka::{ | ||
admin::{AdminClient, NewTopic, TopicReplication}, | ||
client::DefaultClientContext, | ||
types::RDKafkaErrorCode, | ||
ClientConfig, | ||
}; | ||
|
||
/// These tests assume a "vanilla" kafka instance, using the default port, creds, exchange... | ||
const BROKER_HOST: &str = "localhost:9094"; | ||
|
||
fn kafka_admin_client() -> AdminClient<DefaultClientContext> { | ||
// create does block I/O, but we don't care in tests | ||
ClientConfig::new() | ||
.set("bootstrap.servers", BROKER_HOST) | ||
.create() | ||
.unwrap() | ||
} | ||
|
||
async fn create_topic(admin_client: &AdminClient<DefaultClientContext>, topic: &str) { | ||
let new_topic = NewTopic::new(topic, 1, TopicReplication::Fixed(1)); | ||
if let Err(e) = admin_client | ||
.create_topics(&[new_topic], &Default::default()) | ||
.await | ||
{ | ||
if e.rdkafka_error_code() != Some(RDKafkaErrorCode::TopicAlreadyExists) { | ||
panic!("{e}"); | ||
} | ||
} | ||
} | ||
|
||
async fn delete_topic(admin_client: &AdminClient<DefaultClientContext>, topic: &str) { | ||
admin_client | ||
.delete_topics(&[topic], &Default::default()) | ||
.await | ||
.unwrap(); | ||
} | ||
|
||
macro_rules! unique_topic_name { | ||
() => { | ||
&format!( | ||
"test_{}_{}", | ||
file!() | ||
.split('/') | ||
.next_back() | ||
.unwrap() | ||
.strip_suffix(".rs") | ||
.unwrap(), | ||
line!() | ||
) | ||
}; | ||
} | ||
|
||
mod kafka_consumer; | ||
mod kafka_producer; |