Skip to content

Commit 6ae76f8

Browse files
Utkarsh Mehtautkmehta
authored andcommitted
RUST-484 Add a test lock and move integration tests
1 parent eb5d9d7 commit 6ae76f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+153
-31
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ base64 = "0.12.1"
4141
assert_matches = "1.2"
4242
serde_bytes = "0.11"
4343
pretty_assertions = "0.6.1"
44+
lazy_static = "1.4.0"
4445

4546
[package.metadata.docs.rs]
4647
features = ["decimal128"]

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,6 @@ pub mod extjson;
214214
pub mod oid;
215215
pub mod ser;
216216
pub mod spec;
217+
218+
#[cfg(test)]
219+
mod tests;

src/oid.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,12 @@ impl fmt::Debug for ObjectId {
198198
}
199199
}
200200

201+
#[cfg(test)]
202+
use crate::tests::LOCK;
203+
201204
#[test]
202205
fn count_generated_is_big_endian() {
206+
let _guard = LOCK.run_exclusively();
203207
let start = 1_122_866;
204208
OID_COUNTER.store(start, Ordering::SeqCst);
205209

src/tests/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mod modules;
2+
mod spec;
3+
4+
use lazy_static::lazy_static;
5+
use modules::TestLock;
6+
7+
lazy_static! {
8+
pub(crate) static ref LOCK: TestLock = TestLock::new();
9+
}

tests/modules/bson.rs renamed to src/tests/modules/bson.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::convert::TryFrom;
22

3-
use bson::{
3+
use crate::{
44
doc,
55
oid::ObjectId,
66
spec::BinarySubtype,
7+
tests::LOCK,
78
Binary,
89
Bson,
910
Document,
@@ -15,6 +16,7 @@ use serde_json::{json, Value};
1516

1617
#[test]
1718
fn to_json() {
19+
let _guard = LOCK.run_concurrently();
1820
let mut doc = Document::new();
1921
doc.insert(
2022
"_id",
@@ -49,19 +51,22 @@ fn to_json() {
4951

5052
#[test]
5153
fn bson_default() {
54+
let _guard = LOCK.run_concurrently();
5255
let bson1 = Bson::default();
5356
assert_eq!(bson1, Bson::Null);
5457
}
5558

5659
#[test]
5760
fn document_default() {
61+
let _guard = LOCK.run_concurrently();
5862
let doc1 = Document::default();
5963
assert_eq!(doc1.keys().count(), 0);
6064
assert_eq!(doc1, Document::new());
6165
}
6266

6367
#[test]
6468
fn from_impls() {
69+
let _guard = LOCK.run_concurrently();
6570
assert_eq!(Bson::from(1.5f32), Bson::Double(1.5));
6671
assert_eq!(Bson::from(2.25f64), Bson::Double(2.25));
6772
assert_eq!(Bson::from("data"), Bson::String(String::from("data")));
@@ -147,6 +152,7 @@ fn from_impls() {
147152

148153
#[test]
149154
fn timestamp_ordering() {
155+
let _guard = LOCK.run_concurrently();
150156
let ts1 = Timestamp {
151157
time: 0,
152158
increment: 1,

src/tests/modules/lock.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
2+
3+
#[derive(Default)]
4+
pub struct TestLock {
5+
inner: RwLock<()>,
6+
}
7+
8+
impl TestLock {
9+
pub fn new() -> Self {
10+
Default::default()
11+
}
12+
13+
pub fn run_concurrently(&self) -> RwLockReadGuard<'_, ()> {
14+
self.inner.read().unwrap()
15+
}
16+
17+
pub fn run_exclusively(&self) -> RwLockWriteGuard<'_, ()> {
18+
self.inner.write().unwrap()
19+
}
20+
}

tests/modules/macros.rs renamed to src/tests/modules/macros.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use bson::{doc, oid::ObjectId, spec::BinarySubtype, Binary, Bson, Regex, Timestamp};
1+
use crate::{doc, oid::ObjectId, spec::BinarySubtype, tests::LOCK, Binary, Bson, Regex, Timestamp};
22
use chrono::offset::Utc;
33
use pretty_assertions::assert_eq;
44

55
#[test]
66
fn standard_format() {
7+
let _guard = LOCK.run_concurrently();
78
let id_string = "thisismyname";
89
let string_bytes: Vec<_> = id_string.bytes().collect();
910
let mut bytes = [0; 12];
@@ -52,6 +53,7 @@ fn standard_format() {
5253

5354
#[test]
5455
fn non_trailing_comma() {
56+
let _guard = LOCK.run_concurrently();
5557
let doc = doc! {
5658
"a": "foo",
5759
"b": { "ok": "then" }
@@ -64,6 +66,7 @@ fn non_trailing_comma() {
6466
#[test]
6567
#[allow(clippy::float_cmp)]
6668
fn recursive_macro() {
69+
let _guard = LOCK.run_concurrently();
6770
let doc = doc! {
6871
"a": "foo",
6972
"b": {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
mod bson;
2+
mod lock;
23
mod macros;
34
mod oid;
45
mod ordered;
56
mod ser;
67
mod serializer_deserializer;
8+
9+
pub use self::lock::TestLock;

tests/modules/oid.rs renamed to src/tests/modules/oid.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use bson::oid::ObjectId;
1+
use crate::{oid::ObjectId, tests::LOCK};
22

33
#[test]
44
fn string_oid() {
5+
let _guard = LOCK.run_concurrently();
56
let s = "123456789012123456789012";
67
let oid_res = ObjectId::with_string(s);
78
assert!(oid_res.is_ok());
@@ -11,6 +12,7 @@ fn string_oid() {
1112

1213
#[test]
1314
fn byte_string_oid() {
15+
let _guard = LOCK.run_concurrently();
1416
let s = "541b1a00e8a23afa832b218e";
1517
let oid_res = ObjectId::with_string(s);
1618
assert!(oid_res.is_ok());
@@ -26,18 +28,21 @@ fn byte_string_oid() {
2628

2729
#[test]
2830
fn oid_equals() {
31+
let _guard = LOCK.run_concurrently();
2932
let oid = ObjectId::new();
3033
assert_eq!(oid, oid);
3134
}
3235

3336
#[test]
3437
fn oid_not_equals() {
38+
let _guard = LOCK.run_concurrently();
3539
assert!(ObjectId::new() != ObjectId::new());
3640
}
3741

3842
// check that the last byte in objectIDs is increasing
3943
#[test]
4044
fn counter_increasing() {
45+
let _guard = LOCK.run_concurrently();
4146
let oid1_bytes = ObjectId::new().bytes();
4247
let oid2_bytes = ObjectId::new().bytes();
4348
assert!(oid1_bytes[11] < oid2_bytes[11]);

tests/modules/ordered.rs renamed to src/tests/modules/ordered.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#[cfg(feature = "decimal128")]
2-
use bson::decimal128::Decimal128;
3-
use bson::{
2+
use crate::decimal128::Decimal128;
3+
use crate::{
44
doc,
55
document::ValueAccessError,
66
oid::ObjectId,
77
spec::BinarySubtype,
8+
tests::LOCK,
89
Binary,
910
Bson,
1011
Document,
@@ -14,6 +15,7 @@ use chrono::Utc;
1415

1516
#[test]
1617
fn ordered_insert() {
18+
let _guard = LOCK.run_concurrently();
1719
let mut doc = Document::new();
1820
doc.insert("first".to_owned(), Bson::Int32(1));
1921
doc.insert("second".to_owned(), Bson::String("foo".to_owned()));
@@ -31,6 +33,7 @@ fn ordered_insert() {
3133

3234
#[test]
3335
fn ordered_insert_shorthand() {
36+
let _guard = LOCK.run_concurrently();
3437
let mut doc = Document::new();
3538
doc.insert("first", 1i32);
3639
doc.insert("second", "foo");
@@ -48,17 +51,16 @@ fn ordered_insert_shorthand() {
4851

4952
#[cfg(feature = "decimal128")]
5053
fn test_decimal128(doc: &mut Document) {
54+
let _guard = LOCK.run_concurrently();
5155
let dec = Decimal128::from_str("968E+1");
5256
doc.insert("decimal128".to_string(), Bson::Decimal128(dec.clone()));
5357
assert_eq!(Some(&Bson::Decimal128(dec.clone())), doc.get("decimal128"));
5458
assert_eq!(Ok(&dec), doc.get_decimal128("decimal128"));
5559
}
5660

57-
#[cfg(not(feature = "decimal128"))]
58-
fn test_decimal128(_doc: &mut Document) {}
59-
6061
#[test]
6162
fn test_getters() {
63+
let _guard = LOCK.run_concurrently();
6264
let datetime = Utc::now();
6365
let cloned_dt = datetime;
6466
let binary = vec![0, 1, 2, 3, 4];
@@ -137,6 +139,7 @@ fn test_getters() {
137139
assert_eq!(Some(&Bson::DateTime(datetime)), doc.get("datetime"));
138140
assert_eq!(Ok(&datetime), doc.get_datetime("datetime"));
139141

142+
#[cfg(feature = "decimal128")]
140143
test_decimal128(&mut doc);
141144

142145
assert_eq!(Some(&Bson::DateTime(datetime)), doc.get("datetime"));
@@ -159,6 +162,7 @@ fn test_getters() {
159162

160163
#[test]
161164
fn remove() {
165+
let _guard = LOCK.run_concurrently();
162166
let mut doc = Document::new();
163167
doc.insert("first", Bson::Int32(1));
164168
doc.insert("second", Bson::String("foo".to_owned()));
@@ -175,6 +179,7 @@ fn remove() {
175179

176180
#[test]
177181
fn entry() {
182+
let _guard = LOCK.run_concurrently();
178183
let mut doc = doc! {
179184
"first": 1i32,
180185
"second": "foo",

0 commit comments

Comments
 (0)