Skip to content

Commit f9a8b52

Browse files
authored
RUST-504 Use js timestamps when generating objectids (#406)
1 parent fda2cf3 commit f9a8b52

File tree

7 files changed

+67
-2
lines changed

7 files changed

+67
-2
lines changed

.evergreen/config.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ functions:
133133
${PREPARE_SHELL}
134134
.evergreen/check-rustdoc.sh
135135
136+
"run wasm tests":
137+
- command: shell.exec
138+
type: test
139+
params:
140+
shell: bash
141+
working_dir: "src"
142+
script: |
143+
${PREPARE_SHELL}
144+
.evergreen/run-wasm-tests.sh
145+
136146
"init test-results":
137147
- command: shell.exec
138148
params:
@@ -177,6 +187,10 @@ tasks:
177187
commands:
178188
- func: "run fuzzer"
179189

190+
- name: "wasm-test"
191+
commands:
192+
- func: "run wasm tests"
193+
180194
axes:
181195
- id: "extra-rust-versions"
182196
values:
@@ -207,6 +221,7 @@ buildvariants:
207221
- ubuntu1804-test
208222
tasks:
209223
- name: "compile-only"
224+
210225
-
211226
name: "lint"
212227
display_name: "Lint"
@@ -224,3 +239,11 @@ buildvariants:
224239
- ubuntu1804-test
225240
tasks:
226241
- name: "run-fuzzer"
242+
243+
-
244+
name: "wasm"
245+
display_name: "WASM"
246+
run_on:
247+
- ubuntu1804-test
248+
tasks:
249+
- name: "wasm-test"

.evergreen/run-wasm-tests.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
set -o errexit
4+
5+
. ~/.cargo/env
6+
7+
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
8+
9+
cd $(dirname $0)/../wasm-test
10+
wasm-pack test --node

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ serde_with = { version = "1", optional = true }
6969
time = { version = "0.3.9", features = ["formatting", "parsing", "macros", "large-dates"] }
7070
bitvec = "1.0.1"
7171

72+
[target.'cfg(target_arch = "wasm32")'.dependencies]
73+
js-sys = "0.3"
74+
7275
[dev-dependencies]
7376
assert_matches = "1.2"
7477
criterion = "0.3.0"

src/oid.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
//! For more information, see the documentation for the [`ObjectId`] type.
33
44
use std::{
5-
convert::TryInto,
65
error,
76
fmt,
87
result,
98
str::FromStr,
109
sync::atomic::{AtomicUsize, Ordering},
11-
time::SystemTime,
1210
};
1311

12+
#[cfg(not(target_arch = "wasm32"))]
13+
use std::{convert::TryInto, time::SystemTime};
14+
1415
use hex::{self, FromHexError};
1516
use rand::{thread_rng, Rng};
1617

@@ -240,6 +241,9 @@ impl ObjectId {
240241
/// Generates a new timestamp representing the current seconds since epoch.
241242
/// Represented in Big Endian.
242243
fn gen_timestamp() -> [u8; 4] {
244+
#[cfg(target_arch = "wasm32")]
245+
let timestamp: u32 = (js_sys::Date::now() / 1000.0) as u32;
246+
#[cfg(not(target_arch = "wasm32"))]
243247
let timestamp: u32 = SystemTime::now()
244248
.duration_since(SystemTime::UNIX_EPOCH)
245249
.expect("system clock is before 1970")

wasm-test/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "bson-wasm-test"
3+
version = "0.1.0"
4+
authors = ["Abraham Egnor <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[lib]
10+
crate-type = ["cdylib", "rlib"]
11+
12+
[dependencies]
13+
bson = { path = ".." }
14+
getrandom = { version = "0.2", features = ["js"] }
15+
16+
[dev-dependencies]
17+
wasm-bindgen-test = "0.3.0"

wasm-test/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[cfg(test)]
2+
mod test;

wasm-test/src/test.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use wasm_bindgen_test::wasm_bindgen_test;
2+
3+
#[wasm_bindgen_test]
4+
fn objectid_new() {
5+
let _ = bson::oid::ObjectId::new();
6+
}

0 commit comments

Comments
 (0)