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