comparison UnitTestsSources/TestMessageBroker.cpp @ 251:192e6e349e69 am-2

first usage of new message system (in SDL only)
author am@osimis.io
date Mon, 02 Jul 2018 18:13:46 +0200
parents
children e5a9b3d03478
comparison
equal deleted inserted replaced
250:5e642859267e 251:192e6e349e69
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-2018 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 "../Framework/Messages/MessageBroker.h"
25 #include "../Framework/Messages/IMessage.h"
26 #include "../Framework/Messages/IObservable.h"
27 #include "../Framework/Messages/IObserver.h"
28 #include "../Framework/StoneEnumerations.h"
29
30
31 static int globalCounter = 0;
32 class MyObserver : public OrthancStone::IObserver
33 {
34
35 public:
36 MyObserver(OrthancStone::MessageBroker& broker)
37 : OrthancStone::IObserver(broker)
38 {}
39
40
41 void HandleMessage(OrthancStone::IObservable& from, const OrthancStone::IMessage& message) {
42 if (message.GetType() == OrthancStone::MessageType_Generic) {
43 globalCounter++;
44 }
45
46 }
47
48 };
49
50
51 TEST(MessageBroker, NormalUsage)
52 {
53 OrthancStone::MessageBroker broker;
54 OrthancStone::IObservable observable(broker);
55
56 globalCounter = 0;
57
58 OrthancStone::IMessage genericMessage(OrthancStone::MessageType_Generic);
59
60 // no observers have been registered -> nothing shall happen
61 observable.Emit(genericMessage);
62
63 ASSERT_EQ(0, globalCounter);
64
65 // register an observer, check it is called
66 MyObserver observer(broker);
67 observable.RegisterObserver(observer);
68
69 observable.Emit(genericMessage);
70
71 ASSERT_EQ(1, globalCounter);
72
73 // check the observer is not called when another message is issued
74 OrthancStone::IMessage wrongMessage(OrthancStone::MessageType_GeometryReady);
75 // no observers have been registered
76 observable.Emit(wrongMessage);
77
78 ASSERT_EQ(1, globalCounter);
79
80 // unregister the observer, make sure nothing happens afterwards
81 observable.UnregisterObserver(observer);
82 observable.Emit(genericMessage);
83 ASSERT_EQ(1, globalCounter);
84 }
85
86 TEST(MessageBroker, DeleteObserverWhileRegistered)
87 {
88 OrthancStone::MessageBroker broker;
89 OrthancStone::IObservable observable(broker);
90
91 globalCounter = 0;
92
93 OrthancStone::IMessage genericMessage(OrthancStone::MessageType_Generic);
94
95 {
96 // register an observer, check it is called
97 MyObserver observer(broker);
98 observable.RegisterObserver(observer);
99
100 observable.Emit(genericMessage);
101
102 ASSERT_EQ(1, globalCounter);
103 }
104
105 // at this point, the observer has been deleted, the handle shall not be called again (and it shall not crash !)
106 observable.Emit(genericMessage);
107
108 ASSERT_EQ(1, globalCounter);
109 }