Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUST-504 Use js timestamps when generating objectids #406

Merged
merged 2 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -68,6 +68,9 @@ serde_bytes = "0.11.5"
serde_with = { version = "1", optional = true }
time = { version = "0.3.9", features = ["formatting", "parsing", "macros", "large-dates"] }

[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 <abe.egnor@gmail.com>"]
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();
}