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
1 change: 1 addition & 0 deletions catkit2/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ PYBIND11_MODULE(catkit_bindings, m)
.def("interrupt_service", &TestbedProxy::InterruptService)
.def("terminate_service", &TestbedProxy::TerminateService)
.def("shut_down", &TestbedProxy::ShutDown)
.def("reload_config", &TestbedProxy::ReloadConfig)
.def_property_readonly("message_broker", &TestbedProxy::GetMessageBroker)
.def_property_readonly("is_simulated", &TestbedProxy::IsSimulated)
.def_property_readonly("is_alive", &TestbedProxy::IsAlive)
Expand Down
320 changes: 281 additions & 39 deletions catkit2/testbed/testbed.py

Large diffs are not rendered by default.

29 changes: 27 additions & 2 deletions catkit_core/TestbedProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,30 @@ void TestbedProxy::ShutDown()
}
}

void TestbedProxy::ReloadConfig(const json &new_config)
{
catkit_proto::testbed::ReloadConfigRequest request;
request.set_config(new_config.dump());

catkit_proto::testbed::ReloadConfigReply reply;

try
{
reply.ParseFromString(MakeRequest("reload_config", Serialize(request)));
}
catch (...)
{
throw std::runtime_error("Unable to reload config.");
}

// Invalidate this proxy's cached config so it is refetched from the server on the
// next access. Note: this only affects *this* TestbedProxy instance. Other
// TestbedProxy objects (in this or other processes) keep their cached config until
// they are recreated, which matches the feature's "restart the service to apply"
// semantics. Broadcasting the invalidation to all live proxies would be a follow-up.
m_HasGottenInfo = false;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This variable is just for this instance of a TestbedProxy. It won't affect other TestbedProxy objects. I think you're aware of this, but I wanted to make sure.

}

std::shared_ptr<DataStream> TestbedProxy::GetHeartbeat()
{
GetTestbedInfo();
Expand Down Expand Up @@ -366,8 +390,9 @@ std::string TestbedProxy::GetLongTermMonitoringPath()

void TestbedProxy::GetTestbedInfo()
{
// Do not communicate with the server unnecessarily.
// The server info will not change over its lifetime.
// Do not communicate with the server unnecessarily. The server info is treated as
// stable over the proxy's lifetime; the one exception is ReloadConfig(), which clears
// m_HasGottenInfo on this instance so the next access refetches the updated config.
if (m_HasGottenInfo)
return;

Expand Down
1 change: 1 addition & 0 deletions catkit_core/TestbedProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class TestbedProxy : public Client, public std::enable_shared_from_this<TestbedP
bool IsAlive();

void ShutDown();
void ReloadConfig(const nlohmann::json &new_config);

std::shared_ptr<DataStream> GetHeartbeat();
std::shared_ptr<MessageBroker> GetMessageBroker();
Expand Down
28 changes: 28 additions & 0 deletions proto/service.proto

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This file might've been committed accidentally?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

oops! thanks for catching that

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@ import "core.proto";

package catkit_proto.service;

message GetInfoRequest
{
}

message GetInfoReply
{
string service_id = 1;
string service_type = 2;
string config = 3;

repeated string property_names = 4;
repeated string command_names = 5;
map<string, string> datastream_ids = 6;

string heartbeat_stream_id = 7;
}

message ExecuteCommandRequest
{
string command_name = 1;
Dict arguments = 2;
}

message ExecuteCommandReply
{
Value result = 1;
}

message ShutDownRequest
{
}
Expand Down
9 changes: 9 additions & 0 deletions proto/testbed.proto
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,12 @@ message ShutDownRequest
message ShutDownReply
{
}

message ReloadConfigRequest
{
string config = 1;
}

message ReloadConfigReply
{
}
105 changes: 105 additions & 0 deletions tests/test_testbed_dependency_graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import logging

import pytest

from catkit2.testbed.testbed import Testbed


class DummyTestbed:
'''A stand-in for a Testbed instance to call _compute_reverse_dependencies() on.'''
log = logging.getLogger(__name__)


def compute(nodes, safety_service_id=None):
return Testbed._compute_reverse_dependencies(DummyTestbed(), nodes, safety_service_id)


def test_simple_graph():
nodes = {
'camera': ([], False),
'controller': (['camera'], False),
'script': (['controller', 'camera'], False)
}

graph = compute(nodes)

assert graph == {
'camera': ['controller', 'script'],
'controller': ['script'],
'script': []
}


def test_safety_edges():
nodes = {
'safety': ([], False),
'dm': ([], True),
'camera': ([], False)
}

graph = compute(nodes, safety_service_id='safety')

assert graph['safety'] == ['dm']
assert graph['dm'] == []
assert graph['camera'] == []


def test_unknown_dependency_is_ignored():
nodes = {
'camera': (['nonexistent'], False)
}

graph = compute(nodes)

assert graph == {'camera': []}


def test_requires_safety_without_safety_service_raises():
nodes = {
'dm': ([], True)
}

with pytest.raises(RuntimeError, match='requires safety'):
compute(nodes, safety_service_id=None)


def test_safety_service_not_in_services_raises():
nodes = {
'dm': ([], True)
}

with pytest.raises(RuntimeError, match='safety service'):
compute(nodes, safety_service_id='safety')


def test_circular_dependencies_raise():
nodes = {
'a': (['b'], False),
'b': (['a'], False)
}

with pytest.raises(RuntimeError, match='[Cc]ircular'):
compute(nodes)


def test_self_dependency_raises():
nodes = {
'a': (['a'], False)
}

with pytest.raises(RuntimeError, match='[Cc]ircular'):
compute(nodes)


def test_graph_is_independent_of_state():
# _compute_reverse_dependencies() must not modify its inputs.
dependencies = ['camera']
nodes = {
'camera': ([], False),
'controller': (dependencies, False)
}

compute(nodes)

assert dependencies == ['camera']
assert set(nodes.keys()) == {'camera', 'controller'}
Loading