Skip to content

Commit

Permalink
fix(rust): change sequence number type of message id from i16 to i32 (#…
Browse files Browse the repository at this point in the history
…566)

* fix(rust): change sequence number type of message id from i16 to i32

Signed-off-by: SSpirits <[email protected]>

* fix(rust): optimize unit test

Signed-off-by: SSpirits <[email protected]>

* fix(rust): ignore message id test

Signed-off-by: SSpirits <[email protected]>

---------

Signed-off-by: SSpirits <[email protected]>
  • Loading branch information
ShadowySpirits authored Jul 11, 2023
1 parent 29d332b commit f4f198b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ keywords = ["rocketmq", "api", "client", "sdk", "grpc"]
[dependencies]
tokio = { version = "1", features = ["full"] }
tokio-rustls = {version = "0.24.0", features = ["default", "dangerous_configuration"] }
tokio-stream="0.1.12"
async-trait = "0.1.68"
lazy_static = "1.4"
tonic = {version = "0.9.0", features = ["tls", "default", "channel", "tls-roots"]}
Expand All @@ -60,7 +61,6 @@ mac_address = "1.1.4"
hex = "0.4.3"
time = "0.3"
once_cell = "1.9.0"
tokio-stream="0.1.12"

mockall = "0.11.4"
mockall_double= "0.3.0"
Expand Down
14 changes: 8 additions & 6 deletions rust/src/model/message_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ pub(crate) static UNIQ_ID_GENERATOR: Lazy<Mutex<UniqueIdGenerator>> = Lazy::new(
Mutex::new(generator)
});

pub struct UniqueIdGenerator {
counter: i16,
pub(crate) struct UniqueIdGenerator {
counter: i32,
prefix: String,
start_timestamp: i64,
next_timestamp: i64,
Expand Down Expand Up @@ -122,19 +122,21 @@ impl UniqueIdGenerator {
((OffsetDateTime::now_utc().unix_timestamp() - self.start_timestamp) * 1000) as i32,
)
.unwrap();
buf.write_i16::<BigEndian>(self.counter).unwrap();
buf.write_i32::<BigEndian>(self.counter).unwrap();
self.prefix.clone() + &hex::encode(buf)
}
}

#[cfg(test)]
mod test {
#[ignore]
#[test]
fn text_generate_uniq_id() {
fn generate_uniq_id() {
use super::UNIQ_ID_GENERATOR;
for i in 0..10 {
for i in 1..17 {
let uid = UNIQ_ID_GENERATOR.lock().next_id();
println!("i: {}, uid: {}", i, uid);
assert_eq!(uid.len(), 34);
assert_eq!(uid.get(26..).unwrap(), hex::encode(vec![0, 0, 0, i as u8]));
}
}
}

0 comments on commit f4f198b

Please sign in to comment.