Coverage Report

Created: 2026-12-01 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/behaviortreecpp/src/actions/test_node.cpp
Line
Count
Source
1
#include "behaviortree_cpp/actions/test_node.h"
2
3
#include "behaviortree_cpp/scripting/script_parser.hpp"
4
#include "behaviortree_cpp/utils/timer_queue.h"
5
6
#include <atomic>
7
8
namespace BT
9
{
10
11
namespace
12
{
13
14
bool isAllowedTestNodeStatus(NodeStatus status)
15
4.65k
{
16
4.65k
  switch(status)
17
4.65k
  {
18
0
    case NodeStatus::RUNNING:
19
3.10k
    case NodeStatus::SUCCESS:
20
4.65k
    case NodeStatus::FAILURE:
21
4.65k
    case NodeStatus::SKIPPED:
22
4.65k
      return true;
23
24
0
    case NodeStatus::IDLE:
25
0
      return false;
26
4.65k
  }
27
0
  return false;
28
4.65k
}
29
30
void validateTestNodeStatus(NodeStatus status, StringView source)
31
4.65k
{
32
4.65k
  if(!isAllowedTestNodeStatus(status))
33
0
  {
34
0
    throw RuntimeError("TestNode ", std::string(source),
35
0
                       " resolved to invalid status (integer value: ",
36
0
                       std::to_string(static_cast<int>(status)), ")");
37
0
  }
38
4.65k
}
39
40
EnumsTablePtr createTestNodeEnums(const EnumsTablePtr& base_enums)
41
1.67k
{
42
1.67k
  auto enums = base_enums ? std::make_shared<EnumsTable>(*base_enums) :
43
1.67k
                            std::make_shared<EnumsTable>();
44
45
1.67k
  (*enums)["IDLE"] = static_cast<int>(NodeStatus::IDLE);
46
1.67k
  (*enums)["RUNNING"] = static_cast<int>(NodeStatus::RUNNING);
47
1.67k
  (*enums)["SUCCESS"] = static_cast<int>(NodeStatus::SUCCESS);
48
1.67k
  (*enums)["FAILURE"] = static_cast<int>(NodeStatus::FAILURE);
49
1.67k
  (*enums)["SKIPPED"] = static_cast<int>(NodeStatus::SKIPPED);
50
1.67k
  return enums;
51
1.67k
}
52
53
NodeStatus convertScriptResultToStatus(const Any& result)
54
1.67k
{
55
1.67k
  if(result.empty())
56
0
  {
57
0
    throw RuntimeError("return_status_script returned an empty value");
58
0
  }
59
60
1.67k
  if(result.isString())
61
0
  {
62
0
    auto status = convertFromString<NodeStatus>(result.cast<std::string>());
63
0
    validateTestNodeStatus(status, "return_status_script");
64
0
    return status;
65
0
  }
66
67
1.67k
  if(auto status = result.tryCast<int>())
68
1.67k
  {
69
1.67k
    auto resolved = static_cast<NodeStatus>(status.value());
70
1.67k
    validateTestNodeStatus(resolved, "return_status_script");
71
1.67k
    return resolved;
72
1.67k
  }
73
74
0
  throw RuntimeError("return_status_script must evaluate to a NodeStatus-compatible "
75
0
                     "value");
76
1.67k
}
77
78
}  // namespace
79
80
struct TestNode::PImpl
81
{
82
  std::shared_ptr<TestNodeConfig> config;
83
  ScriptFunction return_status_executor;
84
  ScriptFunction success_executor;
85
  ScriptFunction failure_executor;
86
  ScriptFunction post_executor;
87
  std::atomic_bool completed = false;
88
  // Keep last: ~TimerQueue() joins the worker thread, so this must be destroyed
89
  // before completed, which the timer handler touches.
90
  TimerQueue<> timer;
91
};
92
93
TestNode::TestNode(const std::string& name, const NodeConfig& config,
94
                   TestNodeConfig test_config)
95
0
  : TestNode(name, config, std::make_shared<TestNodeConfig>(std::move(test_config)))
96
0
{}
97
98
TestNode::TestNode(const std::string& name, const NodeConfig& config,
99
                   std::shared_ptr<TestNodeConfig> test_config)
100
1.33k
  : StatefulActionNode(name, config), _p(std::make_unique<PImpl>())
101
1.33k
{
102
1.33k
  _p->config = std::move(test_config);
103
1.33k
  setRegistrationID("TestNode");
104
105
1.33k
  if(_p->config->return_status_script.empty())
106
766
  {
107
766
    validateTestNodeStatus(_p->config->return_status, "return_status");
108
766
  }
109
110
5.34k
  auto prepareScript = [](const std::string& script, auto& executor) {
111
5.34k
    if(!script.empty())
112
1.23k
    {
113
1.23k
      auto result = ParseScript(script);
114
1.23k
      if(!result)
115
0
      {
116
0
        throw RuntimeError(result.error());
117
0
      }
118
1.23k
      executor = result.value();
119
1.23k
    }
120
5.34k
  };
121
1.33k
  prepareScript(_p->config->return_status_script, _p->return_status_executor);
122
1.33k
  prepareScript(_p->config->success_script, _p->success_executor);
123
1.33k
  prepareScript(_p->config->failure_script, _p->failure_executor);
124
1.33k
  prepareScript(_p->config->post_script, _p->post_executor);
125
1.33k
}
126
127
1.33k
TestNode::~TestNode() = default;
128
129
NodeStatus TestNode::onStart()
130
4.78k
{
131
4.78k
  if(_p->config->async_delay <= std::chrono::milliseconds(0))
132
2.21k
  {
133
2.21k
    return onCompleted();
134
2.21k
  }
135
  // convert this in an asynchronous operation. Use another thread to count
136
  // a certain amount of time.
137
2.57k
  _p->completed = false;
138
  // The handler must not read _p: ~TimerQueue() runs it (aborted) while the
139
  // PImpl is being deleted, and std::unique_ptr may already hold nullptr then.
140
2.57k
  _p->timer.add(std::chrono::milliseconds(_p->config->async_delay),
141
2.57k
                [this, p = _p.get()](bool aborted) {
142
2.57k
                  if(!aborted)
143
6
                  {
144
6
                    p->completed.store(true);
145
6
                    this->emitWakeUpSignal();
146
6
                  }
147
2.56k
                  else
148
2.56k
                  {
149
2.56k
                    p->completed.store(false);
150
2.56k
                  }
151
2.57k
                });
152
2.57k
  return NodeStatus::RUNNING;
153
4.78k
}
154
155
NodeStatus TestNode::onRunning()
156
1.24k
{
157
1.24k
  if(_p->completed)
158
6
  {
159
6
    return onCompleted();
160
6
  }
161
1.24k
  return NodeStatus::RUNNING;
162
1.24k
}
163
164
void TestNode::onHalted()
165
2.56k
{
166
2.56k
  _p->timer.cancelAll();
167
2.56k
}
168
169
NodeStatus TestNode::onCompleted()
170
2.21k
{
171
2.21k
  NodeStatus status = NodeStatus::IDLE;
172
173
2.21k
  if(_p->config->complete_func)
174
0
  {
175
0
    status = _p->config->complete_func();
176
0
  }
177
2.21k
  else if(_p->return_status_executor)
178
1.67k
  {
179
    // return_status_script may reference NodeStatus names (e.g. SUCCESS, FAILURE),
180
    // so it is evaluated with an enum table augmented with those values. The
181
    // post-execution scripts below deliberately do NOT see these names.
182
1.67k
    auto status_enums = createTestNodeEnums(config().enums);
183
1.67k
    Ast::Environment status_env = { config().blackboard, status_enums };
184
1.67k
    status = convertScriptResultToStatus(_p->return_status_executor(status_env));
185
1.67k
  }
186
542
  else
187
542
  {
188
542
    status = _p->config->return_status;
189
542
  }
190
191
2.21k
  validateTestNodeStatus(status, "completion");
192
193
  // success/failure/post scripts use only the node's own enums, so that a
194
  // blackboard entry named like a status (e.g. "SUCCESS") is not shadowed.
195
2.21k
  Ast::Environment env = { config().blackboard, config().enums };
196
197
2.21k
  if(status == NodeStatus::SUCCESS && _p->success_executor)
198
0
  {
199
0
    _p->success_executor(env);
200
0
  }
201
2.21k
  else if(status == NodeStatus::FAILURE && _p->failure_executor)
202
0
  {
203
0
    _p->failure_executor(env);
204
0
  }
205
2.21k
  if(_p->post_executor)
206
6
  {
207
6
    _p->post_executor(env);
208
6
  }
209
2.21k
  return status;
210
2.21k
}
211
212
}  // namespace BT