annotate UnitTestsSources/TestMessageBroker2.cpp @ 323:dbfe2e9e5020 am-2

playing with LambdaCallable
author am@osimis.io
date Mon, 15 Oct 2018 23:36:43 +0200
parents 4a79193ffb58
children e7a494bdd956
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
1 /**
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
2 * Stone of Orthanc
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
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:
diff changeset
4 * Department, University Hospital of Liege, Belgium
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
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:
diff changeset
6 *
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
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:
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:
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:
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:
diff changeset
11 *
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
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:
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:
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:
diff changeset
15 * Affero General Public License for more details.
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
16 *
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
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:
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:
diff changeset
19 **/
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
20
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
21
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
22 #include "gtest/gtest.h"
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
23
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
24 #include "Framework/Messages/MessageBroker.h"
300
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
25 #include "Framework/Messages/Promise.h"
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
26 #include "Framework/Messages/IObservable.h"
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
27 #include "Framework/Messages/IObserver.h"
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
28 #include "Framework/Messages/MessageForwarder.h"
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
29
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
30
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
31 int testCounter = 0;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
32 namespace {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
33
302
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
34 using namespace OrthancStone;
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
35
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
36
302
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
37 enum CustomMessageType
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
38 {
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
39 CustomMessageType_First = MessageType_CustomMessage + 1,
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
40
302
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
41 CustomMessageType_Completed,
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
42 CustomMessageType_Increment
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
43 };
300
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
44
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
45
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
46 class MyObservable : public IObservable
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
47 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
48 public:
302
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
49 struct MyCustomMessage: public BaseMessage<CustomMessageType_Completed>
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
50 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
51 int payload_;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
52
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
53 MyCustomMessage(int payload)
300
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
54 : BaseMessage(),
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
55 payload_(payload)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
56 {}
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
57 };
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
58
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
59 MyObservable(MessageBroker& broker)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
60 : IObservable(broker)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
61 {}
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
62
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
63 };
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
64
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
65 class MyObserver : public IObserver
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
66 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
67 public:
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
68 MyObserver(MessageBroker& broker)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
69 : IObserver(broker)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
70 {}
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
71
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
72 void HandleCompletedMessage(const MyObservable::MyCustomMessage& message)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
73 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
74 testCounter += message.payload_;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
75 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
76
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
77 };
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
78
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
79
300
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
80 class MyIntermediate : public IObserver, public IObservable
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
81 {
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
82 IObservable& observedObject_;
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
83 public:
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
84 MyIntermediate(MessageBroker& broker, IObservable& observedObject)
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
85 : IObserver(broker),
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
86 IObservable(broker),
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
87 observedObject_(observedObject)
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
88 {
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
89 observedObject_.RegisterObserverCallback(new MessageForwarder<MyObservable::MyCustomMessage>(broker, *this));
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
90 }
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
91 };
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
92
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
93
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
94 class MyPromiseSource : public IObservable
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
95 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
96 Promise* currentPromise_;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
97 public:
300
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
98 struct MyPromiseMessage: public BaseMessage<MessageType_Test1>
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
99 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
100 int increment;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
101
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
102 MyPromiseMessage(int increment)
300
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
103 : BaseMessage(),
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
104 increment(increment)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
105 {}
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
106 };
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
107
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
108 MyPromiseSource(MessageBroker& broker)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
109 : IObservable(broker),
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
110 currentPromise_(NULL)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
111 {}
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
112
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
113 Promise& StartSomethingAsync()
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
114 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
115 currentPromise_ = new Promise(broker_);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
116 return *currentPromise_;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
117 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
118
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
119 void CompleteSomethingAsyncWithSuccess(int payload)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
120 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
121 currentPromise_->Success(MyPromiseMessage(payload));
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
122 delete currentPromise_;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
123 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
124
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
125 void CompleteSomethingAsyncWithFailure(int payload)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
126 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
127 currentPromise_->Failure(MyPromiseMessage(payload));
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
128 delete currentPromise_;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
129 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
130 };
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
131
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
132
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
133 class MyPromiseTarget : public IObserver
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
134 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
135 public:
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
136 MyPromiseTarget(MessageBroker& broker)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
137 : IObserver(broker)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
138 {}
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
139
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
140 void IncrementCounter(const MyPromiseSource::MyPromiseMessage& args)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
141 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
142 testCounter += args.increment;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
143 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
144
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
145 void DecrementCounter(const MyPromiseSource::MyPromiseMessage& args)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
146 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
147 testCounter -= args.increment;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
148 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
149 };
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
150 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
151
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
152
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
153 TEST(MessageBroker2, TestPermanentConnectionSimpleUseCase)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
154 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
155 MessageBroker broker;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
156 MyObservable observable(broker);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
157 MyObserver observer(broker);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
158
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
159 // create a permanent connection between an observable and an observer
300
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
160 observable.RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(observer, &MyObserver::HandleCompletedMessage));
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
161
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
162 testCounter = 0;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
163 observable.EmitMessage(MyObservable::MyCustomMessage(12));
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
164 ASSERT_EQ(12, testCounter);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
165
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
166 // the connection is permanent; if we emit the same message again, the observer will be notified again
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
167 testCounter = 0;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
168 observable.EmitMessage(MyObservable::MyCustomMessage(20));
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
169 ASSERT_EQ(20, testCounter);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
170 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
171
323
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
172 TEST(MessageBroker2, TestMessageForwarderSimpleUseCase)
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
173 {
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
174 MessageBroker broker;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
175 MyObservable observable(broker);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
176 MyIntermediate intermediate(broker, observable);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
177 MyObserver observer(broker);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
178
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
179 // let the observer observers the intermediate that is actually forwarding the messages from the observable
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
180 intermediate.RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(observer, &MyObserver::HandleCompletedMessage));
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
181
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
182 testCounter = 0;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
183 observable.EmitMessage(MyObservable::MyCustomMessage(12));
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
184 ASSERT_EQ(12, testCounter);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
185
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
186 // the connection is permanent; if we emit the same message again, the observer will be notified again
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
187 testCounter = 0;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
188 observable.EmitMessage(MyObservable::MyCustomMessage(20));
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
189 ASSERT_EQ(20, testCounter);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
190 }
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
191
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
192 TEST(MessageBroker2, TestPermanentConnectionDeleteObserver)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
193 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
194 MessageBroker broker;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
195 MyObservable observable(broker);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
196 MyObserver* observer = new MyObserver(broker);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
197
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
198 // create a permanent connection between an observable and an observer
300
b4abaeb783b1 messaging refactoring almost complete: works fine in native
am@osimis.io
parents: 299
diff changeset
199 observable.RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(*observer, &MyObserver::HandleCompletedMessage));
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
200
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
201 testCounter = 0;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
202 observable.EmitMessage(MyObservable::MyCustomMessage(12));
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
203 ASSERT_EQ(12, testCounter);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
204
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
205 // delete the observer and check that the callback is not called anymore
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
206 delete observer;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
207
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
208 // the connection is permanent; if we emit the same message again, the observer will be notified again
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
209 testCounter = 0;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
210 observable.EmitMessage(MyObservable::MyCustomMessage(20));
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
211 ASSERT_EQ(0, testCounter);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
212 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
213
302
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
214 TEST(MessageBroker2, TestMessageForwarderDeleteIntermediate)
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
215 {
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
216 MessageBroker broker;
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
217 MyObservable observable(broker);
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
218 MyIntermediate* intermediate = new MyIntermediate(broker, observable);
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
219 MyObserver observer(broker);
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
220
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
221 // let the observer observers the intermediate that is actually forwarding the messages from the observable
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
222 intermediate->RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(observer, &MyObserver::HandleCompletedMessage));
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
223
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
224 testCounter = 0;
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
225 observable.EmitMessage(MyObservable::MyCustomMessage(12));
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
226 ASSERT_EQ(12, testCounter);
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
227
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
228 delete intermediate;
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
229
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
230 observable.EmitMessage(MyObservable::MyCustomMessage(20));
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
231 ASSERT_EQ(12, testCounter);
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
232 }
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
233
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
234 TEST(MessageBroker2, TestCustomMessage)
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
235 {
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
236 MessageBroker broker;
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
237 MyObservable observable(broker);
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
238 MyIntermediate intermediate(broker, observable);
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
239 MyObserver observer(broker);
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
240
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
241 // let the observer observers the intermediate that is actually forwarding the messages from the observable
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
242 intermediate.RegisterObserverCallback(new Callable<MyObserver, MyObservable::MyCustomMessage>(observer, &MyObserver::HandleCompletedMessage));
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
243
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
244 testCounter = 0;
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
245 observable.EmitMessage(MyObservable::MyCustomMessage(12));
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
246 ASSERT_EQ(12, testCounter);
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
247
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
248 // the connection is permanent; if we emit the same message again, the observer will be notified again
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
249 testCounter = 0;
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
250 observable.EmitMessage(MyObservable::MyCustomMessage(20));
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
251 ASSERT_EQ(20, testCounter);
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
252 }
4a79193ffb58 support for custom messages + no leaks in unit-tests
am@osimis.io
parents: 300
diff changeset
253
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
254
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
255 TEST(MessageBroker2, TestPromiseSuccessFailure)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
256 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
257 MessageBroker broker;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
258 MyPromiseSource source(broker);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
259 MyPromiseTarget target(broker);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
260
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
261 // test a successful promise
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
262 source.StartSomethingAsync()
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
263 .Then(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(target, &MyPromiseTarget::IncrementCounter))
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
264 .Else(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(target, &MyPromiseTarget::DecrementCounter));
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
265
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
266 testCounter = 0;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
267 source.CompleteSomethingAsyncWithSuccess(10);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
268 ASSERT_EQ(10, testCounter);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
269
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
270 // test a failing promise
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
271 source.StartSomethingAsync()
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
272 .Then(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(target, &MyPromiseTarget::IncrementCounter))
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
273 .Else(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(target, &MyPromiseTarget::DecrementCounter));
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
274
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
275 testCounter = 0;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
276 source.CompleteSomethingAsyncWithFailure(15);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
277 ASSERT_EQ(-15, testCounter);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
278 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
279
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
280 TEST(MessageBroker2, TestPromiseDeleteTarget)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
281 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
282 MessageBroker broker;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
283 MyPromiseSource source(broker);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
284 MyPromiseTarget* target = new MyPromiseTarget(broker);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
285
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
286 // create the promise
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
287 source.StartSomethingAsync()
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
288 .Then(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(*target, &MyPromiseTarget::IncrementCounter))
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
289 .Else(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(*target, &MyPromiseTarget::DecrementCounter));
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
290
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
291 // delete the promise target
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
292 delete target;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
293
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
294 // trigger the promise, make sure it does not throw and does not call the callback
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
295 testCounter = 0;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
296 source.CompleteSomethingAsyncWithSuccess(10);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
297 ASSERT_EQ(0, testCounter);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
298
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
299 // test a failing promise
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
300 source.StartSomethingAsync()
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
301 .Then(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(*target, &MyPromiseTarget::IncrementCounter))
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
302 .Else(new Callable<MyPromiseTarget, MyPromiseSource::MyPromiseMessage>(*target, &MyPromiseTarget::DecrementCounter));
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
303
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
304 testCounter = 0;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
305 source.CompleteSomethingAsyncWithFailure(15);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
306 ASSERT_EQ(0, testCounter);
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
307 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff changeset
308
323
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
309 #if __cplusplus >= 201103L
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
310
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
311 #include <functional>
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
312
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
313 namespace OrthancStone {
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
314
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
315 template <typename TMessage>
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
316 class LambdaCallable : public MessageHandler<TMessage>
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
317 {
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
318 private:
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
319
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
320 IObserver& observer_;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
321 std::function<void (const TMessage&)> lambda_;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
322
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
323 public:
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
324 LambdaCallable(IObserver& observer,
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
325 std::function<void (const TMessage&)> lambdaFunction) :
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
326 observer_(observer),
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
327 lambda_(lambdaFunction)
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
328 {
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
329 }
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
330
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
331 virtual void Apply(const IMessage& message)
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
332 {
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
333 lambda_(dynamic_cast<const TMessage&>(message));
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
334 }
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
335
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
336 virtual MessageType GetMessageType() const
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
337 {
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
338 return static_cast<MessageType>(TMessage::Type);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
339 }
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
340
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
341 virtual IObserver* GetObserver() const
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
342 {
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
343 return &observer_;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
344 }
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
345 };
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
346
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
347
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
348 }
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
349
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
350 TEST(MessageBroker2, TestLambdaSimpleUseCase)
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
351 {
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
352 MessageBroker broker;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
353 MyObservable observable(broker);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
354 MyObserver* observer = new MyObserver(broker);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
355
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
356 // create a permanent connection between an observable and an observer
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
357 observable.RegisterObserverCallback(new LambdaCallable<MyObservable::MyCustomMessage>(*observer, [&](const MyObservable::MyCustomMessage& message) {testCounter += 2 * message.payload_;}));
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
358
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
359 testCounter = 0;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
360 observable.EmitMessage(MyObservable::MyCustomMessage(12));
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
361 ASSERT_EQ(24, testCounter);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
362
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
363 // delete the observer and check that the callback is not called anymore
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
364 delete observer;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
365
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
366 // the connection is permanent; if we emit the same message again, the observer will be notified again
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
367 testCounter = 0;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
368 observable.EmitMessage(MyObservable::MyCustomMessage(20));
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
369 ASSERT_EQ(0, testCounter);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
370 }
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
371
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
372 namespace {
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
373 class MyObserverWithLambda : public IObserver {
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
374 private:
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
375 int multiplier_; // this is a private variable we want to access in a lambda
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
376
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
377 public:
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
378 MyObserverWithLambda(MessageBroker& broker, int multiplier, MyObservable& observable)
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
379 : IObserver(broker),
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
380 multiplier_(multiplier)
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
381 {
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
382 // register a callable to a lambda that access private members
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
383 observable.RegisterObserverCallback(new LambdaCallable<MyObservable::MyCustomMessage>(*this, [this](const MyObservable::MyCustomMessage& message) {
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
384 testCounter += multiplier_ * message.payload_;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
385 }));
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
386
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
387 }
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
388 };
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
389 }
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
390
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
391 TEST(MessageBroker2, TestLambdaCaptureThisAndAccessPrivateMembers)
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
392 {
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
393 MessageBroker broker;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
394 MyObservable observable(broker);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
395 MyObserverWithLambda* observer = new MyObserverWithLambda(broker, 3, observable);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
396
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
397 testCounter = 0;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
398 observable.EmitMessage(MyObservable::MyCustomMessage(12));
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
399 ASSERT_EQ(36, testCounter);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
400
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
401 // delete the observer and check that the callback is not called anymore
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
402 delete observer;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
403
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
404 // the connection is permanent; if we emit the same message again, the observer will be notified again
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
405 testCounter = 0;
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
406 observable.EmitMessage(MyObservable::MyCustomMessage(20));
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
407 ASSERT_EQ(0, testCounter);
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
408 }
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
409
dbfe2e9e5020 playing with LambdaCallable
am@osimis.io
parents: 302
diff changeset
410 #endif // C++ 11