From f9a8b52f8bbcfaed089e668b3875788307a5c966 Mon Sep 17 00:00:00 2001 From: Abraham Egnor Date: Thu, 2 Mar 2023 14:50:10 -0500 Subject: [PATCH] RUST-504 Use js timestamps when generating objectids (#406) --- .evergreen/config.yml | 23 +++++++++++++++++++++++ .evergreen/run-wasm-tests.sh | 10 ++++++++++ Cargo.toml | 3 +++ src/oid.rs | 8 ++++++-- wasm-test/Cargo.toml | 17 +++++++++++++++++ wasm-test/src/lib.rs | 2 ++ wasm-test/src/test.rs | 6 ++++++ 7 files changed, 67 insertions(+), 2 deletions(-) create mode 100755 .evergreen/run-wasm-tests.sh create mode 100644 wasm-test/Cargo.toml create mode 100644 wasm-test/src/lib.rs create mode 100644 wasm-test/src/test.rs diff --git a/.evergreen/config.yml b/.evergreen/config.yml index ff09d7ff..7c9ba3af 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -133,6 +133,16 @@ functions: ${PREPARE_SHELL} .evergreen/check-rustdoc.sh + "run wasm tests": + - command: shell.exec + type: test + params: + shell: bash + working_dir: "src" + script: | + ${PREPARE_SHELL} + .evergreen/run-wasm-tests.sh + "init test-results": - command: shell.exec params: @@ -177,6 +187,10 @@ tasks: commands: - func: "run fuzzer" + - name: "wasm-test" + commands: + - func: "run wasm tests" + axes: - id: "extra-rust-versions" values: @@ -207,6 +221,7 @@ buildvariants: - ubuntu1804-test tasks: - name: "compile-only" + - name: "lint" display_name: "Lint" @@ -224,3 +239,11 @@ buildvariants: - ubuntu1804-test tasks: - name: "run-fuzzer" + +- + name: "wasm" + display_name: "WASM" + run_on: + - ubuntu1804-test + tasks: + - name: "wasm-test" \ No newline at end of file diff --git a/.evergreen/run-wasm-tests.sh b/.evergreen/run-wasm-tests.sh new file mode 100755 index 00000000..806c675e --- /dev/null +++ b/.evergreen/run-wasm-tests.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +set -o errexit + +. ~/.cargo/env + +curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh + +cd $(dirname $0)/../wasm-test +wasm-pack test --node \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 8329dcaf..dde634fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,6 +69,9 @@ serde_with = { version = "1", optional = true } time = { version = "0.3.9", features = ["formatting", "parsing", "macros", "large-dates"] } bitvec = "1.0.1" +[target.'cfg(target_arch = "wasm32")'.dependencies] +js-sys = "0.3" + [dev-dependencies] assert_matches = "1.2" criterion = "0.3.0" diff --git a/src/oid.rs b/src/oid.rs index 9161cd8f..263fc805 100644 --- a/src/oid.rs +++ b/src/oid.rs @@ -2,15 +2,16 @@ //! For more information, see the documentation for the [`ObjectId`] type. use std::{ - convert::TryInto, error, fmt, result, str::FromStr, sync::atomic::{AtomicUsize, Ordering}, - time::SystemTime, }; +#[cfg(not(target_arch = "wasm32"))] +use std::{convert::TryInto, time::SystemTime}; + use hex::{self, FromHexError}; use rand::{thread_rng, Rng}; @@ -240,6 +241,9 @@ impl ObjectId { /// Generates a new timestamp representing the current seconds since epoch. /// Represented in Big Endian. fn gen_timestamp() -> [u8; 4] { + #[cfg(target_arch = "wasm32")] + let timestamp: u32 = (js_sys::Date::now() / 1000.0) as u32; + #[cfg(not(target_arch = "wasm32"))] let timestamp: u32 = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .expect("system clock is before 1970") diff --git a/wasm-test/Cargo.toml b/wasm-test/Cargo.toml new file mode 100644 index 00000000..fb583123 --- /dev/null +++ b/wasm-test/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "bson-wasm-test" +version = "0.1.0" +authors = ["Abraham Egnor "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +bson = { path = ".." } +getrandom = { version = "0.2", features = ["js"] } + +[dev-dependencies] +wasm-bindgen-test = "0.3.0" \ No newline at end of file diff --git a/wasm-test/src/lib.rs b/wasm-test/src/lib.rs new file mode 100644 index 00000000..534ffa4b --- /dev/null +++ b/wasm-test/src/lib.rs @@ -0,0 +1,2 @@ +#[cfg(test)] +mod test; \ No newline at end of file diff --git a/wasm-test/src/test.rs b/wasm-test/src/test.rs new file mode 100644 index 00000000..8703effa --- /dev/null +++ b/wasm-test/src/test.rs @@ -0,0 +1,6 @@ +use wasm_bindgen_test::wasm_bindgen_test; + +#[wasm_bindgen_test] +fn objectid_new() { + let _ = bson::oid::ObjectId::new(); +} \ No newline at end of file