annotate UnitTestsSources/TestMessageBroker.cpp @ 1153:113131100638 broker

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 16 Nov 2019 08:59:55 +0100
parents f72d1ab42932
children 0ca50d275b9a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
1 /**
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
2 * Stone of Orthanc
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
4 * Department, University Hospital of Liege, Belgium
440
a750f11892ec merge default->am-vsol-upgrade
amazy
parents: 428 439
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
6 *
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
7 * This program is free software: you can redistribute it and/or
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
10 * the License, or (at your option) any later version.
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
11 *
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
12 * This program is distributed in the hope that it will be useful, but
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
15 * Affero General Public License for more details.
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
16 *
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
19 **/
251
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
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
22 #include "gtest/gtest.h"
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
23
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
24 #include "Framework/Messages/IObservable.h"
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
25 #include "Framework/Messages/ObserverBase.h"
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
26
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
27
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
28 int testCounter = 0;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
29 namespace {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
30
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
31 using namespace OrthancStone;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
32
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
33
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
34 class MyObservable : public IObservable
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
35 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
36 public:
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
37 struct MyCustomMessage : public IMessage
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
38 {
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
39 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__);
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
40
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
41 int payload_;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
42
643
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
43 MyCustomMessage(int payload) :
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
44 payload_(payload)
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
45 {
f0008c55e5f7 getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 624
diff changeset
46 }
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
47 };
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
48 };
267
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
49
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
50 class MyObserver : public ObserverBase<MyObserver>
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
51 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
52 public:
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
53 void HandleCompletedMessage(const MyObservable::MyCustomMessage& message)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
54 {
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
55 testCounter += message.payload_;
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
56 }
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
57 };
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
58 }
251
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
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
61 TEST(MessageBroker, TestPermanentConnectionSimpleUseCase)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
62 {
1066
b537002f83a9 removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1057
diff changeset
63 MyObservable observable;
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
64 boost::shared_ptr<MyObserver> observer(new MyObserver);
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
65
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
66 // create a permanent connection between an observable and an observer
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
67 observer->Register<MyObservable::MyCustomMessage>(observable, &MyObserver::HandleCompletedMessage);
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
68
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
69 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
70 observable.BroadcastMessage(MyObservable::MyCustomMessage(12));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
71 ASSERT_EQ(12, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
72
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
73 // the connection is permanent; if we emit the same message again, the observer will be notified again
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
74 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
75 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
76 ASSERT_EQ(20, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
77
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
78 // Unregister the observer; make sure it's not called anymore
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
79 observer.reset();
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
80 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
81 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
82 ASSERT_EQ(0, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
83 }
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
84
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
85 TEST(MessageBroker, TestPermanentConnectionDeleteObserver)
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
86 {
1066
b537002f83a9 removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1057
diff changeset
87 MyObservable observable;
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
88 boost::shared_ptr<MyObserver> observer(new MyObserver);
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents:
diff changeset
89
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
90 // create a permanent connection between an observable and an observer
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
91 observer->Register<MyObservable::MyCustomMessage>(observable, &MyObserver::HandleCompletedMessage);
267
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
92
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
93 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
94 observable.BroadcastMessage(MyObservable::MyCustomMessage(12));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
95 ASSERT_EQ(12, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
96
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
97 // delete the observer and check that the callback is not called anymore
1067
05b2e71ed145 removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
98 observer.reset();
267
89d02de83c03 added declaretion of messages handled/emitted
am@osimis.io
parents: 266
diff changeset
99
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
100 // the connection is permanent; if we emit the same message again, the observer will be notified again
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
101 testCounter = 0;
624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 621
diff changeset
102 observable.BroadcastMessage(MyObservable::MyCustomMessage(20));
428
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
103 ASSERT_EQ(0, testCounter);
751fb354149e ability to change the scene of the RadiographyWidget
am@osimis.io
parents: 397
diff changeset
104 }