Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions crates/apache-pulsar-protocol/tests/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ fn legacy_layout_routes_like_v4_partitions() {
}
}

/// A legacy segment active beside a managed one: keys still route by hash range. Only a layout whose active
/// segments are all legacy routes them like v4 partitions. Java's
/// `SegmentRouterTest.testMixedLegacyAndRegularStillUsesHashRouting`.
#[test]
fn a_legacy_segment_beside_a_managed_one_routes_by_hash_range() {
let legacy = SegmentInfoProto {
legacy_topic_name: Some("persistent://acme/orders/events-partition-0".into()),
..info(0, 0, 0x7fff, true)
};
let layout = layout(vec![legacy, info(1, 0x8000, 0xffff, true)]);
// A key in each half whose v4 partition is the other segment.
let crossed = |high: bool| {
(0..)
.map(|i: u32| KeyHash::of(&i.to_be_bytes()))
.find(|h| (h.segment_hash() >= 0x8000) == high && h.partition(2) == u32::from(!high))
.unwrap()
};
assert_eq!(layout.route_key(crossed(false)), Ok(0));
assert_eq!(layout.route_key(crossed(true)), Ok(1));
}

/// A layout replaces one of a lower epoch. At the same epoch, only a migrated topic's first layout replaces the
/// synthetic one, as the 5.0.1 broker gives both epoch 0.
#[test]
Expand Down Expand Up @@ -127,3 +148,17 @@ fn keeps_bucket_splits() {
segment.entry_bucket_splits = vec![0x100, 0x1_0000];
assert_eq!(layout(vec![segment]).segment(0).unwrap().bucket_splits, [0x100, 0xffff]);
}

