Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions score/mw/com/test/move_semantics/skeleton_method/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# *******************************************************************************
# Copyright (c) 2026 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Apache License Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0
#
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************

load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
load("@score_baselibs//score/language/safecpp:toolchain_features.bzl", "COMPILER_WARNING_FEATURES")
load("//bazel/tools:json_schema_validator.bzl", "validate_json_schema_test")
load("//score/mw/com/test:pkg_application.bzl", "pkg_application")

validate_json_schema_test(
name = "validate_config_schema",
json = "config/mw_com_config.json",
schema = "//score/mw/com:config_schema",
tags = ["lint"],
)

cc_library(
name = "test_method_datatype",
srcs = ["test_method_datatype.cpp"],
hdrs = ["test_method_datatype.h"],
features = COMPILER_WARNING_FEATURES,
deps = [
"//score/mw/com",
],
)

cc_library(
name = "test_parameters",
srcs = ["test_parameters.cpp"],
hdrs = ["test_parameters.h"],
features = COMPILER_WARNING_FEATURES,
deps = [
"//score/mw/com",
"//score/mw/com/test/common_test_resources:command_line_parser",
"//score/mw/com/test/common_test_resources:fail_test",
],
)

cc_library(
name = "provider",
srcs = ["provider.cpp"],
hdrs = ["provider.h"],
features = COMPILER_WARNING_FEATURES,
deps = [
":test_method_datatype",
":test_parameters",
"//score/mw/com",
"//score/mw/com/test/common_test_resources:fail_test",
"//score/mw/com/test/common_test_resources:skeleton_container",
"@score_baselibs//score/concurrency:notification",
"@score_baselibs//score/language/futurecpp",
],
)

cc_library(
name = "consumer",
srcs = ["consumer.cpp"],
hdrs = ["consumer.h"],
features = COMPILER_WARNING_FEATURES,
deps = [
":test_method_datatype",
":test_parameters",
"//score/mw/com",
"//score/mw/com/test/common_test_resources:fail_test",
"//score/mw/com/test/common_test_resources:proxy_container",
"@score_baselibs//score/language/futurecpp",
],
)

cc_binary(
name = "main_provider",
srcs = ["main_provider.cpp"],
data = ["config/mw_com_config.json"],
features = COMPILER_WARNING_FEATURES + [
"aborts_upon_exception",
],
deps = [
":provider",
":test_parameters",
"//score/mw/com",
"//score/mw/com/test/common_test_resources:assert_handler",
"//score/mw/com/test/common_test_resources:fail_test",
"//score/mw/com/test/common_test_resources:stop_token_sig_term_handler",
],
)

cc_binary(
name = "main_consumer",
srcs = ["main_consumer.cpp"],
data = ["config/mw_com_config.json"],
features = COMPILER_WARNING_FEATURES + [
"aborts_upon_exception",
],
deps = [
":consumer",
":test_parameters",
"//score/mw/com",
"//score/mw/com/test/common_test_resources:assert_handler",
"//score/mw/com/test/common_test_resources:fail_test",
"//score/mw/com/test/common_test_resources:stop_token_sig_term_handler",
],
)

cc_binary(
name = "main_consumer_and_provider",
srcs = ["main_consumer_and_provider.cpp"],
data = ["config/mw_com_config.json"],
features = COMPILER_WARNING_FEATURES + [
"aborts_upon_exception",
],
deps = [
":consumer",
":provider",
":test_parameters",
"//score/mw/com",
"//score/mw/com/test/common_test_resources:assert_handler",
"//score/mw/com/test/common_test_resources:fail_test",
"//score/mw/com/test/common_test_resources:stop_token_sig_term_handler",
],
)

pkg_application(
name = "main_provider-pkg",
app_name = "MainProviderApp",
bin = [":main_provider"],
etc = [
"config/mw_com_config.json",
"config/logging.json",
],
visibility = [
"//score/mw/com/test/move_semantics/skeleton_method:__subpackages__",
],
)

pkg_application(
name = "main_consumer-pkg",
app_name = "MainConsumerApp",
bin = [":main_consumer"],
etc = [
"config/mw_com_config.json",
"config/logging.json",
],
visibility = [
"//score/mw/com/test/move_semantics/skeleton_method:__subpackages__",
],
)

