@@ -4,17 +4,20 @@ use rdkafka::producer::{FutureProducer, FutureRecord};
44use std:: sync:: { Arc , Mutex } ;
55use std:: time:: Duration ;
66use testcontainers:: runners:: AsyncRunner ;
7+ use testcontainers_modules:: testcontainers:: ContainerAsync ;
78use testcontainers_modules:: { kafka, kafka:: Kafka } ;
89use tips_audit:: BundleEvent ;
9- use tips_bundle_pool:: { BundleStore , InMemoryBundlePool , KafkaBundleSource } ;
10+ use tips_bundle_pool:: {
11+ BundleStore , InMemoryBundlePool , KafkaBundleSource , connect_sources_to_pool,
12+ } ;
1013use tips_core:: {
1114 BundleWithMetadata ,
1215 test_utils:: { create_test_bundle, create_transaction} ,
1316} ;
1417use tokio:: sync:: mpsc;
1518
16- # [ tokio :: test ]
17- async fn test_kafka_bundle_source_to_pool_integration ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
19+ async fn setup_kafka ( )
20+ -> Result < ( ContainerAsync < Kafka > , FutureProducer , ClientConfig ) , Box < dyn std:: error:: Error > > {
1821 let kafka_container = Kafka :: default ( ) . start ( ) . await ?;
1922 let bootstrap_servers = format ! (
2023 "127.0.0.1:{}" ,
@@ -23,15 +26,11 @@ async fn test_kafka_bundle_source_to_pool_integration() -> Result<(), Box<dyn st
2326 . await ?
2427 ) ;
2528
26- let topic = "test-bundles" ;
27-
2829 let kafka_producer = ClientConfig :: new ( )
2930 . set ( "bootstrap.servers" , & bootstrap_servers)
3031 . set ( "message.timeout.ms" , "5000" )
3132 . create :: < FutureProducer > ( ) ?;
3233
33-
34- let ( bundle_tx, mut bundle_rx) = mpsc:: unbounded_channel :: < BundleWithMetadata > ( ) ;
3534 let mut kafka_consumer_config = ClientConfig :: new ( ) ;
3635 kafka_consumer_config
3736 . set ( "group.id" , "bundle-pool-test-source" )
@@ -40,23 +39,25 @@ async fn test_kafka_bundle_source_to_pool_integration() -> Result<(), Box<dyn st
4039 . set ( "enable.auto.commit" , "false" )
4140 . set ( "auto.offset.reset" , "earliest" ) ;
4241
42+ Ok ( ( kafka_container, kafka_producer, kafka_consumer_config) )
43+ }
44+
45+ #[ tokio:: test]
46+ async fn test_kafka_bundle_source_to_pool_integration ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
47+ let topic = "test-bundles" ;
48+ let ( _kafka_container, kafka_producer, kafka_consumer_config) = setup_kafka ( ) . await ?;
49+
50+ let ( bundle_tx, bundle_rx) = mpsc:: unbounded_channel :: < BundleWithMetadata > ( ) ;
51+
4352 let kafka_source = KafkaBundleSource :: new ( kafka_consumer_config, topic. to_string ( ) , bundle_tx) ?;
44- tokio:: spawn ( async move {
45- kafka_source. run ( ) . await . expect ( "Kafka source failed" ) ;
46- } ) ;
4753
4854 let ( audit_tx, _audit_rx) = mpsc:: unbounded_channel :: < BundleEvent > ( ) ;
4955 let pool = Arc :: new ( Mutex :: new ( InMemoryBundlePool :: new (
5056 audit_tx,
5157 "test-builder" . to_string ( ) ,
5258 ) ) ) ;
5359
54- let pool_clone = pool. clone ( ) ;
55- tokio:: spawn ( async move {
56- while let Some ( bundle) = bundle_rx. recv ( ) . await {
57- pool_clone. lock ( ) . unwrap ( ) . add_bundle ( bundle) ;
58- }
59- } ) ;
60+ connect_sources_to_pool ( vec ! [ kafka_source] , bundle_rx, pool. clone ( ) ) ;
6061
6162 let alice = PrivateKeySigner :: random ( ) ;
6263 let bob = PrivateKeySigner :: random ( ) ;
@@ -79,11 +80,9 @@ async fn test_kafka_bundle_source_to_pool_integration() -> Result<(), Box<dyn st
7980 let mut counter = 0 ;
8081 loop {
8182 counter += 1 ;
82- if counter > 10 {
83- panic ! ( "Bundle was not added to pool within timeout" ) ;
84- }
83+ assert ! ( counter < 10 ) ;
8584
86- tokio:: time:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
85+ tokio:: time:: sleep ( Duration :: from_millis ( 500 ) ) . await ;
8786
8887 let bundles = pool. lock ( ) . unwrap ( ) . get_bundles ( ) ;
8988 if bundles. is_empty ( ) {
0 commit comments