Skip to content

Commit

Permalink
RUST-504 Use js timestamps when generating objectids (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
abr-egn authored Mar 2, 2023
1 parent fda2cf3 commit f9a8b52
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 2 deletions.
23 changes: 23 additions & 0 deletions .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -177,6 +187,10 @@ tasks:
commands:
- func: "run fuzzer"

- name: "wasm-test"
commands:
- func: "run wasm tests"

axes:
- id: "extra-rust-versions"
values:
Expand Down Expand Up @@ -207,6 +221,7 @@ buildvariants:
- ubuntu1804-test
tasks:
- name: "compile-only"

-
name: "lint"
display_name: "Lint"
Expand All @@ -224,3 +239,11 @@ buildvariants:
- ubuntu1804-test
tasks:
- name: "run-fuzzer"

-
name: "wasm"
display_name: "WASM"
run_on:
- ubuntu1804-test
tasks:
- name: "wasm-test"
10 changes: 10 additions & 0 deletions .evergreen/run-wasm-tests.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 6 additions & 2 deletions src/oid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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")
Expand Down
17 changes: 17 additions & 0 deletions wasm-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "bson-wasm-test"
version = "0.1.0"
authors = ["Abraham Egnor <[email protected]>"]
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"
2 changes: 2 additions & 0 deletions wasm-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(test)]
mod test;
6 changes: 6 additions & 0 deletions wasm-test/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use wasm_bindgen_test::wasm_bindgen_test;

#[wasm_bindgen_test]
fn objectid_new() {
let _ = bson::oid::ObjectId::new();
}

0 comments on commit f9a8b52

Please sign in to comment.