From 53dde4df98bace1a9be74ed926501906c8775007 Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Mon, 23 Nov 2026 15:24:46 -0800 Subject: [PATCH] M7, ports part 4: the Queue and Checkpoint consumers Java V5's tests of the Queue and Checkpoint consumers that had no Rust counterpart, ported (PLAN.md, M7; tests/PORTING.md follows in its own PR): Checkpoint consumers from the earliest position each reading every message, group members outnumbering the segments delivering each message once, waiting receives answered in the order they were polled, taking half the receiver queue granting as many permits again, a refused lookup, a missing topic not found, a broker without scalable topics refused, and a Checkpoint consumer closed twice. Only test code changes. --- .../tests/checkpoint_group.rs | 11 +- crates/apache-pulsar/src/checkpoint_tests.rs | 82 ++++++++++- crates/apache-pulsar/src/consumer_tests.rs | 135 +++++++++++++++++- 3 files changed, 224 insertions(+), 4 deletions(-) diff --git a/crates/apache-pulsar-testkit/tests/checkpoint_group.rs b/crates/apache-pulsar-testkit/tests/checkpoint_group.rs index 3face0a..90c0656 100644 --- a/crates/apache-pulsar-testkit/tests/checkpoint_group.rs +++ b/crates/apache-pulsar-testkit/tests/checkpoint_group.rs @@ -119,10 +119,15 @@ fn members_of_a_group_share_the_segments() { assert_eq!(h.members[a].consumer.segments().collect::>(), [0, 2]); } -/// With more members than segments, the segment goes to one member, and the others idle: a reader can't share it. +/// With more members than segments, the segment goes whole to one member, and the others idle: a reader can't share +/// it. The segment's four entry buckets would let the controller share it among the members of a Stream group: their +/// registration as Checkpoint consumers keeps it whole. Java's +/// `V5CheckpointConsumerGroupTest.testMembersOutnumberingSegmentsDeliverEachMessageOnce`. #[test] fn members_outnumbering_the_segments_deliver_each_message_once() { - let mut h = GroupHarness::new(world(1), TOPIC, GROUP); + let mut world = world(1); + world.set_buckets(TOPIC, 0, 4); + let mut h = GroupHarness::new(world, TOPIC, GROUP); let members: Vec = ["member-0", "member-1", "member-2"] .iter() .zip(["reader-a", "reader-b", "reader-c"]) @@ -133,6 +138,8 @@ fn members_outnumbering_the_segments_deliver_each_message_once() { let got: Vec> = members.iter().map(|m| h.take_values(*m)).collect(); assert!(members.iter().all(|m| h.members[*m].subscribed == Some(Ok(())))); + // Whole, to the first member by name. + assert_eq!(h.world.assigned(TOPIC, GROUP, "member-0"), [(0, false)]); assert_eq!(got.iter().map(Vec::len).collect::>(), [100, 0, 0]); assert_eq!(got[0].iter().cloned().collect::>(), sent); assert_eq!(h.world.consumer_count(TOPIC, 0, "reader-a"), 1); diff --git a/crates/apache-pulsar/src/checkpoint_tests.rs b/crates/apache-pulsar/src/checkpoint_tests.rs index 711e0a5..df18ac6 100644 --- a/crates/apache-pulsar/src/checkpoint_tests.rs +++ b/crates/apache-pulsar/src/checkpoint_tests.rs @@ -23,7 +23,9 @@ use std::time::Duration; use apache_pulsar_protocol::proto::base_command::Type; -use apache_pulsar_protocol::proto::{MessageMetadata, ServerError}; +use apache_pulsar_protocol::proto::{FeatureFlags, MessageMetadata, ServerError}; +use apache_pulsar_testkit::LookupAnswer; +use apache_pulsar_testkit::scalable::World; use bytes::Bytes; use futures_util::{FutureExt, StreamExt}; @@ -72,6 +74,12 @@ fn resumes_from_a_checkpoint() { let first = consumer.receive_batch(4, Duration::from_secs(60)).await.unwrap(); let checkpoint = consumer.checkpoint().await.unwrap(); consumer.close().await.unwrap(); + // Closing again is fine, and sends nothing more (C14). + consumer.close().await.unwrap(); + assert_eq!(server.received(Type::CLOSE_CONSUMER), 2); + assert_eq!(consumer.receive().await.unwrap_err().kind(), ErrorKind::AlreadyClosed); + assert_eq!(consumer.receive_timeout(Duration::ZERO).await.unwrap_err().kind(), ErrorKind::AlreadyClosed); + assert_eq!(consumer.receive_batch(1, Duration::ZERO).await.unwrap_err().kind(), ErrorKind::AlreadyClosed); assert_eq!(consumer.checkpoint().await.unwrap_err().kind(), ErrorKind::AlreadyClosed); // Restored from its bytes, as an application keeps it. let resumed = reader(&client, Checkpoint::from_bytes(&checkpoint.to_bytes()).unwrap()).await; @@ -104,6 +112,27 @@ fn a_latest_consumer_reads_what_comes_after_it_and_its_checkpoint_says_so() { }); } +/// Checkpoint consumers have no subscription on the brokers: two from the earliest position don't share the messages, +/// each reads every one, on every segment. Java's `V5MultipleConsumersTest.testCheckpointConsumersEachSeeFullStream`. +#[test] +fn consumers_from_the_earliest_position_each_read_every_message() { + run(async { + let server = WorldServer::start(world(2)).await; + let sent: Vec = (0..10).map(|i| format!("m{i}")).collect(); + for (i, value) in sent.iter().enumerate() { + publish(&server, i as u64 % 2, value); + } + let client = local_client(&server).await; + let a = reader(&client, Checkpoint::earliest()).await; + let b = reader(&client, Checkpoint::earliest()).await; + for consumer in [&a, &b] { + let got = consumer.receive_batch(sent.len(), Duration::from_secs(60)).await.unwrap(); + assert_eq!(sorted(values(&got)), sent); + } + client.close().await.unwrap(); + }); +} + /// A split seals the parent: once its reader is at the end the broker gives for it, the reader is let go of, and /// the children's messages come. #[test] @@ -202,6 +231,37 @@ fn a_refused_reader_fails_the_create_and_closes_the_rest() { }); } +/// With auto-creation off, a create on a topic that doesn't exist fails with NotFound. Java's +/// `V5ScalableTopicAutoCreateTest.testNoAutoCreateWhenNamespaceDisallows`. +#[test] +fn a_missing_topic_is_not_found() { + run(async { + let mut world = World::default(); + world.auto_create = false; + let server = WorldServer::start(world).await; + let client = local_client(&server).await; + let error = client.new_checkpoint_consumer(Schema::string()).topic(TOPIC).create().await.unwrap_err(); + assert_eq!(error.kind(), ErrorKind::NotFound, "{error}"); + client.close().await.unwrap(); + }); +} + +/// A broker that doesn't advertise scalable topics is refused: the create fails before it asks the broker for the +/// topic's layout (C15). +#[test] +fn a_broker_without_scalable_topics_is_refused() { + run(async { + let mut world = world(1); + world.features = FeatureFlags { supports_scalable_topics: Some(false), ..Default::default() }; + let server = WorldServer::start(world).await; + let client = local_client(&server).await; + let error = client.new_checkpoint_consumer(Schema::string()).topic(TOPIC).create().await.unwrap_err(); + assert_eq!(error.kind(), ErrorKind::UnsupportedBroker, "{error}"); + assert_eq!((server.received(Type::SCALABLE_TOPIC_LOOKUP), server.received(Type::SUBSCRIBE)), (0, 0)); + client.close().await.unwrap(); + }); +} + /// A reader that loses its connection subscribes again after what the application got. #[test] fn a_lost_connection_resumes_after_what_was_received() { @@ -407,6 +467,26 @@ fn a_refused_registration_fails_the_create() { }); } +/// A lookup of the topic the broker refuses fails the create, alone or in a group, with the broker's category, before +/// the topic's layout is asked for. Java's `V5AuthErrorTest.testLookupNotAuthorized`. +#[test] +fn a_refused_lookup_fails_the_create() { + run(async { + let server = WorldServer::start(world(1)).await; + server.with(|w| { + w.lookup = LookupAnswer::Fail { error: ServerError::AuthorizationError, message: "not you".into() } + }); + let client = local_client(&server).await; + let alone = client.new_checkpoint_consumer(Schema::string()).topic(TOPIC).create(); + assert_eq!(alone.await.unwrap_err().kind(), ErrorKind::Authorization); + let member = client.new_checkpoint_consumer(Schema::string()).topic(TOPIC).consumer_group(GROUP).create(); + assert_eq!(member.await.unwrap_err().kind(), ErrorKind::Authorization); + assert_eq!(server.received(Type::SCALABLE_TOPIC_LOOKUP), 0); + assert_eq!((server.received(Type::SCALABLE_TOPIC_SUBSCRIBE), server.received(Type::SUBSCRIBE)), (0, 0)); + client.close().await.unwrap(); + }); +} + /// A member that loses its connection registers again, and reads on where it stopped. #[test] fn a_member_registers_again_after_a_lost_connection() { diff --git a/crates/apache-pulsar/src/consumer_tests.rs b/crates/apache-pulsar/src/consumer_tests.rs index 52b5f95..5be39ec 100644 --- a/crates/apache-pulsar/src/consumer_tests.rs +++ b/crates/apache-pulsar/src/consumer_tests.rs @@ -24,7 +24,9 @@ use std::time::{Duration, UNIX_EPOCH}; use apache_pulsar_protocol::proto::base_command::Type; -use apache_pulsar_protocol::proto::{KeyValue, MessageMetadata, ServerError}; +use apache_pulsar_protocol::proto::{FeatureFlags, KeyValue, MessageMetadata, ServerError}; +use apache_pulsar_testkit::LookupAnswer; +use apache_pulsar_testkit::scalable::World; use bytes::Bytes; use futures_util::future::Either; use futures_util::{FutureExt, StreamExt}; @@ -213,6 +215,86 @@ fn a_dropped_receive_loses_no_message() { }); } +/// Receives waiting at once are answered in the order they were first polled: a newer one, with a timeout or not, or +/// a batch receive, takes no message before an older one. Java's +/// `V5ReceiveQueueTest.olderPendingReceiveIsServedBeforeANewerOne`, `olderPendingReceiveIsServedBeforeANewerTimedOne` +/// and `receiveMultiDoesNotOvertakeAnOlderPendingReceive`. +#[test] +fn waiting_receives_are_answered_in_the_order_they_were_polled() { + run(async { + let server = WorldServer::start(world(1)).await; + let client = local_client(&server).await; + let consumer = consumer(&client).await; + // Two receives. + let mut older = Box::pin(consumer.receive()); + assert!((&mut older).now_or_never().is_none()); + let mut newer = Box::pin(consumer.receive()); + assert!((&mut newer).now_or_never().is_none()); + publish(&server, 0, "a"); + publish(&server, 0, "b"); + assert_eq!(older.await.unwrap().value().unwrap(), "a"); + assert_eq!(newer.await.unwrap().value().unwrap(), "b"); + // A receive, then one with a timeout. + let mut older = Box::pin(consumer.receive()); + assert!((&mut older).now_or_never().is_none()); + let mut timed = Box::pin(consumer.receive_timeout(Duration::from_secs(60))); + assert!((&mut timed).now_or_never().is_none()); + publish(&server, 0, "c"); + publish(&server, 0, "d"); + assert_eq!(older.await.unwrap().value().unwrap(), "c"); + assert_eq!(timed.await.unwrap().unwrap().value().unwrap(), "d"); + // A receive, then a batch receive. + let mut older = Box::pin(consumer.receive()); + assert!((&mut older).now_or_never().is_none()); + let mut batch = Box::pin(consumer.receive_batch(2, Duration::from_secs(60))); + assert!((&mut batch).now_or_never().is_none()); + for value in ["e", "f", "g"] { + publish(&server, 0, value); + } + assert_eq!(older.await.unwrap().value().unwrap(), "e"); + assert_eq!(values(&batch.await.unwrap()), ["f", "g"]); + client.close().await.unwrap(); + }); +} + +/// The broker pushes what the receiver queue's permits allow, and gets as many again each time the application took +/// half of them, by a batch receive or by receives with a timeout: reading resumes. Java's +/// `V5ReceiveQueueTest.resumeViaReceiveMultiDrain` and `resumeViaTimedReceiveDrain`, whose receive queue is the +/// segments' permits here. +#[test] +fn taking_half_the_receiver_queue_grants_as_many_permits_again() { + run(async { + let server = WorldServer::start(world(1)).await; + let client = local_client(&server).await; + let consumer = client + .new_queue_consumer(Schema::string()) + .topic(TOPIC) + .subscription_name(SUB) + .subscription_initial_position(SubscriptionInitialPosition::Earliest) + .receiver_queue_size(4) + .subscribe() + .await + .unwrap(); + let flows = || -> Vec { + server.world.borrow().received(Type::FLOW).iter().map(|c| c.flow.messagePermits).collect() + }; + for i in 0..8 { + publish(&server, 0, &format!("m{i}")); + } + assert_eq!(values(&consumer.receive_batch(2, Duration::from_secs(60)).await.unwrap()), ["m0", "m1"]); + wait_until("the batch receive's permits go out", || flows() == [4, 2]).await; + for value in ["m2", "m3"] { + let message = consumer.receive_timeout(Duration::from_secs(60)).await.unwrap().unwrap(); + assert_eq!(message.value().unwrap(), value); + } + wait_until("the timed receives' permits go out", || flows() == [4, 2, 2]).await; + // The broker pushed the rest on those permits. + let rest = consumer.receive_batch(4, Duration::from_secs(60)).await.unwrap(); + assert_eq!(values(&rest), ["m4", "m5", "m6", "m7"]); + client.close().await.unwrap(); + }); +} + #[test] fn a_negative_ack_brings_the_message_back() { run(async { @@ -405,6 +487,57 @@ fn a_refused_subscribe_fails_with_the_brokers_error_and_closes_the_rest() { }); } +/// A lookup of the topic the broker refuses fails the subscribe with the broker's category, before the topic's layout +/// is asked for. Java's `V5AuthErrorTest.testLookupNotAuthorized`. +#[test] +fn a_refused_lookup_fails_the_subscribe() { + run(async { + let server = WorldServer::start(world(1)).await; + server.with(|w| { + w.lookup = LookupAnswer::Fail { error: ServerError::AuthorizationError, message: "not you".into() } + }); + let client = local_client(&server).await; + let subscribe = client.new_queue_consumer(Schema::bytes()).topic(TOPIC).subscription_name(SUB).subscribe(); + let error = subscribe.await.unwrap_err(); + assert_eq!(error.kind(), ErrorKind::Authorization, "{error}"); + assert_eq!((server.received(Type::SCALABLE_TOPIC_LOOKUP), server.received(Type::SUBSCRIBE)), (0, 0)); + client.close().await.unwrap(); + }); +} + +/// With auto-creation off, a subscribe to a topic that doesn't exist fails with NotFound. Java's +/// `V5ScalableTopicAutoCreateTest.testNoAutoCreateWhenNamespaceDisallows`. +#[test] +fn a_missing_topic_is_not_found() { + run(async { + let mut world = World::default(); + world.auto_create = false; + let server = WorldServer::start(world).await; + let client = local_client(&server).await; + let subscribe = client.new_queue_consumer(Schema::bytes()).topic(TOPIC).subscription_name(SUB).subscribe(); + let error = subscribe.await.unwrap_err(); + assert_eq!(error.kind(), ErrorKind::NotFound, "{error}"); + client.close().await.unwrap(); + }); +} + +/// A broker that doesn't advertise scalable topics is refused: the subscribe fails before it asks the broker for the +/// topic's layout (C15). +#[test] +fn a_broker_without_scalable_topics_is_refused() { + run(async { + let mut world = world(1); + world.features = FeatureFlags { supports_scalable_topics: Some(false), ..Default::default() }; + let server = WorldServer::start(world).await; + let client = local_client(&server).await; + let subscribe = client.new_queue_consumer(Schema::bytes()).topic(TOPIC).subscription_name(SUB).subscribe(); + let error = subscribe.await.unwrap_err(); + assert_eq!(error.kind(), ErrorKind::UnsupportedBroker, "{error}"); + assert_eq!((server.received(Type::SCALABLE_TOPIC_LOOKUP), server.received(Type::SUBSCRIBE)), (0, 0)); + client.close().await.unwrap(); + }); +} + #[test] fn a_dropped_subscribe_closes_the_consumer() { run(async {