Mercurial > hg > orthanc-stone
annotate OrthancStone/UnitTestsSources/TestMessageBroker.cpp @ 1556:8898f8f755c8
typo
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 18 Aug 2020 11:58:47 +0200 |
parents | 244ad1e4e76a |
children |
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 |
1270
2d8ab34c8c91
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1080
diff
changeset
|
5 * Copyright (C) 2017-2020 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 | 20 |
21 | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1279
diff
changeset
|
22 #include <gtest/gtest.h> |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
23 |
1512
244ad1e4e76a
reorganization of folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1455
diff
changeset
|
24 #include "../Sources/Messages/IObservable.h" |
244ad1e4e76a
reorganization of folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1455
diff
changeset
|
25 #include "../Sources/Messages/ObserverBase.h" |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
26 |
251 | 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 | 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 | 59 |
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 | 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 | 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 | 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 | 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 | 92 |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
397
diff
changeset
|
93 testCounter = 0; |
624 | 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 | 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 | 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 } |