Skip to content

Commit 851361f

Browse files
committed
[eclipse-iceoryx#139] Add dynamic storage tests for dropping
1 parent bed825f commit 851361f

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

iceoryx2-bb/testing/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#[macro_use]
1414
pub mod assert;
15+
pub mod lifetime_tracker;
1516
pub mod watchdog;
1617

1718
#[macro_export(local_inner_macros)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2024 Contributors to the Eclipse Foundation
2+
//
3+
// See the NOTICE file(s) distributed with this work for additional
4+
// information regarding copyright ownership.
5+
//
6+
// This program and the accompanying materials are made available under the
7+
// terms of the Apache Software License 2.0 which is available at
8+
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9+
// which is available at https://opensource.org/licenses/MIT.
10+
//
11+
// SPDX-License-Identifier: Apache-2.0 OR MIT
12+
13+
use std::sync::atomic::{AtomicUsize, Ordering};
14+
15+
static CREATION_COUNTER: AtomicUsize = AtomicUsize::new(0);
16+
static DROP_COUNTER: AtomicUsize = AtomicUsize::new(0);
17+
18+
#[derive(Debug)]
19+
pub struct LifetimeTracker {}
20+
21+
impl LifetimeTracker {
22+
pub fn new() -> Self {
23+
CREATION_COUNTER.fetch_add(1, Ordering::Relaxed);
24+
25+
Self {}
26+
}
27+
28+
pub fn start_tracking() {
29+
CREATION_COUNTER.store(0, Ordering::Relaxed);
30+
DROP_COUNTER.store(0, Ordering::Relaxed);
31+
}
32+
33+
pub fn number_of_living_instances() -> usize {
34+
CREATION_COUNTER.load(Ordering::Relaxed) - DROP_COUNTER.load(Ordering::Relaxed)
35+
}
36+
}
37+
38+
impl Clone for LifetimeTracker {
39+
fn clone(&self) -> Self {
40+
Self::new()
41+
}
42+
}
43+
44+
impl Drop for LifetimeTracker {
45+
fn drop(&mut self) {
46+
DROP_COUNTER.fetch_add(1, Ordering::Relaxed);
47+
}
48+
}

iceoryx2-cal/tests/dynamic_storage_trait_tests.rs

+65
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod dynamic_storage {
1717
use iceoryx2_bb_elementary::math::ToB64;
1818
use iceoryx2_bb_posix::unique_system_id::UniqueSystemId;
1919
use iceoryx2_bb_system_types::file_name::FileName;
20+
use iceoryx2_bb_testing::lifetime_tracker::LifetimeTracker;
2021
use iceoryx2_bb_testing::{assert_that, test_requires};
2122
use iceoryx2_cal::dynamic_storage::*;
2223
use iceoryx2_cal::named_concept::*;
@@ -34,6 +35,7 @@ mod dynamic_storage {
3435
value: AtomicI64,
3536
supplementary_ptr: *mut u8,
3637
supplementary_len: usize,
38+
_lifetime_tracker: LifetimeTracker,
3739
}
3840

3941
impl TestData {
@@ -42,6 +44,7 @@ mod dynamic_storage {
4244
value: AtomicI64::new(value),
4345
supplementary_ptr: std::ptr::null_mut::<u8>(),
4446
supplementary_len: 0,
47+
_lifetime_tracker: LifetimeTracker::new(),
4548
}
4649
}
4750
}
@@ -462,6 +465,68 @@ mod dynamic_storage {
462465
assert_that!(Sut::does_exist(&sut_name), eq Ok(false));
463466
}
464467

468+
#[test]
469+
fn when_storage_is_removed_it_calls_drop<Sut: DynamicStorage<TestData>>() {
470+
let storage_name = generate_name();
471+
472+
LifetimeTracker::start_tracking();
473+
474+
let sut = Sut::Builder::new(&storage_name)
475+
.create(TestData::new(123))
476+
.unwrap();
477+
478+
assert_that!(sut.has_ownership(), eq true);
479+
drop(sut);
480+
481+
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
482+
}
483+
484+
#[test]
485+
fn when_storage_is_persistent_it_does_not_call_drop<Sut: DynamicStorage<TestData>>() {
486+
if Sut::does_support_persistency() {
487+
let storage_name = generate_name();
488+
489+
LifetimeTracker::start_tracking();
490+
491+
let sut = Sut::Builder::new(&storage_name)
492+
.create(TestData::new(123))
493+
.unwrap();
494+
sut.release_ownership();
495+
drop(sut);
496+
497+
assert_that!(LifetimeTracker::number_of_living_instances(), eq 1);
498+
}
499+
}
500+
501+
#[test]
502+
fn explicit_remove_of_persistent_storage_calls_drop<Sut: DynamicStorage<TestData>>() {
503+
let storage_name = generate_name();
504+
505+
LifetimeTracker::start_tracking();
506+
507+
let sut = Sut::Builder::new(&storage_name)
508+
.create(TestData::new(123))
509+
.unwrap();
510+
sut.release_ownership();
511+
// it leaks a memory mapping here but this we want explicitly to test remove also
512+
// for platforms that do not support persistent dynamic storage
513+
std::mem::forget(sut);
514+
515+
assert_that!(unsafe { Sut::remove(&storage_name) }, eq Ok(true));
516+
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
517+
}
518+
519+
#[test]
520+
fn remove_storage_with_unfinished_initialization_does_not_call_drop<
521+
Sut: DynamicStorage<TestData>,
522+
>() {
523+
}
524+
525+
#[test]
526+
fn opening_dynamic_storage_with_wrong_type_fails<Sut: DynamicStorage<TestData>>() {}
527+
528+
fn removing_dynamic_storage_with_wrong_type_fails<Sut: DynamicStorage<TestData>>() {}
529+
465530
#[instantiate_tests(<iceoryx2_cal::dynamic_storage::posix_shared_memory::Storage<TestData>>)]
466531
mod posix_shared_memory {}
467532

0 commit comments

Comments
 (0)