File: mtl.cpp

package info (click to toggle)
actor-framework 0.18.7-1~exp1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 8,740 kB
  • sloc: cpp: 85,162; sh: 491; python: 187; makefile: 11
file content (220 lines) | stat: -rw-r--r-- 6,902 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.

#define CAF_SUITE mtl

#include "caf/mtl.hpp"

#include "core-test.hpp"

#include "caf/json_reader.hpp"
#include "caf/json_writer.hpp"
#include "caf/typed_actor.hpp"
#include "caf/typed_event_based_actor.hpp"

using namespace caf;

namespace {

using testee_actor = typed_actor<result<void>(put_atom, std::string, int32_t),
                                 result<int32_t>(get_atom, std::string)>;

struct testee_state {
  static inline const char* name = "testee";

  std::map<std::string, std::int32_t> kv_store;

  testee_actor::behavior_type make_behavior() {
    return {
      [this](put_atom, const std::string& key, int32_t val) {
        kv_store[key] = val;
      },
      [this](get_atom, const std::string& key) -> result<int32_t> {
        if (auto i = kv_store.find(key); i != kv_store.end())
          return {i->second};
        else
          return {make_error(sec::runtime_error, "key not found")};
      },
    };
  }
};

using testee_impl = testee_actor::stateful_impl<testee_state>;

template <class T>
struct kvp_field_name;

template <>
struct kvp_field_name<std::string> {
  static constexpr string_view value = "key";
};

template <>
struct kvp_field_name<int32_t> {
  static constexpr string_view value = "value";
};

template <class T>
constexpr string_view kvp_field_name_v = kvp_field_name<T>::value;

// Adapter for converting atom-prefixed message to pseudo-objects.
struct adapter {
  template <class Inspector, class Atom, class... Ts>
  bool read(Inspector& f, Atom, Ts&... xs) {
    auto type_annotation = type_name_v<Atom>;
    if (f.assert_next_object_name(type_annotation)
        && f.virtual_object(type_annotation)
             .fields(f.field(kvp_field_name_v<Ts>, xs)...)) {
      last_read = make_type_id_list<Atom, Ts...>();
      return true;
    } else {
      return false;
    }
  }

  template <class Inspector>
  bool write(Inspector& f, int32_t result) {
    return f.apply(result);
  }

  template <class Inspector>
  bool write(Inspector& f) {
    return f.apply(unit);
  }

  // Stores the type IDs for the last successful read.
  type_id_list last_read = make_type_id_list();
};

struct driver_state {
  static inline const char* name = "driver";

  event_based_actor* self;

  testee_actor kvs;

  json_reader reader;

  json_writer writer;

  driver_state(event_based_actor* self, testee_actor kvs)
    : self(self), kvs(std::move(kvs)) {
    // nop
  }

  behavior make_behavior() {
    return {
      [this](const std::string& mode,
             const std::string& json_text) -> result<message> {
        reader.load(json_text);
        auto mtl = make_mtl(self, adapter{}, &reader);
        CHECK(mtl.self() == self);
        CHECK(std::addressof(mtl.reader()) == &reader);
        if (mode == "try_send") {
          CHECK(mtl.try_send(kvs));
          MESSAGE("adapter generated: " << mtl.adapter().last_read);
          return make_message();
        } else {
          CAF_ASSERT(mode == "try_request");
          auto rp = self->make_response_promise();
          auto on_result = [this, rp](auto&... xs) mutable {
            // Must receive either an int32_t or an empty message.
            if constexpr (sizeof...(xs) == 1) {
              CHECK_EQ(make_type_id_list<int32_t>(),
                       make_type_id_list<std::decay_t<decltype(xs)>...>());
            } else {
              static_assert(sizeof...(xs) == 0);
            }
            // Convert input to JSON and fulfill the promise using the string.
            writer.reset();
            adapter{}.write(writer, xs...);
            rp.deliver(to_string(writer.str()));
          };
          auto on_error = [rp](error& err) mutable {
            rp.deliver(std::move(err));
          };
          CHECK(mtl.try_request(kvs, infinite, on_result, on_error));
          MESSAGE("adapter generated: " << mtl.adapter().last_read);
          return rp;
        }
      },
      [](int32_t) {
        // nop
      },
    };
  }
};

using driver_impl = stateful_actor<driver_state>;

struct fixture : test_coordinator_fixture<> {
  testee_actor testee;

  actor driver;

  fixture() {
    testee = sys.spawn<testee_impl, lazy_init>();
    driver = sys.spawn<driver_impl, lazy_init>(testee);
  }
};

} // namespace

BEGIN_FIXTURE_SCOPE(fixture)

SCENARIO("an MTL allows sending asynchronous messages") {
  GIVEN("a driver using an MTL to communicate to the testee") {
    WHEN("sending a JSON put message to the driver") {
      std::string put = R"({"@type": "caf::put_atom", "key": "a", "value": 1})";
      THEN("try_send generates a CAF put message to the testee") {
        inject((std::string, std::string),
               from(self).to(driver).with("try_send", put));
        expect((put_atom, std::string, int32_t),
               from(driver).to(testee).with(_, "a", 1));
        CHECK(!sched.has_job());
      }
    }
    WHEN("send a JSON get message to the driver afterwards") {
      std::string get = R"({"@type": "caf::get_atom", "key": "a"})";
      THEN("try_send generates a CAF get message to the testee") {
        inject((std::string, std::string),
               from(self).to(driver).with("try_send", get));
        expect((get_atom, std::string), from(driver).to(testee).with(_, "a"));
        expect((int32_t), from(testee).to(driver).with(1));
        CHECK(!sched.has_job());
      }
    }
  }
}

SCENARIO("an MTL allows sending requests") {
  GIVEN("a driver using an MTL to communicate to the testee") {
    WHEN("sending a JSON put message to the driver") {
      std::string put = R"({"@type": "caf::put_atom", "key": "a", "value": 1})";
      THEN("try_request generates a CAF put message to the testee") {
        inject((std::string, std::string),
               from(self).to(driver).with("try_request", put));
        expect((put_atom, std::string, int32_t),
               from(driver).to(testee).with(_, "a", 1));
        expect((void), from(testee).to(driver));
        expect((std::string),
               from(driver).to(self).with(R"({"@type": "caf::unit_t"})"));
        CHECK(!sched.has_job());
      }
    }
    WHEN("send a JSON get message to the driver afterwards") {
      std::string get = R"({"@type": "caf::get_atom", "key": "a"})";
      THEN("try_request generates a CAF get message to the testee") {
        inject((std::string, std::string),
               from(self).to(driver).with("try_request", get));
        expect((get_atom, std::string), from(driver).to(testee).with(_, "a"));
        expect((int32_t), from(testee).to(driver).with(1));
        expect((std::string), from(driver).to(self).with("1"));
        CHECK(!sched.has_job());
      }
    }
  }
}

END_FIXTURE_SCOPE()