~thomas-voss/location-service/add-snapcraft-setup-next

« back to all changes in this revision

Viewing changes to tests/serializing_bus_test.cpp

Merge lp:~thomas-voss/location-service/simplify-provider-interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2016 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
 
 
19
#include <location/serializing_bus.h>
 
20
#include <location/runtime.h>
 
21
 
 
22
#include <gmock/gmock.h>
 
23
 
 
24
namespace
 
25
{
 
26
struct IndexedEvent : public location::Event
 
27
{
 
28
    IndexedEvent(std::size_t idx) : idx{idx}
 
29
    {
 
30
    }
 
31
 
 
32
    Event::Type type() const override
 
33
    {
 
34
        return Event::Type{Event::Type::first_user_defined_type};
 
35
    }
 
36
 
 
37
    std::size_t idx;
 
38
};
 
39
 
 
40
struct MockEventReceiver : public location::Event::Receiver
 
41
{
 
42
    MOCK_METHOD1(on_new_event, void(const location::Event&));
 
43
};
 
44
}
 
45
 
 
46
TEST(SerializingBus, ctor_and_dtor_work_for_valid_runtime)
 
47
{
 
48
    auto sb = location::SerializingBus::create(location::Runtime::create());
 
49
}
 
50
 
 
51
TEST(SerializingBus, dispatches_events_serially)
 
52
{
 
53
    using namespace ::testing;
 
54
 
 
55
    auto rt = location::Runtime::create();
 
56
    auto sb = location::SerializingBus::create(rt);
 
57
    auto receiver = std::make_shared<NiceMock<MockEventReceiver>>();
 
58
 
 
59
    std::size_t last{0};
 
60
 
 
61
    auto verifier = [&last](const location::Event& e)
 
62
    {
 
63
        const IndexedEvent& indexed_event = dynamic_cast<const IndexedEvent&>(e);
 
64
        EXPECT_EQ(1, indexed_event.idx - last);
 
65
        last = indexed_event.idx;
 
66
    };
 
67
 
 
68
    ON_CALL(*receiver, on_new_event(_)).WillByDefault(Invoke(verifier));
 
69
 
 
70
    sb->subscribe(receiver);
 
71
 
 
72
    rt->start();
 
73
 
 
74
    for (std::size_t idx = 1; idx <= 100000; idx++)
 
75
        sb->dispatch(std::make_shared<IndexedEvent>(idx));
 
76
 
 
77
    while (last != 100000) {}
 
78
}