summaryrefslogtreecommitdiff
path: root/org.genivi.commonapi.core.verification/src/AFSelective.cpp
blob: dc192dba63cfa312b7b7bbe15c8ebe57c9bafe95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Copyright (C) 2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

/**
* @file advanced/selective
*/

#include <functional>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <fstream>
#include <gtest/gtest.h>
#include "CommonAPI/CommonAPI.hpp"
#include "v1/commonapi/advanced/bselective/TestInterfaceProxy.hpp"
#include "stub/AFSelectiveStub.h"

const std::string serviceId = "service-sample";
const std::string clientId = "client-sample";
const std::string otherclientId = "other-client-sample";

const std::string domain = "local";
const std::string testAddress = "commonapi.advanced.bselective.TestInterface";
const int tasync = 100000;

using namespace v1_0::commonapi::advanced::bselective;

class Environment: public ::testing::Environment {
public:
    virtual ~Environment() {
    }

    virtual void SetUp() {
    }

    virtual void TearDown() {
    }
};

class AFSelective: public ::testing::Test {

public:

    void recvSubscribedValue(uint8_t y) {
        value_ = y;
    }

protected:
    void SetUp() {
        runtime_ = CommonAPI::Runtime::get();
        ASSERT_TRUE((bool)runtime_);
        std::mutex availabilityMutex;
        std::mutex availabilityMutex2;
        std::unique_lock<std::mutex> lock(availabilityMutex);
        std::unique_lock<std::mutex> lock2(availabilityMutex2);
        std::condition_variable cv;
        std::condition_variable cv2;        
        bool proxyAvailable = false;
        bool proxy2Available = false;

        std::thread t1([this, &proxyAvailable, &cv, &availabilityMutex]() {
            std::lock_guard<std::mutex> lock(availabilityMutex);
            testProxy_ = runtime_->buildProxy<TestInterfaceProxy>(domain, testAddress, clientId);
            testProxy_->isAvailableBlocking();
            ASSERT_TRUE((bool)testProxy_);
            proxyAvailable = true;
            cv.notify_one();
        });
        
        std::thread t2([this, &proxy2Available, &cv2, &availabilityMutex2]() {
            std::lock_guard<std::mutex> lock2(availabilityMutex2);
            testProxy2_ = runtime_->buildProxy<TestInterfaceProxy>(domain, testAddress, otherclientId);
            testProxy2_->isAvailableBlocking();
            ASSERT_TRUE((bool)testProxy2_);
            proxy2Available = true;
            cv2.notify_one();
        });        
        
        testStub_ = std::make_shared<AFSelectiveStub>();
        bool serviceRegistered = runtime_->registerService(domain, testAddress, testStub_, serviceId);
        ASSERT_TRUE(serviceRegistered);

        while(!proxyAvailable) {
            cv.wait(lock);
        }
        t1.join();
        ASSERT_TRUE(testProxy_->isAvailable());
        
        while(!proxy2Available) {
            cv2.wait(lock2);
        }
        t2.join();
        ASSERT_TRUE(testProxy2_->isAvailable());        
    }

    void TearDown() {
        bool serviceUnregistered =
                runtime_->unregisterService(domain, AFSelectiveStub::StubInterface::getInterface(),
                        testAddress);

         ASSERT_TRUE(serviceUnregistered);

         // wait that proxies are not available
         int counter = 0;  // counter for avoiding endless loop
         while ( testProxy_->isAvailable() && counter < 10 ) {
             std::this_thread::sleep_for(std::chrono::milliseconds(100));
             counter++;
         }
         ASSERT_FALSE(testProxy_->isAvailable());

         counter = 0;  // counter for avoiding endless loop
         while ( testProxy2_->isAvailable() && counter < 10 ) {
             std::this_thread::sleep_for(std::chrono::milliseconds(100));
             counter++;
         }
         ASSERT_FALSE(testProxy2_->isAvailable());
    }

    uint8_t value_;
    std::shared_ptr<CommonAPI::Runtime> runtime_;
    std::shared_ptr<AFSelectiveStub> testStub_;
    std::shared_ptr<TestInterfaceProxy<>> testProxy_;
    std::shared_ptr<TestInterfaceProxy<>> testProxy2_;        
};


