Skip to content

Commit 75c73e4

Browse files
committed
fix tests and mocks, definitions in discovery server, template in resource config
1 parent e123c05 commit 75c73e4

File tree

10 files changed

+69
-53
lines changed

10 files changed

+69
-53
lines changed

bin/run-clang-format.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
# Check if clang-format is installed
18+
if ! command -v clang-format >/dev/null 2>&1; then
19+
echo "Error: clang-format is not installed"
20+
echo "Please install it using: brew install clang-format"
21+
exit 1
22+
fi
23+
1724
find ./src -not -path "./src/viam/api" -type f \( -name \*.cpp -o -name \*.hpp \) | xargs clang-format -style=file -i -fallback-style=none "$@"

src/viam/sdk/config/resource.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <viam/api/app/v1/robot.pb.h>
1010
#include <viam/api/robot/v1/robot.pb.h>
11+
#include <viam/api/service/discovery/v1/discovery.pb.h>
1112

1213
#include <viam/sdk/common/exception.hpp>
1314
#include <viam/sdk/common/private/repeated_ptr_convert.hpp>
@@ -156,6 +157,12 @@ ResourceConfig from_proto_impl<app::v1::ComponentConfig>::operator()(
156157
proto->has_frame() ? from_proto(proto->frame()) : LinkConfig{});
157158
}
158159

160+
std::vector<ResourceConfig>
161+
from_proto_impl<service::discovery::v1::DiscoverResourcesResponse>::operator()(
162+
const service::discovery::v1::DiscoverResourcesResponse* proto) const {
163+
return impl::from_repeated_field(proto->discoveries());
164+
}
165+
159166
} // namespace proto_convert_details
160167
} // namespace sdk
161168
} // namespace viam

src/viam/sdk/config/resource.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ class ResourceLevelServiceConfig;
1717

1818
} // namespace v1
1919
} // namespace app
20+
21+
namespace service {
22+
namespace discovery {
23+
namespace v1 {
24+
25+
class DiscoverResourcesResponse;
26+
}
27+
} // namespace discovery
28+
} // namespace service
2029
} // namespace viam
2130

2231
namespace viam {
@@ -83,6 +92,12 @@ struct from_proto_impl<app::v1::ComponentConfig> {
8392
ResourceConfig operator()(const app::v1::ComponentConfig*) const;
8493
};
8594

95+
template <>
96+
struct from_proto_impl<service::discovery::v1::DiscoverResourcesResponse> {
97+
std::vector<ResourceConfig> operator()(
98+
const service::discovery::v1::DiscoverResourcesResponse*) const;
99+
};
100+
86101
} // namespace proto_convert_details
87102

88103
} // namespace sdk

