aboutsummaryrefslogtreecommitdiff
path: root/kernel/kapi/devices/interface_registry.tests.cpp
blob: 071b9a6d893f42d39ce0b1621f206e91afb0a94b (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <kapi/devices.hpp>

#include <kstd/memory.hpp>

#include <catch2/catch_test_macros.hpp>

#include <cstddef>

namespace
{
  struct probe_device
  {
    constexpr auto static id = kapi::devices::interface_id{"probe"};

    virtual ~probe_device() = default;

    [[nodiscard]] auto virtual get_value() const noexcept -> int = 0;
  };

  struct const_device
  {
    constexpr auto static id = kapi::devices::interface_id{"flip"};

    virtual ~const_device() = default;

    [[nodiscard]] auto get_constant() const noexcept -> int
    {
      return 0;
    }
  };

  struct unimplemented_device
  {
    constexpr auto static id = kapi::devices::interface_id{"unimplemented"};

    virtual ~unimplemented_device() = default;
  };

  struct test_device final : kapi::devices::device, probe_device
  {
    explicit test_device(int value, const_device & const_device)
        : device{"probeable"}
        , m_value{value}
        , m_const_device{&const_device}
    {}

    [[nodiscard]] constexpr auto get_value() const noexcept -> int override
    {
      return m_value;
    }

    auto query_interface(kapi::devices::interface_id interface) -> void * override
    {
      if (interface == probe_device::id)
      {
        return static_cast<probe_device *>(this);
      }
      if (interface == const_device::id)
      {
        return m_const_device;
      }

      return kapi::devices::device::query_interface(interface);
    }

  private:
    int m_value;
    const_device * m_const_device;
  };

  struct counting_observer final : kapi::devices::interface_registry_observer
  {
    auto on_interface_published(kapi::devices::interface_id, kapi::devices::interface_registry::entry const &)
        -> void override
    {
      ++published;
    }

    auto on_interface_withdrawn(kapi::devices::interface_id, kapi::devices::device &) -> void override
    {
      ++withdrawn;
    }

    std::size_t published{};
    std::size_t withdrawn{};
  };

  struct evil_observer final : kapi::devices::interface_registry_observer
  {
    explicit evil_observer(kstd::shared_ptr<counting_observer> & victim)
        : m_victim{victim}
    {}

    auto on_interface_published(kapi::devices::interface_id, kapi::devices::interface_registry::entry const &)
        -> void override
    {
      m_victim.reset();
    }

    auto on_interface_withdrawn(kapi::devices::interface_id, kapi::devices::device &) -> void override
    {
      m_victim.reset();
    }

  private:
    kstd::shared_ptr<counting_observer> & m_victim;  // NOLINT
  };

}  // namespace

SCENARIO("Publishing and finding a device", "[kapi][devices][interface_registry]")
{
  GIVEN("An empty registry, a device, and an interface the device implements by inheritance")
  {
    auto registry = kapi::devices::interface_registry{};
    auto free_standing_interface = const_device{};
    auto device = kstd::make_shared<test_device>(128, free_standing_interface);

    THEN("Publishing an interface without a name fails")
    {
      REQUIRE_FALSE(registry.publish<probe_device>(device, ""));
    }

    WHEN("publishing the interface for the device")
    {
      CHECK(device->is_a<probe_device>());
      CHECK(device->is_a<const_device>());
      auto published = registry.publish(device, "probe0", device->as<probe_device>());

      THEN("publishing is successful")
      {
        REQUIRE(published);
      }

      THEN("publishing the same device and interface with a different name fails")
      {
        REQUIRE_FALSE(registry.publish(device, "probe1", device->as<probe_device>()));
      }

      THEN("publishing a second interface for the same device succeeds")
      {
        REQUIRE(registry.publish<const_device>(device, "probe0"));
      }

      THEN("publishing a free standing second interface for the same device succeeds")
      {
        REQUIRE(registry.publish(device, "probe0", &free_standing_interface));
      }

      AND_WHEN("getting all devices implementing that interface")
      {
        auto probeable_devices = registry.all(probe_device::id);

        THEN("there is exactly one such device")
        {
          REQUIRE(probeable_devices.size() == 1);
        }

        THEN("the name of the device is 'probe0'")
        {
          REQUIRE(probeable_devices[0].name() == "probe0");
        }

        THEN("the returned device pointer is valid")
        {
          REQUIRE(probeable_devices[0].device());
        }

        THEN("the returned implementation equals the result of device::as")
        {
          REQUIRE(probeable_devices[0].implementation() == device->as<probe_device>());
        }

        THEN("the interface provided function can be invoked")
        {
          auto implementation = probeable_devices[0].as<probe_device>();
          REQUIRE(implementation->get_value() == device->get_value());
        }
      }

      THEN("resolve finds the published capability by name")
      {
        REQUIRE(registry.resolve<probe_device>("probe0"));
      }

      THEN("resolve finds a capability the device implements but never published")
      {
        REQUIRE(registry.resolve<const_device>("probe0"));
      }

      THEN("resolve for a capability the device implements nowhere at all does not find anything")
      {
        REQUIRE_FALSE(registry.resolve<unimplemented_device>("probe0"));
      }

      THEN("resolve for an unpublished device does not find a device")
      {
        REQUIRE_FALSE(registry.resolve<probe_device>("probe1"));
      }

      THEN("withdrawing an interface for a device removes it from the registry")
      {
        registry.withdraw(*device, probe_device::id);
        REQUIRE_FALSE(registry.resolve<probe_device>("probe0"));
      }
    }

    WHEN("the device is destroyed without withdrawing it")
    {
      CHECK(registry.publish(device, "probe0", device->as<probe_device>()));
      device.reset();

      THEN("querying all does no longer report it")
      {
        REQUIRE(registry.all(probe_device::id).empty());
      }

      THEN("resolve() no longer finds it")
      {
        REQUIRE_FALSE(registry.resolve<probe_device>("probe0"));
      }
    }
  }

  GIVEN("no device has ever been published")
  {
    auto registry = kapi::devices::interface_registry{};

    THEN("all() returns an empty vector")
    {
      REQUIRE(registry.all(probe_device::id).empty());
    }
  }

  GIVEN("a null device")
  {
    auto registry = kapi::devices::interface_registry{};
    auto device = kstd::shared_ptr<kapi::devices::device>{};
    auto interface = const_device{};

    THEN("publishing the interface for the device fails")
    {
      REQUIRE_FALSE(registry.publish(device, "probe0", &interface));
    }
  }
}

SCENARIO("Interface registry notifies subscribers", "[kapi][devices][interface_registry]")
{
  GIVEN("a device and a subscribed observer")
  {
    auto registry = kapi::devices::interface_registry{};
    auto free_standing_interface = const_device{};
    auto device = kstd::make_shared<test_device>(128, free_standing_interface);
    auto observer = kstd::make_shared<counting_observer>();

    registry.subscribe(observer);

    WHEN("an interface is published for the device")
    {
      CHECK(registry.publish(device, "probe0", device->as<probe_device>()));

      THEN("the observer is notified exactly once")
      {
        REQUIRE(observer->published == 1);
        REQUIRE(observer->withdrawn == 0);
      }
    }

    WHEN("an interface is published and then withdrawn")
    {
      CHECK(registry.publish(device, "probe0", device->as<probe_device>()));
      registry.withdraw(*device, probe_device::id);

      THEN("the observer is notified of both exactly one")
      {
        REQUIRE(observer->published == 1);
        REQUIRE(observer->withdrawn == 1);
      }
    }

    WHEN("the observer is destroyed before anything is published")
    {
      observer.reset();

      THEN("publishing afterward does not crash")
      {
        REQUIRE_NOTHROW(registry.publish(device, "probe0", device->as<probe_device>()));
      }
    }

    WHEN("the observer unsubscribes before anything is published")
    {
      registry.unsubscribe(*observer);

      THEN("publishing afterward does not crash")
      {
        REQUIRE_NOTHROW(registry.publish(device, "probe0", device->as<probe_device>()));
      }

      THEN("the subscriber is not notified")
      {
        REQUIRE_NOTHROW(registry.publish(device, "probe0", device->as<probe_device>()));
        REQUIRE_NOTHROW(registry.withdraw(*device, probe_device::id));
        REQUIRE(observer->published == 0);
        REQUIRE(observer->withdrawn == 0);
      }
    }
  }

  GIVEN("two subscriber, the first destroying the second")
  {
    auto registry = kapi::devices::interface_registry{};
    auto free_standing_interface = const_device{};
    auto device = kstd::make_shared<test_device>(128, free_standing_interface);

    auto second = kstd::make_shared<counting_observer>();
    auto first = kstd::make_shared<evil_observer>(second);

    registry.subscribe(first);
    registry.subscribe(second);

    THEN("publishing an interface does not crash")
    {
      REQUIRE_NOTHROW(registry.publish(device, "probe0", device->as<probe_device>()));
      REQUIRE(second == nullptr);
    }
  }
}