/**
* @test Test selective broadcasts.
*  - inform stub to stop accepting subscriptions
*  - try to subscribe to the selective broadcast
*  - check that an error was received
*  - inform stub to send a broadcast
*  - check that nothing was received in a reasonable time
*/
TEST_F(AFSelective, SelectiveBroadcastRejected) {

    CommonAPI::CallStatus callStatus;
    CommonAPI::CallStatus subStatus;
    uint8_t result = 0;
    uint8_t in_ = 0;
    uint8_t out_ = 0;
       
    // send value '2' via a method call - this tells stub to stop accepting subs
    in_ = 2;
    testProxy_->testMethod(in_, callStatus, out_);    
    
    // subscribe
    subStatus = CommonAPI::CallStatus::UNKNOWN;
    testProxy_->getBTestSelectiveSelectiveEvent().subscribe([&](
        const uint8_t &y
    ) {
        result = y;
    },
    [&](
        const CommonAPI::CallStatus &status
    ) {
        subStatus = status;
    });    
    
    // check that subscription failed correctly
    for (int i = 0; i < 100; i++) {
        if (subStatus != CommonAPI::CallStatus::UNKNOWN) break;
        usleep(10000);
    }
    // EXPECT_EQ(subStatus, CommonAPI::CallStatus::SUBSCRIPTION_REFUSED); // Not supported by SOME/IP yet.    

    // send value '3' via a method call - this tells stub to broadcast through the selective bc
    result = 0;
    in_ = 3;
    out_ = 0;   
    testProxy_->testMethod(in_, callStatus, out_);  
    
    // check that no value was correctly received
    for (int i = 0; i < 100; i++) {
        if (result != 0) break;
        usleep(10000);
    }
    uint8_t expected = 0;
    EXPECT_EQ(expected, result);

}

/**
* @test Test selective broadcasts.
*  - inform stub to start accepting subscriptions
*  - subscribe to the selective broadcast
*  - check that no error was received (in a reasonable time)
*  - inform stub to send a broadcast
*  - check that a correct value is received
*/
TEST_F(AFSelective, SelectiveBroadcast) {

    CommonAPI::CallStatus callStatus;
    CommonAPI::CallStatus subStatus;
    uint8_t result = 0;
    uint8_t in_ = 0;
    uint8_t out_ = 0;

    // send value '4' via a method call - this tells stub to start accepting subs
    in_ = 4;   
    testProxy_->testMethod(in_, callStatus, out_);
    
    // subscribe
    subStatus = CommonAPI::CallStatus::UNKNOWN;
    testProxy_->getBTestSelectiveSelectiveEvent().subscribe([&](
        const uint8_t &y
    ) {
        result = y;
    },
    [&](
        const CommonAPI::CallStatus &status
    ) {
        subStatus = status;
    });
    
    // check that no error was received
    for (int i = 0; i < 100; i++) {
        if (subStatus != CommonAPI::CallStatus::UNKNOWN) break;
        usleep(10000);
    }
    EXPECT_EQ(CommonAPI::CallStatus::UNKNOWN, subStatus);
    
    // send value '3' via a method call - this tells stub to broadcast through the selective bc
    result = 0;
    in_ = 3;
    out_ = 0;   
    testProxy_->testMethod(in_, callStatus, out_);

    // check that value was correctly received
    for (int i = 0; i < 100; i++) {
        if (result != 0) break;
        usleep(10000);
    }
    uint8_t expected = 1;
    EXPECT_EQ(expected, result);
}

/**
* @test Test multiple selective broadcasts.
*  - inform stub to start accepting subscriptions
*  - subscribe to the selective broadcast
*  - check that no error was received (in a reasonable time)
*  - inform stub to send a broadcast
*  - check that a correct value is received
*/
TEST_F(AFSelective, SelectiveMultiBroadcast) {

    CommonAPI::CallStatus callStatus;
    CommonAPI::CallStatus subStatus;
    uint8_t result = 0;
    uint8_t result2 = 0;    
    uint8_t in_ = 0;
    uint8_t out_ = 0;

    // send value '4' via a method call - this tells stub to start accepting subs
    in_ = 4;   
    testProxy_->testMethod(in_, callStatus, out_);
    
    // subscribe
    subStatus = CommonAPI::CallStatus::UNKNOWN;
    testProxy_->getBTestSelectiveSelectiveEvent().subscribe([&](
        const uint8_t &y
    ) {
        result = y;
    },
    [&](
        const CommonAPI::CallStatus &status
    ) {
        subStatus = status;
    });
    
    // check that no error was received
    for (int i = 0; i < 100; i++) {
        if (subStatus != CommonAPI::CallStatus::UNKNOWN) break;
        usleep(10000);
    }
    EXPECT_EQ(CommonAPI::CallStatus::UNKNOWN, subStatus);

    // subscribe from another proxy
    subStatus = CommonAPI::CallStatus::UNKNOWN;
    testProxy2_->getBTestSelectiveSelectiveEvent().subscribe([&](
        const uint8_t &y
    ) {
        result2 = y;
    },
    [&](
        const CommonAPI::CallStatus &status
    ) {
        subStatus = status;
    });
    
    // check that no error was received
    for (int i = 0; i < 100; i++) {
        if (subStatus != CommonAPI::CallStatus::UNKNOWN) break;
        usleep(10000);
    }
    EXPECT_EQ(CommonAPI::CallStatus::UNKNOWN, subStatus);
    
    // send value '3' via a method call - this tells stub to broadcast through the selective bc
    result = 0;
    in_ = 3;
    out_ = 0;   
    testProxy_->testMethod(in_, callStatus, out_);

    // check that value was correctly received
    for (int i = 0; i < 100; i++) {
        if (result != 0) break;
        usleep(10000);
    }
    uint8_t expected = 1;
    EXPECT_EQ(expected, result);

    // check that value was correctly received
    for (int i = 0; i < 100; i++) {
        if (result2 != 0) break;
        usleep(10000);
    }
    uint8_t expected2 = 1;
    EXPECT_EQ(expected2, result2);
}

