annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
1 /**
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
2 * Stone of Orthanc
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
6 *
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
10 * the License, or (at your option) any later version.
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
11 *
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
15 * Affero General Public License for more details.
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
16 *
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
19 **/
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
20
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
21
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
22 #include "gtest/gtest.h"
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
23
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
24 #include "../Framework/Messages/MessageBroker.h"
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
25 #include "../Framework/Messages/IMessage.h"
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
26 #include "../Framework/Messages/IObservable.h"
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
27 #include "../Framework/Messages/IObserver.h"
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
28 #include "../Framework/StoneEnumerations.h"
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
29
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
30
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
31 static int globalCounter = 0;
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
32 class MyObserver : public OrthancStone::IObserver
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
33 {
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
34
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
35 public:
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
36 MyObserver(OrthancStone::MessageBroker& broker)
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
37 : OrthancStone::IObserver(broker)
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
38 {}
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
39
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
40
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
41 void HandleMessage(OrthancStone::IObservable& from, const OrthancStone::IMessage& message) {
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
42 if (message.GetType() == OrthancStone::MessageType_Generic) {
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
43 globalCounter++;
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
44 }
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
45
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
46 }
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
47
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
48 };
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
49
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
50
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
51 TEST(MessageBroker, NormalUsage)
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
52 {
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
53 OrthancStone::MessageBroker broker;
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
54 OrthancStone::IObservable observable(broker);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
55
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
56 globalCounter = 0;
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
57
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
58 OrthancStone::IMessage genericMessage(OrthancStone::MessageType_Generic);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
59
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
60 // no observers have been registered -> nothing shall happen
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
61 observable.Emit(genericMessage);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
62
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
63 ASSERT_EQ(0, globalCounter);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
64
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
65 // register an observer, check it is called
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
66 MyObserver observer(broker);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
67 observable.RegisterObserver(observer);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
68
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
69 observable.Emit(genericMessage);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
70
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
71 ASSERT_EQ(1, globalCounter);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
72
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
73 // check the observer is not called when another message is issued
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
74 OrthancStone::IMessage wrongMessage(OrthancStone::MessageType_GeometryReady);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
75 // no observers have been registered
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
76 observable.Emit(wrongMessage);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
77
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
78 ASSERT_EQ(1, globalCounter);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
79
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
80 // unregister the observer, make sure nothing happens afterwards
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
81 observable.UnregisterObserver(observer);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
82 observable.Emit(genericMessage);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
83 ASSERT_EQ(1, globalCounter);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
84 }
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
85
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
86 TEST(MessageBroker, DeleteObserverWhileRegistered)
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
87 {
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
88 OrthancStone::MessageBroker broker;
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
89 OrthancStone::IObservable observable(broker);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
90
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
91 globalCounter = 0;
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
92
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
93 OrthancStone::IMessage genericMessage(OrthancStone::MessageType_Generic);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
94
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
95 {
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
96 // register an observer, check it is called
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
97 MyObserver observer(broker);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
98 observable.RegisterObserver(observer);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
99
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
100 observable.Emit(genericMessage);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
101
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
102 ASSERT_EQ(1, globalCounter);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
103 }
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
104
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
105 // at this point, the observer has been deleted, the handle shall not be called again (and it shall not crash !)
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
106 observable.Emit(genericMessage);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
107
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
108 ASSERT_EQ(1, globalCounter);
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
109 }