Skip to main content

rocketmq_protocol/protocol/header/
update_consumer_offset_conditional_header.rs

1// Copyright 2026 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use cheetah_string::CheetahString;
16use rocketmq_macros::RequestHeaderCodec;
17use serde::Deserialize;
18use serde::Serialize;
19
20/// One exact queue reset guarded by the offset observed during preflight.
21#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize, RequestHeaderCodec)]
22#[header(
23    type_id = "rocketmq_protocol::protocol::header::update_consumer_offset_conditional_header::UpdateConsumerOffsetConditionalHeader"
24)]
25#[serde(rename_all = "camelCase")]
26pub struct UpdateConsumerOffsetConditionalHeader {
27    #[header(required)]
28    pub consumer_group: CheetahString,
29    #[header(required)]
30    pub topic: CheetahString,
31    #[header(required)]
32    pub queue_id: i32,
33    #[header(required)]
34    pub expected_offset: i64,
35    #[header(required)]
36    pub new_offset: i64,
37}
38
39#[cfg(test)]
40mod tests {
41    use cheetah_string::CheetahString;
42
43    use super::UpdateConsumerOffsetConditionalHeader;
44    use crate::protocol::command_custom_header::CommandCustomHeader;
45    use crate::protocol::command_custom_header::FromMap;
46
47    #[test]
48    fn exact_offset_condition_round_trips() {
49        let header = UpdateConsumerOffsetConditionalHeader {
50            consumer_group: "group-a".into(),
51            topic: "topic-a".into(),
52            queue_id: 3,
53            expected_offset: 41,
54            new_offset: 12,
55        };
56        let map = header.to_map().expect("header should encode");
57        assert_eq!(map.get("expectedOffset").map(CheetahString::as_str), Some("41"));
58        assert_eq!(map.get("newOffset").map(CheetahString::as_str), Some("12"));
59        assert_eq!(
60            <UpdateConsumerOffsetConditionalHeader as FromMap>::from(&map).expect("header should decode"),
61            header
62        );
63    }
64}