/**
* @test Test multiple selective broadcasts, with rejection.
*  - subscribe to stub three times:
*    once from proxy2,
*    once from proxy1 (accepted)
*    once from proxy2 (rejected)
*  - This should result with two subscription callbacks being called from broadcast.
*/

/* 
 * This test currently fails because a single proxy can only subscribe once.
 * When proxy subscribes the second time, the stub is not notified.
 * Therefore, whatever the stub did with the first subscription (rejected / accepted)
 * will also hold for the second subscription.
 */
 
TEST_F(AFSelective, DISABLED_SelectiveRejectedMultiBroadcast) {

    CommonAPI::CallStatus callStatus;
    CommonAPI::CallStatus subStatus;
    uint8_t result = 0;
    uint8_t result2 = 0;
    uint8_t result3 = 0;     
    uint8_t in_ = 0;
    uint8_t out_ = 0;

    // send value '4' via a method call - this tells stub to start accepting subs
    in_ = 4;   
    testProxy_->testMethod(in_, callStatus, out_);
    
    // subscribe
    subStatus = CommonAPI::CallStatus::UNKNOWN;
    testProxy_->getBTestSelectiveSelectiveEvent().subscribe([&](
        const uint8_t &y
    ) {
        result = y;
    },
    [&](
        const CommonAPI::CallStatus &status
    ) {
        subStatus = status;
    });
    
    // check that no error was received
    for (int i = 0; i < 100; i++) {
        if (subStatus != CommonAPI::CallStatus::UNKNOWN) break;
        usleep(10000);
    }
    EXPECT_EQ(CommonAPI::CallStatus::UNKNOWN, subStatus);

    // subscribe from another proxy
    subStatus = CommonAPI::CallStatus::UNKNOWN;
    testProxy2_->getBTestSelectiveSelectiveEvent().subscribe([&](
        const uint8_t &y
    ) {
        result3 = y;
    },
    [&](
        const CommonAPI::CallStatus &status
    ) {
        subStatus = status;
    });
    
    // check that no error was received
    for (int i = 0; i < 100; i++) {
        if (subStatus != CommonAPI::CallStatus::UNKNOWN) break;
        usleep(10000);
    }
    EXPECT_EQ(CommonAPI::CallStatus::UNKNOWN, subStatus);
    
    // send value '2' via a method call - this tells stub to stop accepting subs
    in_ = 2;
    testProxy2_->testMethod(in_, callStatus, out_);    
    
    // subscribe
    subStatus = CommonAPI::CallStatus::UNKNOWN;
    testProxy2_->getBTestSelectiveSelectiveEvent().subscribe([&](
        const uint8_t &y
    ) {
        result2 = y;
    },
    [&](
        const CommonAPI::CallStatus &status
    ) {
        subStatus = status;
    });    
    
    // check that subscription failed correctly
    for (int i = 0; i < 100; i++) {
        if (subStatus != CommonAPI::CallStatus::UNKNOWN) break;
        usleep(10000);
    }
    // EXPECT_EQ(subStatus, CommonAPI::CallStatus::SUBSCRIPTION_REFUSED); // Not supported by SOME/IP yet.    

    
    // send value '3' via a method call - this tells stub to broadcast through the selective bc
    result = 0;
    in_ = 3;
    out_ = 0;   
    testProxy_->testMethod(in_, callStatus, out_);

    // check that value was correctly received
    for (int i = 0; i < 100; i++) {
        if (result != 0) break;
        usleep(10000);
    }
    uint8_t expected = 1;
    EXPECT_EQ(expected, result);
    uint8_t expected2 = 0;
    EXPECT_EQ(expected2, result2);
    uint8_t expected3 = 1;
    EXPECT_EQ(expected3, result3);
}

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    ::testing::AddGlobalTestEnvironment(new Environment());
    return RUN_ALL_TESTS();
}