src/viam/sdk/services/private/discovery_server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ::grpc::Status DiscoveryServer::DiscoverResources(
2121
const std::vector<ResourceConfig> resources =
2222
discovery->discover_resources(helper.getExtra());
2323
for (const auto& resource : resources) {
24-
*response->mutable_resources()->Add() = to_proto(resource);
24+
*response->mutable_discoveries()->Add() = to_proto(resource);
2525
}
2626
});
2727
}
@@ -31,7 +31,7 @@ ::grpc::Status DiscoveryServer::DoCommand(
3131
const ::viam::common::v1::DoCommandRequest* request,
3232
::viam::common::v1::DoCommandResponse* response) noexcept {
3333
return make_service_helper<Discovery>(
34-
"DiscoveryServer::DoCommand", this, request)([&](auto& helper, auto& discovery) {
34+
"DiscoveryServer::DoCommand", this, request)([&](auto&, auto& discovery) {
3535
const ProtoStruct result = discovery->do_command(from_proto(request->command()));
3636
*response->mutable_result() = to_proto(result);
3737
});

src/viam/sdk/services/private/discovery_server.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class DiscoveryServer : public ResourceServer,
2323
public:
2424
using interface_type = Discovery;
2525
using service_type = service::discovery::v1::DiscoveryService;
26-
27-
explicit DiscoveryServer(std::shared_ptr<ResourceManager> manager);
26+
explicit DiscoveryServer(std::shared_ptr<ResourceManager> manager)
27+
: ResourceServer(std::move(manager)) {}
2828

2929
::grpc::Status DiscoverResources(
3030
::grpc::ServerContext* context,

src/viam/sdk/tests/mocks/mock_discovery.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ namespace viam {
77
namespace sdktests {
88
namespace discovery {
99

10-
std::shared_ptr<MockDiscovery> MockDiscovery::get_mock_discovery() {
11-
return std::make_shared<MockDiscovery>("mock_discovery");
12-
}
10+
MockDiscovery::MockDiscovery(std::string name) : Discovery(std::move(name)) {}
1311

1412
std::vector<sdk::ResourceConfig> MockDiscovery::discover_resources(const sdk::ProtoStruct&) {
15-
return fake_resources();
13+
return fake_discovered_resources();
1614
}
1715

18-
sdk::ProtoStruct MockDiscovery::do_command(const sdk::ProtoStruct& command) {
19-
return (peek_command = command);
16+
ProtoStruct MockDiscovery::do_command(const sdk::ProtoStruct& command) {
17+
return ProtoStruct{};
2018
}
2119

2220
} // namespace discovery

src/viam/sdk/tests/mocks/mock_discovery.hpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@ namespace discovery {
88

99
class MockDiscovery : public sdk::Discovery {
1010
public:
11-
MockDiscovery(std::string name) : Discovery(std::move(name)) {}
11+
MockDiscovery(std::string);
1212

13-
static std::shared_ptr<MockDiscovery> get_mock_discovery();
14-
15-
std::vector<ResourceConfig> discover_resources(const sdk::ProtoStruct&) override;
13+
std::vector<sdk::ResourceConfig> discover_resources(const sdk::ProtoStruct&) override;
1614
sdk::ProtoStruct do_command(const sdk::ProtoStruct& command) override;
17-
18-
bool peek_stop_called;
19-
sdk::ProtoStruct peek_command;
2015
};
2116

2217
} // namespace discovery
Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#define BOOST_TEST_MODULE test module test_discovery
2-
#include <viam/sdk/components/discovery.hpp>
2+
#include <viam/sdk/services/discovery.hpp>
33

44
#include <boost/optional/optional_io.hpp>
55
#include <boost/qvm/all.hpp>
@@ -12,39 +12,26 @@ BOOST_TEST_DONT_PRINT_LOG_VALUE(std::vector<viam::sdk::ResourceConfig>)
1212

1313
namespace viam {
1414
namespace sdktests {
15+
namespace discovery {
1516

1617
using namespace viam::sdk;
1718

18-
BOOST_AUTO_TEST_SUITE(test_discovery)
19-
20-
BOOST_AUTO_TEST_CASE(mock_get_api) {
21-
const MockDiscovery discovery("mock_discovery");
22-
auto api = discovery.api();
23-
auto static_api = API::get<Discovery>();
24-
25-
BOOST_CHECK_EQUAL(api, static_api);
26-
BOOST_CHECK_EQUAL(static_api.resource_subtype(), "discovery");
27-
}
28-
2919
BOOST_AUTO_TEST_CASE(test_discover_resources) {
30-
std::shared_ptr<MockDiscovery> mock = MockDiscovery::get_mock_discovery();
20+
auto mock = std::make_shared<MockDiscovery>("mock_discovery");
3121
client_to_mock_pipeline<Discovery>(mock, [](Discovery& client) {
3222
const auto& resources = client.discover_resources();
33-
BOOST_CHECK_EQUAL(resource, fake_discovered_resources());
23+
BOOST_CHECK_EQUAL(resources.size(), 3);
3424
});
3525
}
3626

3727
BOOST_AUTO_TEST_CASE(test_do_command) {
38-
std::shared_ptr<MockDiscovery> mock = MockDiscovery::get_mock_discovery();
39-
client_to_mock_pipeline<Discovery>(mock, [](Discovery& client) {
40-
ProtoStruct expected = fake_map();
41-
42-
ProtoStruct command = fake_map();
43-
ProtoStruc result_map = client.do_command(command);
44-
45-
BOOST_CHECK(result.map.at("test") == expected.map.at("test"));
28+
auto mock = std::make_shared<MockDiscovery>("mock_discovery");
29+
client_to_mock_pipeline<Discovery>(mock, [&](Discovery& client) {
30+
const auto ret = client.do_command({});
31+
BOOST_CHECK_EQUAL(ret.size(), 0);
4632
});
4733
}
4834

35+
} // namespace discovery
4936
} // namespace sdktests
5037
} // namespace viam

src/viam/sdk/tests/test_utils.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,37 @@ std::vector<GeometryConfig> fake_geometries() {
2727
}
2828

2929
std::vector<ResourceConfig> fake_discovered_resources() {
30+
pose p{1, 2, 3};
31+
orientation_vector o{0, 0, 0, 1};
32+
translation t{1, 2, 3};
33+
34+
LinkConfig link(t, o, GeometryConfig(p, sphere{1}, "sphere"), "world");
35+
36+
3037
return {
3138
ResourceConfig("camera",
3239
"mycam",
3340
"rdk",
3441
{{"width", 640}, {"height", 480}, {"format", "mjpeg"}},
3542
"rdk:component:camera",
36-
"rdk:builtin:webcam"),
37-
ResourceConfig("motor",
38-
"arm_motor",
39-
"rand",
40-
{
41-
{"pins", {{"pwm", 18}, {"dir", 23}}},
42-
{"max_rpm", 100},
43-
{"encoder_steps", 200},
44-
},
45-
"rdk:component:motor",
46-
"rand:cool:motor"),
43+
Model("rdk", "camera", "mycam"),
44+
link),
4745
ResourceConfig("sensor",
4846
"temp_sensor",
4947
"viam",
5048
{{"type", "temperature"}, {"unit", "celsius"}, {"poll_rate", 1000}},
5149
"rdk:component:sensor",
52-
"viam:temp:sensor1"),
53-
};
54-
}
50+
Model("viam", "temp", "my_temp_sensor"),
51+
link),
52+
ResourceConfig("motor",
53+
"arm_motor",
54+
"rand",
55+
{{"max_rpm", 100}, {"encoder_steps", 200}},
56+
"rdk:component:motor",
57+
Model("rand", "cool", "my_motor"),
58+
link),
59+
};
60+
};
5561

5662
TestServer::TestServer(std::shared_ptr<Server> sdk_server) : sdk_server_(std::move(sdk_server)) {}
5763

src/viam/sdk/tests/test_utils.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ using namespace viam::sdk;
1414

1515
ProtoStruct fake_map();
1616
std::vector<GeometryConfig> fake_geometries();
17+
std::vector<ResourceConfig> fake_discovered_resources();
1718

1819
// TestServer is a wrapper around viam::sdk::Server that is a friend of the
1920
// class and can therefore access its private fields.

0 commit comments

Comments
 (0)