pkg_application(
name = "main_consumer_and_provider-pkg",
app_name = "MainConsumerAndProviderApp",
bin = [":main_consumer_and_provider"],
etc = [
"config/mw_com_config.json",
"config/logging.json",
],
visibility = [
"//score/mw/com/test/move_semantics/skeleton_method:__subpackages__",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"appId": "SMMS",
"appDesc": "skeleton_method_move_semantics",
"logLevel": "kDebug",
"logLevelThresholdConsole": "kDebug",
"logMode": "kRemote|kConsole"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"serviceTypes": [
{
"serviceTypeName": "/test/move_semantics/skeleton_method/SkeletonMethodMoveInterface",
"version": {
"major": 1,
"minor": 0
},
"bindings": [
{
"binding": "SHM",
"serviceId": 2200,
"methods": [
{
"methodName": "moved_method",
"methodId": 1
}
]
}
]
}
],
"serviceInstances": [
{
"instanceSpecifier": "test/move_semantics/skeleton_method/SkeletonMethodMoveInterfaceMovedTo",
"serviceTypeName": "/test/move_semantics/skeleton_method/SkeletonMethodMoveInterface",
"version": {
"major": 1,
"minor": 0
},
"instances": [
{
"instanceId": 1,
"asil-level": "B",
"binding": "SHM",
"methods": [
{
"methodName": "moved_method",
"queueSize": 1
}
],
"allowedConsumer": {
"QM": [
0
],
"B": [
0
]
},
"allowedProvider": {
"QM": [
0
],
"B": [
0
]
}
}
]
},
{
"instanceSpecifier": "test/move_semantics/skeleton_method/SkeletonMethodMoveInterfaceMovedFrom",
"serviceTypeName": "/test/move_semantics/skeleton_method/SkeletonMethodMoveInterface",
"version": {
"major": 1,
"minor": 0
},
"instances": [
{
"instanceId": 2,
"asil-level": "B",
"binding": "SHM",
"methods": [
{
"methodName": "moved_method",
"queueSize": 1
}
],
"allowedConsumer": {
"QM": [
0
],
"B": [
0
]
},
"allowedProvider": {
"QM": [
0
],
"B": [
0
]
}
}
]
}
],
"global": {
"asil-level": "B"
}
}
95 changes: 95 additions & 0 deletions score/mw/com/test/move_semantics/skeleton_method/consumer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/********************************************************************************
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#include "score/mw/com/test/move_semantics/skeleton_method/consumer.h"

#include "score/mw/com/test/common_test_resources/fail_test.h"
#include "score/mw/com/test/common_test_resources/proxy_container.h"
#include "score/mw/com/test/move_semantics/skeleton_method/test_method_datatype.h"
#include "score/mw/com/test/move_semantics/skeleton_method/test_parameters.h"

#include <score/stop_token.hpp>

#include <chrono>
#include <iostream>
#include <thread>

namespace score::mw::com::test
{
namespace
{

constexpr auto kDetectionWindow = std::chrono::milliseconds{5000};

constexpr auto kDetectionCallGap = std::chrono::milliseconds{5};

std::int32_t CallMethodOrFail(SkeletonMethodMoveProxy& proxy)
{
auto result = proxy.moved_method_(kTestArgA, kTestArgB);
if (!result.has_value())
{
FailTest("Consumer: moved_method_ call failed: ", result.error());
}
return *(result.value());
}

void VerifyResultIsOneOf(std::int32_t actual,
std::int32_t first_expected,
std::int32_t second_expected,
std::size_t call_index)
{
if (actual != first_expected && actual != second_expected)
{
FailTest(
"Consumer: call ", call_index, " expected ", first_expected, " or ", second_expected, " but got ", actual);
}
}

} // namespace

void RunConsumer(const SkeletonMoveScenario& scenario, const score::cpp::stop_token& stop_token)
{
static_cast<void>(stop_token);

// Step 1. Create proxy for kInstanceSpecifierMovedTo.
std::cout << "\nConsumer: Step 1 - Create proxy (kMovedTo)" << std::endl;
ProxyContainer<SkeletonMethodMoveProxy> proxy_moved_to_container{};
proxy_moved_to_container.CreateProxy(kInstanceSpecifierMovedTo, "skeleton_method_move_semantics");
auto& proxy_moved_to = proxy_moved_to_container.GetProxy();

// Step 2. Keep calling the method until the return value identifies the second handler. For
// "before offered" scenarios the first call is always Handler A. For "after offered"
// (fuzzy) scenarios the move may already have happened, so any call may return either
// handler's result — each call is just checked against the two known-valid results.
std::cout << "\nConsumer: Step 2 - Loop calling until second handler is detected" << std::endl;
const std::int32_t first_expected = GetFirstHandlerExpectedResult(scenario);
const std::int32_t second_expected = GetSecondHandlerExpectedResult(scenario);
const auto deadline = std::chrono::steady_clock::now() + kDetectionWindow;
std::size_t call_count = 0U;
std::int32_t actual = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not use 0 to represent an uninitialized value. We should instead use std::optionalstd::int32_t. You can then also change the do while loop to a while loop with the condition being while (!actual.has_value() || actual.value() != second_expected)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment below, it would also simplify the condition.

do
{
if (std::chrono::steady_clock::now() >= deadline)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This timeout is handled by the test infrastructure. I don't think we should also do a timeout check within the test itself (since it's redundant and also complicates the test sequence).

{
FailTest("Consumer: timed out waiting to observe the second handler's result");
}
actual = CallMethodOrFail(proxy_moved_to);
VerifyResultIsOneOf(actual, first_expected, second_expected, call_count);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this function makes the test logic harder to understand. I would rather something like this to make the sequence clearer:

// If the first handler is still registered, then we continue calling the method
if (actual == first_expected)
{
    // sleep
    continue;
}

// If the second handler has been registered, then we can finish
if (actual == second_expected)
{
    break;
}

FailTest(true, ...);

What do you think?

++call_count;
std::this_thread::sleep_for(kDetectionCallGap);
} while (actual != second_expected);

std::cout << "\nConsumer: Step 2 done (" << call_count << " calls, second handler result=" << actual << ")"
<< std::endl;
}

} // namespace score::mw::com::test
Loading
Loading