Mercurial > hg > orthanc-stone
comparison OrthancStone/UnitTestsSources/TestMessageBroker.cpp @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | UnitTestsSources/TestMessageBroker.cpp@30deba7bc8e2 |
children |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include <gtest/gtest.h> | |
23 | |
24 #include "../Sources/Messages/IObservable.h" | |
25 #include "../Sources/Messages/ObserverBase.h" | |
26 | |
27 | |
28 int testCounter = 0; | |
29 namespace { | |
30 | |
31 using namespace OrthancStone; | |
32 | |
33 | |
34 class MyObservable : public IObservable | |
35 { | |
36 public: | |
37 struct MyCustomMessage : public IMessage | |
38 { | |
39 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__); | |
40 | |
41 int payload_; | |
42 | |
43 MyCustomMessage(int payload) : | |
44 payload_(payload) | |
45 { | |
46 } | |
47 }; | |
48 }; | |
49 | |
50 class MyObserver : public ObserverBase<MyObserver> | |
51 { | |
52 public: | |
53 void HandleCompletedMessage(const MyObservable::MyCustomMessage& message) | |
54 { | |
55 testCounter += message.payload_; | |
56 } | |
57 }; | |
58 } | |
59 | |
60 | |
61 TEST(MessageBroker, TestPermanentConnectionSimpleUseCase) | |
62 { | |
63 MyObservable observable; | |
64 boost::shared_ptr<MyObserver> observer(new MyObserver); | |
65 | |
66 // create a permanent connection between an observable and an observer | |
67 observer->Register<MyObservable::MyCustomMessage>(observable, &MyObserver::HandleCompletedMessage); | |
68 | |
69 testCounter = 0; | |
70 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); | |
71 ASSERT_EQ(12, testCounter); | |
72 | |
73 // the connection is permanent; if we emit the same message again, the observer will be notified again | |
74 testCounter = 0; | |
75 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); | |
76 ASSERT_EQ(20, testCounter); | |
77 | |
78 // Unregister the observer; make sure it's not called anymore | |
79 observer.reset(); | |
80 testCounter = 0; | |
81 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); | |
82 ASSERT_EQ(0, testCounter); | |
83 } | |
84 | |
85 TEST(MessageBroker, TestPermanentConnectionDeleteObserver) | |
86 { | |
87 MyObservable observable; | |
88 boost::shared_ptr<MyObserver> observer(new MyObserver); | |
89 | |
90 // create a permanent connection between an observable and an observer | |
91 observer->Register<MyObservable::MyCustomMessage>(observable, &MyObserver::HandleCompletedMessage); | |
92 | |
93 testCounter = 0; | |
94 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); | |
95 ASSERT_EQ(12, testCounter); | |
96 | |
97 // delete the observer and check that the callback is not called anymore | |
98 observer.reset(); | |
99 | |
100 // the connection is permanent; if we emit the same message again, the observer will be notified again | |
101 testCounter = 0; | |
102 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); | |
103 ASSERT_EQ(0, testCounter); | |
104 } |