-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from elast0ny/feature/add_tests
Feature/add tests
- Loading branch information
Showing
7 changed files
with
181 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,11 +43,16 @@ jobs: | |
uses: actions-rs/[email protected] | ||
with: | ||
command: build | ||
args: --all | ||
|
||
- name: Tests | ||
uses: actions-rs/[email protected] | ||
with: | ||
command: test | ||
|
||
- name: Examples | ||
uses: actions-rs/[email protected] | ||
env: | ||
RUST_LOG: "trace" | ||
with: | ||
command: run | ||
args: --all-features --example mutex -- 2 | ||
args: --all-features --example mutex -- 15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[package] | ||
name = "shared_memory" | ||
description = "A user friendly crate that allows you to share memory between processes" | ||
version = "0.12.3" | ||
version = "0.12.4" | ||
authors = ["ElasT0ny <[email protected]>"] | ||
license = "MIT OR Apache-2.0" | ||
edition = "2018" | ||
|
@@ -35,7 +35,7 @@ nix = "0.23" | |
libc = "0.2" | ||
|
||
[target.'cfg(windows)'.dependencies] | ||
win-sys = "0.2" | ||
win-sys = "0.3" | ||
|
||
[dev-dependencies] | ||
raw_sync = "0.1" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use std::path::Path; | ||
|
||
use shared_memory::ShmemConf; | ||
|
||
#[test] | ||
fn create_new() { | ||
let mut s = ShmemConf::new().size(4090).create().unwrap(); | ||
|
||
assert!(s.is_owner()); | ||
assert!(!s.get_os_id().is_empty()); | ||
assert!(s.len() >= 4090); | ||
assert!(!s.as_ptr().is_null()); | ||
unsafe { | ||
assert_eq!(s.as_slice().len(), s.len()); | ||
assert_eq!(s.as_slice_mut().len(), s.len()); | ||
} | ||
} | ||
|
||
#[test] | ||
fn create_with_flink() { | ||
let flink = Path::new("create_new1"); | ||
|
||
let mut s = ShmemConf::new().flink(flink).size(4090).create().unwrap(); | ||
|
||
assert!(s.is_owner()); | ||
assert!(!s.get_os_id().is_empty()); | ||
assert!(flink.is_file()); | ||
assert!(s.len() >= 4090); | ||
assert!(!s.as_ptr().is_null()); | ||
unsafe { | ||
assert_eq!(s.as_slice().len(), s.len()); | ||
assert_eq!(s.as_slice_mut().len(), s.len()); | ||
} | ||
|
||
drop(s); | ||
|
||
assert!(!flink.is_file()); | ||
} | ||
|
||
#[test] | ||
fn open_os_id() { | ||
let s1 = ShmemConf::new().size(4090).create().unwrap(); | ||
|
||
// Open with the unique os id | ||
let os_id = s1.get_os_id().to_string(); | ||
let mut s2 = ShmemConf::new().os_id(&os_id).open().unwrap(); | ||
|
||
assert!(!s2.is_owner()); | ||
assert!(!s2.get_os_id().is_empty()); | ||
assert!(s2.len() >= 4090); | ||
assert!(!s2.as_ptr().is_null()); | ||
unsafe { | ||
assert_eq!(s2.as_slice().len(), s2.len()); | ||
assert_eq!(s2.as_slice_mut().len(), s2.len()); | ||
} | ||
|
||
// Drop the owner of the mapping | ||
drop(s1); | ||
|
||
// Make sure it can be openned again | ||
assert!(ShmemConf::new().os_id(&os_id).open().is_err()); | ||
|
||
drop(s2); | ||
} | ||
|
||
#[test] | ||
fn open_flink() { | ||
let flink = Path::new("create_new2"); | ||
let s1 = ShmemConf::new().flink(flink).size(4090).create().unwrap(); | ||
|
||
// Open with file base link | ||
let mut s2 = ShmemConf::new().flink(&flink).open().unwrap(); | ||
|
||
assert!(!s2.is_owner()); | ||
assert!(!s2.get_os_id().is_empty()); | ||
assert!(flink.is_file()); | ||
assert!(s2.len() >= 4090); | ||
assert!(!s2.as_ptr().is_null()); | ||
unsafe { | ||
assert_eq!(s2.as_slice().len(), s2.len()); | ||
assert_eq!(s2.as_slice_mut().len(), s2.len()); | ||
} | ||
|
||
// Drop the owner of the mapping | ||
drop(s1); | ||
|
||
// Make sure it can be openned again | ||
assert!(ShmemConf::new().flink(&flink).open().is_err()); | ||
|
||
drop(s2); | ||
} | ||
|
||
#[test] | ||
fn share_data() { | ||
let s1 = ShmemConf::new() | ||
.size(core::mem::size_of::<u32>()) | ||
.create() | ||
.unwrap(); | ||
|
||
// Open with the unique os id | ||
let os_id = s1.get_os_id().to_string(); | ||
let s2 = ShmemConf::new().os_id(&os_id).open().unwrap(); | ||
|
||
let ptr1 = s1.as_ptr() as *mut u32; | ||
let ptr2 = s2.as_ptr() as *mut u32; | ||
|
||
// Confirm that the two pointers are different | ||
assert_ne!(ptr1, ptr2); | ||
|
||
// Write a value from s1 and read it from s2 | ||
unsafe { | ||
let shared_val = 0xBADC0FEE; | ||
ptr1.write_volatile(shared_val); | ||
let read_val = ptr2.read_volatile(); | ||
assert_eq!(read_val, shared_val); | ||
} | ||
} |