Mercurial > hg > orthanc-stone
annotate UnitTestsSources/TestMessageBroker.cpp @ 361:f559ac66ef55 am-2
first export of a dicom image
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 30 Oct 2018 16:34:56 +0100 |
parents | 3897f9f28cfa |
children | 1d9dd542adfe |
rev | line source |
---|---|
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
1 ///** |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
2 // * Stone of Orthanc |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
3 // * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
4 // * Department, University Hospital of Liege, Belgium |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
5 // * Copyright (C) 2017-2018 Osimis S.A., Belgium |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
6 // * |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
7 // * This program is free software: you can redistribute it and/or |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
8 // * modify it under the terms of the GNU Affero General Public License |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
9 // * as published by the Free Software Foundation, either version 3 of |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
10 // * the License, or (at your option) any later version. |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
11 // * |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
12 // * This program is distributed in the hope that it will be useful, but |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
13 // * WITHOUT ANY WARRANTY; without even the implied warranty of |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
14 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
15 // * Affero General Public License for more details. |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
16 // * |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
17 // * You should have received a copy of the GNU Affero General Public License |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
18 // * along with this program. If not, see <http://www.gnu.org/licenses/>. |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
19 // **/ |
251 | 20 |
21 | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
22 //#include "gtest/gtest.h" |
251 | 23 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
24 //#include "../Framework/Messages/MessageBroker.h" |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
25 //#include "../Framework/Messages/IMessage.h" |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
26 //#include "../Framework/Messages/IObservable.h" |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
27 //#include "../Framework/Messages/IObserver.h" |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
28 //#include "../Framework/StoneEnumerations.h" |
251 | 29 |
30 | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
31 //static int test1Counter = 0; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
32 //static int test2Counter = 0; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
33 //class MyFullObserver : public OrthancStone::IObserver |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
34 //{ |
267 | 35 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
36 //public: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
37 // MyFullObserver(OrthancStone::MessageBroker& broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
38 // : OrthancStone::IObserver(broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
39 // { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
40 //// DeclareHandledMessage(OrthancStone::MessageType_Test1); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
41 //// DeclareIgnoredMessage(OrthancStone::MessageType_Test2); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
42 // } |
267 | 43 |
44 | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
45 // void HandleMessage(OrthancStone::IObservable& from, const OrthancStone::IMessage& message) { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
46 // switch (message.GetType()) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
47 // { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
48 // case OrthancStone::MessageType_Test1: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
49 // test1Counter++; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
50 // break; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
51 // case OrthancStone::MessageType_Test2: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
52 // test2Counter++; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
53 // break; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
54 // default: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
55 // throw OrthancStone::MessageNotDeclaredException(message.GetType()); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
56 // } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
57 // } |
267 | 58 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
59 //}; |
267 | 60 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
61 //class MyPartialObserver : public OrthancStone::IObserver |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
62 //{ |
251 | 63 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
64 //public: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
65 // MyPartialObserver(OrthancStone::MessageBroker& broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
66 // : OrthancStone::IObserver(broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
67 // { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
68 //// DeclareHandledMessage(OrthancStone::MessageType_Test1); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
69 // // don't declare Test2 on purpose |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
70 // } |
251 | 71 |
72 | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
73 // void HandleMessage(OrthancStone::IObservable& from, const OrthancStone::IMessage& message) { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
74 // switch (message.GetType()) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
75 // { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
76 // case OrthancStone::MessageType_Test1: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
77 // test1Counter++; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
78 // break; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
79 // case OrthancStone::MessageType_Test2: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
80 // test2Counter++; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
81 // break; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
82 // default: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
83 // throw OrthancStone::MessageNotDeclaredException(message.GetType()); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
84 // } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
85 // } |
251 | 86 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
87 //}; |
267 | 88 |
89 | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
90 //class MyObservable : public OrthancStone::IObservable |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
91 //{ |
267 | 92 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
93 //public: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
94 // MyObservable(OrthancStone::MessageBroker& broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
95 // : OrthancStone::IObservable(broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
96 // { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
97 // DeclareEmittableMessage(OrthancStone::MessageType_Test1); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
98 // DeclareEmittableMessage(OrthancStone::MessageType_Test2); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
99 // } |
251 | 100 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
101 //}; |
251 | 102 |
103 | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
104 //TEST(MessageBroker, NormalUsage) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
105 //{ |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
106 // OrthancStone::MessageBroker broker; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
107 // MyObservable observable(broker); |
251 | 108 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
109 // test1Counter = 0; |
251 | 110 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
111 // // no observers have been registered -> nothing shall happen |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
112 // observable.EmitMessage(OrthancStone::IMessage(OrthancStone::MessageType_Test1)); |
251 | 113 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
114 // ASSERT_EQ(0, test1Counter); |
251 | 115 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
116 // // register an observer, check it is called |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
117 // MyFullObserver fullObserver(broker); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
118 // ASSERT_NO_THROW(observable.RegisterObserver(fullObserver)); |
251 | 119 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
120 // observable.EmitMessage(OrthancStone::IMessage(OrthancStone::MessageType_Test1)); |
251 | 121 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
122 // ASSERT_EQ(1, test1Counter); |
251 | 123 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
124 // // register an invalid observer, check it raises an exception |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
125 // MyPartialObserver partialObserver(broker); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
126 // ASSERT_THROW(observable.RegisterObserver(partialObserver), OrthancStone::MessageNotDeclaredException); |
251 | 127 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
128 // // check an exception is thrown when the observable emits an undeclared message |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
129 // ASSERT_THROW(observable.EmitMessage(OrthancStone::IMessage(OrthancStone::MessageType_LayerSource_GeometryReady)), OrthancStone::MessageNotDeclaredException); |
251 | 130 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
131 // // unregister the observer, make sure nothing happens afterwards |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
132 // observable.UnregisterObserver(fullObserver); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
133 // observable.EmitMessage(OrthancStone::IMessage(OrthancStone::MessageType_Test1)); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
134 // ASSERT_EQ(1, test1Counter); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
135 //} |
251 | 136 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
137 //TEST(MessageBroker, DeleteObserverWhileRegistered) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
138 //{ |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
139 // OrthancStone::MessageBroker broker; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
140 // MyObservable observable(broker); |
251 | 141 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
142 // test1Counter = 0; |
251 | 143 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
144 // { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
145 // // register an observer, check it is called |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
146 // MyFullObserver observer(broker); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
147 // observable.RegisterObserver(observer); |
251 | 148 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
149 // observable.EmitMessage(OrthancStone::IMessage(OrthancStone::MessageType_Test1)); |
251 | 150 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
151 // ASSERT_EQ(1, test1Counter); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
152 // } |
251 | 153 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
154 // // at this point, the observer has been deleted, the handle shall not be called again (and it shall not crash !) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
155 // observable.EmitMessage(OrthancStone::IMessage(OrthancStone::MessageType_Test1)); |
251 | 156 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
157 // ASSERT_EQ(1, test1Counter); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
268
diff
changeset
|
158 //} |