/// The DAG names the controller's `pulsar://` and `pulsar+ssl://` URLs: the layout gives the one for the client's
/// connections. Java's `ClientSegmentLayoutTest.testControllerBrokerUrlPropagated`.
#[test]
fn gives_the_controller_url_for_plain_or_tls_connections() {
let dag = ScalableTopicDAG {
controller_broker_url: Some("pulsar://controller:6650".into()),
controller_broker_url_tls: Some("pulsar+ssl://controller-tls:6651".into()),
..Default::default()
};
let layout = Layout::from_dag(&dag, "topic://acme/orders/events").unwrap();
assert_eq!(layout.controller(false), Some("pulsar://controller:6650"));
assert_eq!(layout.controller(true), Some("pulsar+ssl://controller-tls:6651"));
}
10 changes: 10 additions & 0 deletions crates/apache-pulsar-protocol/tests/message_id_vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ fn rejects_malformed_bytes() {
#[test]
fn ids_hash_and_order_as_they_compare() {
use std::collections::{BTreeSet, HashSet};
use std::hash::{BuildHasher, RandomState};
let entry = |batch_index, batch_size| EntryId { ledger_id: 1, entry_id: 2, partition: -1, batch_index, batch_size };
// The batch size is carried, not compared: the same entry in a set once.
let entries: HashSet<EntryId> = [entry(0, 1), entry(0, 5), entry(1, 5)].into_iter().collect();
Expand All @@ -184,6 +185,15 @@ fn ids_hash_and_order_as_they_compare() {
.into_iter()
.collect();
assert_eq!(ids.len(), 2);
// The position vector is carried, not compared: ids differing only there are equal, and hash alike. Java's
// `MessageIdV5Test.testEqualsIgnoresPositionVector`.
let mut one = MessageId::new(7, entry(-1, 0));
one.positions.insert(0, entry(-1, 0));
let mut other = one.clone();
other.positions.insert(1, EntryId { ledger_id: 5, entry_id: 6, ..entry(-1, 0) });
assert_eq!(one, other);
let state = RandomState::new();
assert_eq!(state.hash_one(&one), state.hash_one(&other));
}

#[test]
Expand Down
46 changes: 46 additions & 0 deletions crates/apache-pulsar-testkit/tests/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ fn values_in(harness: &Harness, segment: u64) -> Vec<String> {
harness.world.stored_in(TOPIC, segment).iter().map(|s| String::from_utf8(s.value.to_vec()).unwrap()).collect()
}

/// How many entries were written to the producers of `segment`, whether the world stored them, holds them or
/// ignored them.
fn sends_to(harness: &Harness, segment: u64) -> usize {
let name = format!("p-seg-{segment}");
let producers: Vec<u64> = harness
.world
.received(Type::PRODUCER)
.iter()
.filter(|c| c.producer.producer_name.as_deref() == Some(name.as_str()))
.map(|c| c.producer.producer_id)
.collect();
harness.world.received(Type::SEND).iter().filter(|c| producers.contains(&c.send.producer_id)).count()
}

#[test]
fn sends_a_stamped_batch_of_one_per_message() {
let mut h = Harness::new(world(1), TOPIC, options());
Expand Down Expand Up @@ -189,6 +203,38 @@ fn only_the_keys_of_a_retiring_segment_are_held() {
assert_eq!(h.segment_of(a1), h.segment_of(a2));
}

/// Java: `aLayoutAppliedWhileReleasingStopsTheRestOfTheBatch`. a1 and a2 are in flight on segment 0 when it is
/// terminated, and a3 is held behind them. Segment 0 splits: the three are written to its child, which splits too
/// while they are in flight there. They reach the grandchild, in order.
#[test]
fn messages_moved_to_a_child_that_splits_reach_the_grandchild_in_order() {
let mut h = Harness::new(world(1), TOPIC, options());
// A key of segment 0, of its lower child, and of that child's lower child.
let a = key_in(0, 0x3fff, 0);
h.send(Some(&a), "a0");
h.world.hold_sends = true;
let a1 = h.send(Some(&a), "a1");
let a2 = h.send(Some(&a), "a2");
h.world.terminate(TOPIC, 0);
// The producer registers again, is refused, and parks a1 and a2.
h.run_until(Duration::from_secs(5), |h| h.registrations[&0] == 2);
let a3 = h.send(Some(&a), "a3");
assert_eq!(h.producer.sends_parked_or_held(), 3, "a1 and a2 parked, a3 held");
let (child, _) = h.world.split(TOPIC, 0);
h.pump();
assert_eq!(sends_to(&h, child), 3, "a1, a2 and a3 are in flight on the child");
let (grandchild, _) = h.world.split(TOPIC, child);
h.pump();
// What was written to the closed producers of segment 0 and of the child is ignored.
h.world.hold_sends = false;
h.world.release_sends();
h.run_until(Duration::from_secs(5), |h| h.producer.outstanding() == 0);
assert_eq!(values_in(&h, 0), ["a0"]);
assert!(values_in(&h, child).is_empty());
assert_eq!(values_in(&h, grandchild), ["a1", "a2", "a3"]);
assert_eq!([a1, a2, a3].map(|s| h.segment_of(s)), [grandchild; 3]);
}

/// Java: `V5ProducerLayoutChangeOrderingTest`, on the simulated broker: many keys, a split mid-stream, every key in
/// call order.
#[test]
Expand Down
13 changes: 13 additions & 0 deletions crates/apache-pulsar-testkit/tests/producer_edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,19 @@ fn refused_credentials_are_retried_once_registered() {
);
}

/// Java: `V5AuthErrorTest.testProduceNotAuthorized`, its exclusive producer. An exclusive producer registers on every
/// segment at create: a segment refusing it for authorization fails the create with the refusal, and the producer
/// closes. A shared producer's messages fail instead (`registration_failures`).
#[test]
fn an_exclusive_create_refused_for_authorization_fails() {
let mut t = Test::new(ProducerAccessMode::Exclusive, one_segment());
let generation = Test::connects(&t.actions())[0].1;
let denied = RegisterError::Server { error: ServerError::AuthorizationError, message: "no".into() };
t.producer.on_register_failed(0, generation, denied, t.now);
let refused = SendError::Server { error: ServerError::AuthorizationError, message: "no".into() };
assert_eq!(t.actions(), [Action::Created(Err(refused)), Action::Closed]);
}

#[test]
fn a_message_is_parked_at_most_ten_times() {
let mut t = Test::new(ProducerAccessMode::Shared, one_segment());
Expand Down
Loading
Loading