From a10ade8cf5672fb6fc904f2bef77d7d382797cad Mon Sep 17 00:00:00 2001 From: Namekuji Date: Fri, 2 Jun 2023 12:19:36 -0400 Subject: [PATCH] fix unit test --- packages/backend/native-utils/Cargo.toml | 2 +- packages/backend/native-utils/src/util/id.rs | 43 ++++++++++++++----- packages/backend/native-utils/tests/common.rs | 2 +- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/packages/backend/native-utils/Cargo.toml b/packages/backend/native-utils/Cargo.toml index ba7a48b8d3..071d0f331b 100644 --- a/packages/backend/native-utils/Cargo.toml +++ b/packages/backend/native-utils/Cargo.toml @@ -7,7 +7,7 @@ version = "0.0.0" members = ["migration/Cargo.toml"] [features] -default = ["napi"] +default = [] noarray = [] napi = ["dep:napi", "dep:napi-derive", "dep:radix_fmt"] diff --git a/packages/backend/native-utils/src/util/id.rs b/packages/backend/native-utils/src/util/id.rs index 1d28b80c05..539b0415e8 100644 --- a/packages/backend/native-utils/src/util/id.rs +++ b/packages/backend/native-utils/src/util/id.rs @@ -41,11 +41,13 @@ cfg_if! { use napi_derive::napi; const TIME_2000: u64 = 946_684_800_000; + const TIMESTAMP_LENGTH: u16 = 8; /// Calls [init_id] inside. Must be called before [native_create_id]. #[napi] pub fn native_init_id_generator(length: u16, fingerprint: String) { - init_id(length, fingerprint); + // length to pass init_id shoule be greater than or equal to 8. + init_id(cmp::max(length - TIMESTAMP_LENGTH, 8), fingerprint); } /// Generates @@ -59,19 +61,38 @@ cfg_if! { #[cfg(test)] mod unit_test { + use cfg_if::cfg_if; use pretty_assertions::{assert_eq, assert_ne}; use std::thread; - use crate::util::id; - #[test] - fn can_generate_unique_ids() { - assert_eq!(id::create_id(), Err(id::ErrorUninitialized)); - id::init_id(12, ""); - assert_eq!(id::create_id().unwrap().len(), 12); - assert_ne!(id::create_id().unwrap(), id::create_id().unwrap()); - let id1 = thread::spawn(|| id::create_id().unwrap()); - let id2 = thread::spawn(|| id::create_id().unwrap()); - assert_ne!(id1.join().unwrap(), id2.join().unwrap()) + cfg_if! { + if #[cfg(feature = "napi")] { + use chrono::Utc; + + #[test] + fn can_generate_aid_compat_ids() { + id::native_init_id_generator(16, "".to_string()); + let id1 = id::native_create_id(Utc::now().timestamp_millis().into()); + assert_eq!(id1.len(), 16); + let id1 = id::native_create_id(Utc::now().timestamp_millis().into()); + let id2 = id::native_create_id(Utc::now().timestamp_millis().into()); + assert_ne!(id1, id2); + let id1 = thread::spawn(|| id::native_create_id(Utc::now().timestamp_millis().into())); + let id2 = thread::spawn(|| id::native_create_id(Utc::now().timestamp_millis().into())); + assert_ne!(id1.join().unwrap(), id2.join().unwrap()); + } + } else { + #[test] + fn can_generate_unique_ids() { + assert_eq!(id::create_id(), Err(id::ErrorUninitialized)); + id::init_id(12, ""); + assert_eq!(id::create_id().unwrap().len(), 12); + assert_ne!(id::create_id().unwrap(), id::create_id().unwrap()); + let id1 = thread::spawn(|| id::create_id().unwrap()); + let id2 = thread::spawn(|| id::create_id().unwrap()); + assert_ne!(id1.join().unwrap(), id2.join().unwrap()) + } + } } } diff --git a/packages/backend/native-utils/tests/common.rs b/packages/backend/native-utils/tests/common.rs index 775b372126..61dd20aac7 100644 --- a/packages/backend/native-utils/tests/common.rs +++ b/packages/backend/native-utils/tests/common.rs @@ -139,7 +139,7 @@ async fn cleanup() { } async fn setup_model(db: &DbConn) { - init_id(12); + init_id(12, ""); db.transaction::<_, (), DbErr>(|txn| { Box::pin(async move {