CARLA
test_rpc.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #include "test.h"
8 
10 #include <carla/ThreadGroup.h>
11 #include <carla/rpc/Actor.h>
12 #include <carla/rpc/Client.h>
13 #include <carla/rpc/Response.h>
14 #include <carla/rpc/Server.h>
15 
16 #include <thread>
17 
18 using namespace carla::rpc;
19 using namespace std::chrono_literals;
20 
21 TEST(rpc, compilation_tests) {
22  Server server(TESTING_PORT);
23  server.BindSync("bind00", []() { return 2.0f; });
24  server.BindSync("bind01", [](int x) { return x; });
25  server.BindSync("bind02", [](int, float) { return 0.0; });
26  server.BindSync("bind03", [](int, float, double, char) {});
27 }
28 
29 TEST(rpc, server_bind_sync_run_on_game_thread) {
30  const auto main_thread_id = std::this_thread::get_id();
31 
32  const uint16_t port = (TESTING_PORT != 0u ? TESTING_PORT : 2017u);
33 
34  Server server(port);
35 
36  server.BindSync("do_the_thing", [=](int x, int y) -> int {
37  EXPECT_EQ(std::this_thread::get_id(), main_thread_id);
38  return x + y;
39  });
40 
41  server.AsyncRun(1u);
42 
43  std::atomic_bool done{false};
44 
45  carla::ThreadGroup threads;
46  threads.CreateThread([&]() {
47  Client client("localhost", port);
48  for (auto i = 0; i < 300; ++i) {
49  auto result = client.call("do_the_thing", i, 1).as<int>();
50  EXPECT_EQ(result, i + 1);
51  }
52  done = true;
53  });
54 
55  auto i = 0u;
56  for (; i < 1'000'000u; ++i) {
57  server.SyncRunFor(2ms);
58  if (done) {
59  break;
60  }
61  }
62  std::cout << "game thread: run " << i << " slices.\n";
63  ASSERT_TRUE(done);
64 }
void AsyncRun(size_t worker_threads)
Definition: rpc/Server.h:49
constexpr uint16_t TESTING_PORT
Definition: test.h:24
void BindSync(const std::string &name, FunctorT &&functor)
Definition: rpc/Server.h:152
void CreateThread(F &&functor)
Definition: ThreadGroup.h:27
auto call(const std::string &function, Args &&... args)
Definition: rpc/Client.h:32
An RPC server in which functions can be bind to run synchronously or asynchronously.
Definition: rpc/Server.h:37
TEST(rpc, compilation_tests)
Definition: test_rpc.cpp:21
void SyncRunFor(time_duration duration)
Definition: rpc/Server.h:53