Skip to content

Commit 5f6a0dc

Browse files
authored
Merge pull request #482 from elBoberido/iox2-349-add-all-remaining-tests-to-bazel
[#349] Add all remaining tests to bazel
2 parents 4dc37ec + 55e736a commit 5f6a0dc

Some content is hidden

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

62 files changed

+1716
-715
lines changed

.bazelrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ build:mingw --cxxopt="-std=c++17"
77

88
build --action_env=CARGO_BAZEL_REPIN=true
99

10+
# test --local_test_jobs=1
11+
# test --test_strategy=standalone
12+
# test --spawn_strategy=standalone
13+
# build --strategy=Genrule=standalone
14+
15+
test --test_output=streamed
16+
test --nocache_test_results
17+
test --action_env=RUST_TEST_THREADS=1
18+
1019
#
1120
# feature flags
1221
#

doc/bazel/README.md

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,61 @@ build --@iceoryx2//:foo=on
244244
| logger_log | auto, on, off | auto == off |
245245
| logger_tracing | auto, on, off | auto == off |
246246

247+
### Running iceory2x Tests in External Project
248+
249+
In general, the iceoryx2 tests can be run in parallel. However, there are
250+
exceptions, as some tests deliberately try to bring the system into an
251+
inconsistent state. When these tests are executed in parallel, they can become
252+
flaky and may fail depending on which other tests are running concurrently.
253+
254+
To mitigate this, it is sufficient to prevent other tests from the same file
255+
from running in parallel. This can be achieved by setting the following
256+
environment variable in your .bashrc:
257+
258+
```bazel
259+
test --action_env=RUST_TEST_THREADS=1
260+
```
261+
262+
Assuming there are two test binaries, without the environment variable, all
263+
tests would be executed in parallel, as illustrated below:
264+
265+
```ascii
266+
bazel test /...
267+
|
268+
+--------------------+
269+
| |
270+
test-binary-A test-binary-B
271+
| |
272+
+----------+ +----------+
273+
| | | |
274+
test-A-1 test-A2 test-B-1 test-B2
275+
| | | |
276+
+----------+ +----------+
277+
| |
278+
+--------------------+
279+
|
280+
test result
281+
```
282+
283+
With the environment variable set, the test execution is partially serialized,
284+
as shown below:
285+
286+
```ascii
287+
bazel test /...
288+
|
289+
+--------------------+
290+
| |
291+
test-binary-A test-binary-B
292+
| |
293+
test-A-1 test-B-1
294+
| |
295+
test-A-2 test-B-2
296+
| |
297+
+--------------------+
298+
|
299+
test result
300+
```
301+
247302
## Instructions for iceoryx2 Developers
248303

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

262-
### Updating Dependencies
263-
264-
Any time a dependency is added or changed, the `Cargo.Bazel.lock` file must be
265-
updated by running:
266-
267-
```bash
268-
CARGO_BAZEL_REPIN=1 bazel build //...
269-
```
270-
271317
### Common Pitfalls
272318

273319
1. **Handling `iceoryx2-ffi-cbindgen` Target**:
@@ -282,3 +328,10 @@ Every `BUILD.bazel` file includes an `all_srcs` filegroup to manage the source f
282328
within the sandbox. The root `BUILD.bazel` file has an `all_srcs` filegroup that
283329
references all sub-packages. When a new package is added, it must also be included
284330
in this `all_srcs` filegroup.
331+
332+
1. **Not All Environment Variables are Available with Bazel**
333+
334+
`bazel` does not automatically export some environment variables that are
335+
typically available with `cargo`, such as `CARGO_PKG_VERSION`. In these cases,
336+
you will need to either set the environment variable manually in your `bazel`
337+
configuration or find an appropriate workaround.

iceoryx2-bb/container/BUILD.bazel

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

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

15-
load("@rules_rust//rust:defs.bzl", "rust_library")
15+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test_suite")
1616

1717
filegroup(
1818
name = "all_srcs",
@@ -33,4 +33,13 @@ rust_library(
3333
],
3434
)
3535

36-
# TODO: [349] add tests
36+
rust_test_suite(
37+
name = "iceoryx2-bb-container-tests",
38+
srcs = glob(["tests/**/*.rs"]),
39+
deps = [
40+
":iceoryx2-bb-container",
41+
"//iceoryx2-bb/elementary:iceoryx2-bb-elementary",
42+
"//iceoryx2-bb/testing:iceoryx2-bb-testing",
43+
"@crate_index//:serde_test",
44+
],
45+
)

iceoryx2-bb/container/tests/queue_tests.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,35 +203,35 @@ mod queue {
203203

204204
#[test]
205205
fn drops_all_objects_when_out_of_scope() {
206-
LifetimeTracker::start_tracking();
206+
let state = LifetimeTracker::start_tracking();
207207
let mut sut = FixedSizeQueue::<LifetimeTracker, SUT_CAPACITY>::new();
208208

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

213-
assert_that!(LifetimeTracker::number_of_living_instances(), eq SUT_CAPACITY);
213+
assert_that!(state.number_of_living_instances(), eq SUT_CAPACITY);
214214
drop(sut);
215-
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
215+
assert_that!(state.number_of_living_instances(), eq 0);
216216
}
217217

218218
#[test]
219219
fn drops_all_objects_with_clear() {
220-
LifetimeTracker::start_tracking();
220+
let state = LifetimeTracker::start_tracking();
221221
let mut sut = FixedSizeQueue::<LifetimeTracker, SUT_CAPACITY>::new();
222222

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

227-
assert_that!(LifetimeTracker::number_of_living_instances(), eq SUT_CAPACITY);
227+
assert_that!(state.number_of_living_instances(), eq SUT_CAPACITY);
228228
sut.clear();
229-
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
229+
assert_that!(state.number_of_living_instances(), eq 0);
230230
}
231231

232232
#[test]
233233
fn pop_releases_object() {
234-
LifetimeTracker::start_tracking();
234+
let state = LifetimeTracker::start_tracking();
235235
let mut sut = FixedSizeQueue::<LifetimeTracker, SUT_CAPACITY>::new();
236236

237237
for _ in 0..sut.capacity() {
@@ -242,34 +242,34 @@ mod queue {
242242
let result = sut.pop();
243243
assert_that!(result, is_some);
244244
drop(result);
245-
assert_that!(LifetimeTracker::number_of_living_instances(), eq i);
245+
assert_that!(state.number_of_living_instances(), eq i);
246246
}
247247
}
248248

249249
#[test]
250250
fn queue_clear_drops_all_objects() {
251-
LifetimeTracker::start_tracking();
251+
let state = LifetimeTracker::start_tracking();
252252
let mut sut = Queue::<LifetimeTracker>::new(SUT_CAPACITY);
253253

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

258258
sut.clear();
259-
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
259+
assert_that!(state.number_of_living_instances(), eq 0);
260260
}
261261

262262
#[test]
263263
fn fixed_size_queue_clear_drops_all_objects() {
264-
LifetimeTracker::start_tracking();
264+
let state = LifetimeTracker::start_tracking();
265265
let mut sut = FixedSizeQueue::<LifetimeTracker, SUT_CAPACITY>::new();
266266

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

271271
sut.clear();
272-
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
272+
assert_that!(state.number_of_living_instances(), eq 0);
273273
}
274274

275275
#[test]

iceoryx2-bb/container/tests/vec_tests.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,35 +205,35 @@ mod fixed_size_vec {
205205

206206
#[test]
207207
fn drops_all_objects_when_out_of_scope() {
208-
LifetimeTracker::start_tracking();
208+
let state = LifetimeTracker::start_tracking();
209209
let mut sut = FixedSizeVec::<LifetimeTracker, SUT_CAPACITY>::new();
210210

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

215-
assert_that!(LifetimeTracker::number_of_living_instances(), eq SUT_CAPACITY);
215+
assert_that!(state.number_of_living_instances(), eq SUT_CAPACITY);
216216
drop(sut);
217-
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
217+
assert_that!(state.number_of_living_instances(), eq 0);
218218
}
219219

220220
#[test]
221221
fn drops_all_objects_with_clear() {
222-
LifetimeTracker::start_tracking();
222+
let state = LifetimeTracker::start_tracking();
223223
let mut sut = FixedSizeVec::<LifetimeTracker, SUT_CAPACITY>::new();
224224

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

229-
assert_that!(LifetimeTracker::number_of_living_instances(), eq SUT_CAPACITY);
229+
assert_that!(state.number_of_living_instances(), eq SUT_CAPACITY);
230230
sut.clear();
231-
assert_that!(LifetimeTracker::number_of_living_instances(), eq 0);
231+
assert_that!(state.number_of_living_instances(), eq 0);
232232
}
233233

234234
#[test]
235235
fn pop_releases_ownership() {
236-
LifetimeTracker::start_tracking();
236+
let state = LifetimeTracker::start_tracking();
237237
let mut sut = FixedSizeVec::<LifetimeTracker, SUT_CAPACITY>::new();
238238

239239
for _ in 0..SUT_CAPACITY {
@@ -244,7 +244,7 @@ mod fixed_size_vec {
244244
let result = sut.pop();
245245
assert_that!(result, is_some);
246246
drop(result);
247-
assert_that!(LifetimeTracker::number_of_living_instances(), eq i);
247+
assert_that!(state.number_of_living_instances(), eq i);
248248
}
249249
}
250250

iceoryx2-bb/derive-macros/BUILD.bazel

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

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

15-
load("@rules_rust//rust:defs.bzl", "rust_proc_macro")
15+
load("@rules_rust//rust:defs.bzl", "rust_proc_macro", "rust_test_suite")
1616

1717
filegroup(
1818
name = "all_srcs",
@@ -30,4 +30,15 @@ rust_proc_macro(
3030
],
3131
)
3232

33-
# TODO: [349] add tests
33+
rust_test_suite(
34+
name = "iceoryx2-bb-derive-macros-tests",
35+
srcs = glob(["tests/**/*.rs"]),
36+
deps = [
37+
"//iceoryx2-bb/elementary:iceoryx2-bb-elementary",
38+
"//iceoryx2-bb/testing:iceoryx2-bb-testing",
39+
],
40+
proc_macro_deps = [
41+
":iceoryx2-bb-derive-macros",
42+
"@crate_index//:generic-tests",
43+
],
44+
)

iceoryx2-bb/elementary/BUILD.bazel

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

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

15-
load("@rules_rust//rust:defs.bzl", "rust_library")
15+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test_suite")
1616

1717
filegroup(
1818
name = "all_srcs",
@@ -27,4 +27,14 @@ rust_library(
2727
],
2828
)
2929

30-
# TODO: [349] add tests
30+
rust_test_suite(
31+
name = "iceoryx2-bb-elementary-tests",
32+
srcs = glob(["tests/**/*.rs"]),
33+
deps = [
34+
":iceoryx2-bb-elementary",
35+
"//iceoryx2-bb/testing:iceoryx2-bb-testing",
36+
],
37+
proc_macro_deps = [
38+
"@crate_index//:generic-tests",
39+
],
40+
)

iceoryx2-bb/elementary/tests/package_version_tests.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111
// SPDX-License-Identifier: Apache-2.0 OR MIT
1212

1313
use iceoryx2_bb_elementary::package_version::PackageVersion;
14-
use iceoryx2_bb_testing::assert_that;
14+
use iceoryx2_bb_testing::{assert_that, test_requires};
1515

1616
#[test]
1717
fn package_version_works() {
18+
// NOTE: The test is skipped when not run with cargo but with bazel
19+
// The CI which runs with cargo ensures that the constants defined
20+
// in PackageVersion::get equal the package version.
21+
test_requires!(option_env!("CARGO").is_some());
22+
1823
let major = option_env!("CARGO_PKG_VERSION_MAJOR")
1924
.and_then(|s| s.parse::<u16>().ok())
2025
.expect("Contains a valid major version number.");

iceoryx2-bb/lock-free/BUILD.bazel

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

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

15-
load("@rules_rust//rust:defs.bzl", "rust_library")
15+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test_suite")
1616

1717
filegroup(
1818
name = "all_srcs",
@@ -30,4 +30,16 @@ rust_library(
3030
],
3131
)
3232

33-
# TODO: [349] add tests
33+
rust_test_suite(
34+
name = "iceoryx2-bb-lock-free-tests",
35+
srcs = glob(["tests/**/*.rs"]),
36+
deps = [
37+
":iceoryx2-bb-lock-free",
38+
"//iceoryx2-bb/elementary:iceoryx2-bb-elementary",
39+
"//iceoryx2-bb/posix:iceoryx2-bb-posix",
40+
"//iceoryx2-bb/testing:iceoryx2-bb-testing",
41+
],
42+
proc_macro_deps = [
43+
"@crate_index//:generic-tests",
44+
],
45+
)

iceoryx2-bb/memory/BUILD.bazel

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

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

15-
load("@rules_rust//rust:defs.bzl", "rust_library")
15+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test_suite")
1616

1717
filegroup(
1818
name = "all_srcs",
@@ -33,4 +33,15 @@ rust_library(
3333
],
3434
)
3535

36-
# TODO: [349] add tests
36+
rust_test_suite(
37+
name = "iceoryx2-bb-memory-tests",
38+
srcs = glob(["tests/**/*.rs"]),
39+
deps = [
40+
"iceoryx2-bb-memory",
41+
"//iceoryx2-bb/elementary:iceoryx2-bb-elementary",
42+
"//iceoryx2-bb/testing:iceoryx2-bb-testing",
43+
],
44+
proc_macro_deps = [
45+
"@crate_index//:generic-tests",
46+
],
47+
)

0 commit comments

Comments
 (0)