Skip to content

Commit

Permalink
Merge pull request eclipse-iceoryx#482 from elBoberido/iox2-349-add-a…
Browse files Browse the repository at this point in the history
…ll-remaining-tests-to-bazel

[eclipse-iceoryx#349] Add all remaining tests to bazel
  • Loading branch information
elBoberido authored Oct 21, 2024
2 parents 4dc37ec + 55e736a commit 5f6a0dc
Show file tree
Hide file tree
Showing 62 changed files with 1,716 additions and 715 deletions.
9 changes: 9 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ build:mingw --cxxopt="-std=c++17"

build --action_env=CARGO_BAZEL_REPIN=true

# test --local_test_jobs=1
# test --test_strategy=standalone
# test --spawn_strategy=standalone
# build --strategy=Genrule=standalone

test --test_output=streamed
test --nocache_test_results
test --action_env=RUST_TEST_THREADS=1

#
# feature flags
#
Expand Down
71 changes: 62 additions & 9 deletions doc/bazel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,61 @@ build --@iceoryx2//:foo=on
| logger_log | auto, on, off | auto == off |
| logger_tracing | auto, on, off | auto == off |

### Running iceory2x Tests in External Project

In general, the iceoryx2 tests can be run in parallel. However, there are
exceptions, as some tests deliberately try to bring the system into an
inconsistent state. When these tests are executed in parallel, they can become
flaky and may fail depending on which other tests are running concurrently.

To mitigate this, it is sufficient to prevent other tests from the same file
from running in parallel. This can be achieved by setting the following
environment variable in your .bashrc:

```bazel
test --action_env=RUST_TEST_THREADS=1
```

Assuming there are two test binaries, without the environment variable, all
tests would be executed in parallel, as illustrated below:

```ascii
bazel test /...
|
+--------------------+
| |
test-binary-A test-binary-B
| |
+----------+ +----------+
| | | |
test-A-1 test-A2 test-B-1 test-B2
| | | |
+----------+ +----------+
| |
+--------------------+
|
test result
```

With the environment variable set, the test execution is partially serialized,
as shown below:

```ascii
bazel test /...
|
+--------------------+
| |
test-binary-A test-binary-B
| |
test-A-1 test-B-1
| |
test-A-2 test-B-2
| |
+--------------------+
|
test result
```

## Instructions for iceoryx2 Developers

When working with Bazel and Cargo in this project, ensure the following steps are
Expand All @@ -259,15 +314,6 @@ within the `BUILD.bazel` file.
file, it must also be included in the `crate_index` target located in the
`WORKSPACE.bazel` file.

### Updating Dependencies

Any time a dependency is added or changed, the `Cargo.Bazel.lock` file must be
updated by running:

```bash
CARGO_BAZEL_REPIN=1 bazel build //...
```

### Common Pitfalls

1. **Handling `iceoryx2-ffi-cbindgen` Target**:
Expand All @@ -282,3 +328,10 @@ Every `BUILD.bazel` file includes an `all_srcs` filegroup to manage the source f
within the sandbox. The root `BUILD.bazel` file has an `all_srcs` filegroup that
references all sub-packages. When a new package is added, it must also be included
in this `all_srcs` filegroup.

1. **Not All Environment Variables are Available with Bazel**

`bazel` does not automatically export some environment variables that are
typically available with `cargo`, such as `CARGO_PKG_VERSION`. In these cases,
you will need to either set the environment variable manually in your `bazel`
configuration or find an appropriate workaround.
13 changes: 11 additions & 2 deletions iceoryx2-bb/container/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package(default_visibility = ["//visibility:public"])

load("@rules_rust//rust:defs.bzl", "rust_library")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test_suite")

filegroup(
name = "all_srcs",
Expand All @@ -33,4 +33,13 @@ rust_library(
],
)

# TODO: [349] add tests
rust_test_suite(
name = "iceoryx2-bb-container-tests",
srcs = glob(["tests/**/*.rs"]),
deps = [
":iceoryx2-bb-container",
"//iceoryx2-bb/elementary:iceoryx2-bb-elementary",
"//iceoryx2-bb/testing:iceoryx2-bb-testing",
"@crate_index//:serde_test",
],
)
24 changes: 12 additions & 12 deletions iceoryx2-bb/container/tests/queue_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,35 +203,35 @@ mod queue {

#[test]
fn drops_all_objects_when_out_of_scope() {
LifetimeTracker::start_tracking();
let state = LifetimeTracker::start_tracking();
let mut sut = FixedSizeQueue::<LifetimeTracker, SUT_CAPACITY>::new();

for _ in 0..sut.capacity() {
sut.push(LifetimeTracker::new());
}

assert_that!(LifetimeTracker::number_of_living_instances(), eq SUT_CAPACITY);
assert_that!(state.number_of_living_instances(), eq SUT_CAPACITY);
drop(sut);
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
assert_that!(state.number_of_living_instances(), eq 0);
}

#[test]
fn drops_all_objects_with_clear() {
LifetimeTracker::start_tracking();
let state = LifetimeTracker::start_tracking();
let mut sut = FixedSizeQueue::<LifetimeTracker, SUT_CAPACITY>::new();

for _ in 0..sut.capacity() {
sut.push(LifetimeTracker::new());
}

assert_that!(LifetimeTracker::number_of_living_instances(), eq SUT_CAPACITY);
assert_that!(state.number_of_living_instances(), eq SUT_CAPACITY);
sut.clear();
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
assert_that!(state.number_of_living_instances(), eq 0);
}

#[test]
fn pop_releases_object() {
LifetimeTracker::start_tracking();
let state = LifetimeTracker::start_tracking();
let mut sut = FixedSizeQueue::<LifetimeTracker, SUT_CAPACITY>::new();

for _ in 0..sut.capacity() {
Expand All @@ -242,34 +242,34 @@ mod queue {
let result = sut.pop();
assert_that!(result, is_some);
drop(result);
assert_that!(LifetimeTracker::number_of_living_instances(), eq i);
assert_that!(state.number_of_living_instances(), eq i);
}
}

#[test]
fn queue_clear_drops_all_objects() {
LifetimeTracker::start_tracking();
let state = LifetimeTracker::start_tracking();
let mut sut = Queue::<LifetimeTracker>::new(SUT_CAPACITY);

for _ in 0..sut.capacity() {
sut.push(LifetimeTracker::new());
}

sut.clear();
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
assert_that!(state.number_of_living_instances(), eq 0);
}

#[test]
fn fixed_size_queue_clear_drops_all_objects() {
LifetimeTracker::start_tracking();
let state = LifetimeTracker::start_tracking();
let mut sut = FixedSizeQueue::<LifetimeTracker, SUT_CAPACITY>::new();

for _ in 0..sut.capacity() {
sut.push(LifetimeTracker::new());
}

sut.clear();
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
assert_that!(state.number_of_living_instances(), eq 0);
}

#[test]
Expand Down
16 changes: 8 additions & 8 deletions iceoryx2-bb/container/tests/vec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,35 +205,35 @@ mod fixed_size_vec {

#[test]
fn drops_all_objects_when_out_of_scope() {
LifetimeTracker::start_tracking();
let state = LifetimeTracker::start_tracking();
let mut sut = FixedSizeVec::<LifetimeTracker, SUT_CAPACITY>::new();

for _ in 0..SUT_CAPACITY {
assert_that!(sut.push(LifetimeTracker::new()), eq true);
}

assert_that!(LifetimeTracker::number_of_living_instances(), eq SUT_CAPACITY);
assert_that!(state.number_of_living_instances(), eq SUT_CAPACITY);
drop(sut);
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
assert_that!(state.number_of_living_instances(), eq 0);
}

#[test]
fn drops_all_objects_with_clear() {
LifetimeTracker::start_tracking();
let state = LifetimeTracker::start_tracking();
let mut sut = FixedSizeVec::<LifetimeTracker, SUT_CAPACITY>::new();

for _ in 0..SUT_CAPACITY {
assert_that!(sut.push(LifetimeTracker::new()), eq true);
}

assert_that!(LifetimeTracker::number_of_living_instances(), eq SUT_CAPACITY);
assert_that!(state.number_of_living_instances(), eq SUT_CAPACITY);
sut.clear();
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
assert_that!(state.number_of_living_instances(), eq 0);
}

#[test]
fn pop_releases_ownership() {
LifetimeTracker::start_tracking();
let state = LifetimeTracker::start_tracking();
let mut sut = FixedSizeVec::<LifetimeTracker, SUT_CAPACITY>::new();

for _ in 0..SUT_CAPACITY {
Expand All @@ -244,7 +244,7 @@ mod fixed_size_vec {
let result = sut.pop();
assert_that!(result, is_some);
drop(result);
assert_that!(LifetimeTracker::number_of_living_instances(), eq i);
assert_that!(state.number_of_living_instances(), eq i);
}
}

Expand Down
15 changes: 13 additions & 2 deletions iceoryx2-bb/derive-macros/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package(default_visibility = ["//visibility:public"])

load("@rules_rust//rust:defs.bzl", "rust_proc_macro")
load("@rules_rust//rust:defs.bzl", "rust_proc_macro", "rust_test_suite")

filegroup(
name = "all_srcs",
Expand All @@ -30,4 +30,15 @@ rust_proc_macro(
],
)

# TODO: [349] add tests
rust_test_suite(
name = "iceoryx2-bb-derive-macros-tests",
srcs = glob(["tests/**/*.rs"]),
deps = [
"//iceoryx2-bb/elementary:iceoryx2-bb-elementary",
"//iceoryx2-bb/testing:iceoryx2-bb-testing",
],
proc_macro_deps = [
":iceoryx2-bb-derive-macros",
"@crate_index//:generic-tests",
],
)
14 changes: 12 additions & 2 deletions iceoryx2-bb/elementary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package(default_visibility = ["//visibility:public"])

load("@rules_rust//rust:defs.bzl", "rust_library")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test_suite")

filegroup(
name = "all_srcs",
Expand All @@ -27,4 +27,14 @@ rust_library(
],
)

# TODO: [349] add tests
rust_test_suite(
name = "iceoryx2-bb-elementary-tests",
srcs = glob(["tests/**/*.rs"]),
deps = [
":iceoryx2-bb-elementary",
"//iceoryx2-bb/testing:iceoryx2-bb-testing",
],
proc_macro_deps = [
"@crate_index//:generic-tests",
],
)
7 changes: 6 additions & 1 deletion iceoryx2-bb/elementary/tests/package_version_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

use iceoryx2_bb_elementary::package_version::PackageVersion;
use iceoryx2_bb_testing::assert_that;
use iceoryx2_bb_testing::{assert_that, test_requires};

#[test]
fn package_version_works() {
// NOTE: The test is skipped when not run with cargo but with bazel
// The CI which runs with cargo ensures that the constants defined
// in PackageVersion::get equal the package version.
test_requires!(option_env!("CARGO").is_some());

let major = option_env!("CARGO_PKG_VERSION_MAJOR")
.and_then(|s| s.parse::<u16>().ok())
.expect("Contains a valid major version number.");
Expand Down
16 changes: 14 additions & 2 deletions iceoryx2-bb/lock-free/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package(default_visibility = ["//visibility:public"])

load("@rules_rust//rust:defs.bzl", "rust_library")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test_suite")

filegroup(
name = "all_srcs",
Expand All @@ -30,4 +30,16 @@ rust_library(
],
)

# TODO: [349] add tests
rust_test_suite(
name = "iceoryx2-bb-lock-free-tests",
srcs = glob(["tests/**/*.rs"]),
deps = [
":iceoryx2-bb-lock-free",
"//iceoryx2-bb/elementary:iceoryx2-bb-elementary",
"//iceoryx2-bb/posix:iceoryx2-bb-posix",
"//iceoryx2-bb/testing:iceoryx2-bb-testing",
],
proc_macro_deps = [
"@crate_index//:generic-tests",
],
)
15 changes: 13 additions & 2 deletions iceoryx2-bb/memory/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package(default_visibility = ["//visibility:public"])

load("@rules_rust//rust:defs.bzl", "rust_library")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test_suite")

filegroup(
name = "all_srcs",
Expand All @@ -33,4 +33,15 @@ rust_library(
],
)

# TODO: [349] add tests
rust_test_suite(
name = "iceoryx2-bb-memory-tests",
srcs = glob(["tests/**/*.rs"]),
deps = [
"iceoryx2-bb-memory",
"//iceoryx2-bb/elementary:iceoryx2-bb-elementary",
"//iceoryx2-bb/testing:iceoryx2-bb-testing",
],
proc_macro_deps = [
"@crate_index//:generic-tests",
],
)
Loading

0 comments on commit 5f6a0dc

Please sign in to comment.