Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests] test cancelling requests from CLI #137

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
17 changes: 16 additions & 1 deletion src/app/border_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "border_agent.hpp"

#include <atomic>
#include <chrono>
#include <thread>

Expand All @@ -42,6 +43,8 @@ namespace ot {

namespace commissioner {

static std::atomic<bool> gIsDiscoverCancelled;

struct BorderAgentOrErrorMsg
{
BorderAgent mBorderAgent;
Expand All @@ -59,6 +62,11 @@ static int HandleRecord(const struct sockaddr *from,
size_t length,
void * user_data);

void CancelDiscoverBorderAgent()
{
gIsDiscoverCancelled = true;
}

Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout)
{
static constexpr size_t kDefaultBufferSize = 1024 * 16;
Expand All @@ -67,18 +75,21 @@ Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeou

Error error;
uint8_t buf[kDefaultBufferSize];
size_t borderAgentCount = 0;

auto begin = std::chrono::system_clock::now();

int socket = mdns_socket_open_ipv4();
VerifyOrExit(socket >= 0, error = ERROR_IO_ERROR("failed to open mDNS IPv4 socket"));

gIsDiscoverCancelled = false;

if (mdns_query_send(socket, kMdnsQueryType, kServiceName, strlen(kServiceName), buf, sizeof(buf)) != 0)
{
ExitNow(error = ERROR_IO_ERROR("failed to send mDNS query"));
}

while (begin + std::chrono::milliseconds(aTimeout) >= std::chrono::system_clock::now())
while (!gIsDiscoverCancelled && begin + std::chrono::milliseconds(aTimeout) >= std::chrono::system_clock::now())
{
BorderAgentOrErrorMsg curBorderAgentOrErrorMsg;

Expand All @@ -91,11 +102,15 @@ Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeou
else if (curBorderAgentOrErrorMsg.mBorderAgent.mPresentFlags != 0)
{
aBorderAgentHandler(&curBorderAgentOrErrorMsg.mBorderAgent, ERROR_NONE);
++borderAgentCount;
}

std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

VerifyOrExit(!gIsDiscoverCancelled, error = ERROR_CANCELLED("Border Agent discovery was cancelled"));
VerifyOrExit(borderAgentCount != 0, error = ERROR_TIMEOUT("Found no Border Agent"));

exit:
if (socket >= 0)
{
Expand Down
8 changes: 8 additions & 0 deletions src/app/border_agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ using BorderAgentHandler = std::function<void(const BorderAgent *aBorderAgent, c
*/
Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout);

/**
* Cancel the call to DiscoverBorderAgent.
*
* This function is thread-safe.
*
*/
void CancelDiscoverBorderAgent();

} // namespace commissioner

} // namespace ot
Expand Down
2 changes: 2 additions & 0 deletions src/app/cli/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ void Interpreter::CancelCommand()
{
mCommissioner->Stop();
}

CancelDiscoverBorderAgent();
}

Interpreter::Expression Interpreter::Read()
Expand Down
13 changes: 11 additions & 2 deletions tests/integration/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,26 @@ stop_commissioner() {
}

## Send command to commissioner.
## Args: $1: the command.
## Args:
## $1: the command.
## $2: the expected error (optional). The command
## is expected to success if this argument is
## not specified.
send_command_to_commissioner() {
set -e
local command=$1
local expect_error=$2
local result
result=$(${COMMISSIONER_CTL} execute "${command}")

echo "${result}"
[ -n "${result}" ] || return 1

echo "${result}" | grep -q "\[done\]" || return 1
if [ -z "${expect_error}" ]; then
echo "${result}" | grep -q "\[done\]" || return 1
else
echo "${result}" | grep -q "${expect_error}" || return 1
fi
}

## Start a joiner
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/test_cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,21 @@ test_get_commissioner_dataset()

stop_daemon
}

test_cancel_command() {
set -e

start_commissioner "${NON_CCM_CONFIG}"

## Make sure that the mDNS service has been stopped.
stop_border_agent_mdns_service

## Expecting error CANCELLED after sending SIGINT
send_command_to_commissioner "borderagent discover" "CANCELLED" &
sleep 1

## send SIGINT to the commissioner CLI to cancel outstanding requests ('borderagent discover' in this case).
pkill --signal SIGINT "${COMMISSIONER_CLI}"

stop_commissioner
}