-
Notifications
You must be signed in to change notification settings - Fork 86
/
bundle_context_services_test.cpp
1718 lines (1403 loc) · 62.1 KB
/
bundle_context_services_test.cpp
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <gtest/gtest.h>
#include <thread>
#include <chrono>
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <string.h>
#include <future>
#include "celix_api.h"
#include "celix_framework_factory.h"
#include "celix_service_factory.h"
#include "service_tracker_private.h"
class CelixBundleContextServicesTests : public ::testing::Test {
public:
celix_framework_t* fw = nullptr;
celix_bundle_context_t *ctx = nullptr;
celix_properties_t *properties = nullptr;
const char * const CMP_TEST_BND_LOC = "" CMP_TEST_BUNDLE_LOC "";
CelixBundleContextServicesTests() {
properties = celix_properties_create();
celix_properties_set(properties, "LOGHELPER_ENABLE_STDOUT_FALLBACK", "true");
celix_properties_set(properties, "org.osgi.framework.storage.clean", "onFirstInit");
celix_properties_set(properties, "org.osgi.framework.storage", ".cacheBundleContextTestFramework");
celix_properties_set(properties, "CELIX_LOGGING_DEFAULT_ACTIVE_LOG_LEVEL", "trace");
celix_properties_setLong(properties, CELIX_FRAMEWORK_STATIC_EVENT_QUEUE_SIZE, 256); //ensure that the floodEventLoopTest overflows the static event queue size
fw = celix_frameworkFactory_createFramework(properties);
ctx = framework_getContext(fw);
}
~CelixBundleContextServicesTests() override {
celix_frameworkFactory_destroyFramework(fw);
}
CelixBundleContextServicesTests(CelixBundleContextServicesTests&&) = delete;
CelixBundleContextServicesTests(const CelixBundleContextServicesTests&) = delete;
CelixBundleContextServicesTests& operator=(CelixBundleContextServicesTests&&) = delete;
CelixBundleContextServicesTests& operator=(const CelixBundleContextServicesTests&) = delete;
void registerAndUseServiceWithCorrectVersion(bool direct) {
struct calc {
int (*calc)(int);
};
const char *calcName = "calc";
struct calc svc;
svc.calc = [](int n) -> int {
return n * 42;
};
celix_service_use_options_t use_opts{};
use_opts.filter.serviceName = "calc";
use_opts.filter.versionRange = "[1,2)";
use_opts.direct = direct;
celix_service_registration_options_t reg_opts{};
reg_opts.serviceName = calcName;
reg_opts.serviceVersion = "1.5";
reg_opts.svc = &svc;
bool called = celix_bundleContext_useServiceWithOptions(ctx, &use_opts);
ASSERT_TRUE(!called); //service not avail.
std::future<bool> result{std::async([&]{
use_opts.waitTimeoutInSeconds = 5.0;
printf("Trying to call calc with timeout of %f\n", use_opts.waitTimeoutInSeconds);
bool calledAsync = celix_bundleContext_useServiceWithOptions(ctx, &use_opts);
printf("returned from use service with timeout. calc called %s.\n", calledAsync ? "true" : "false");
return calledAsync;
})};
long svcId = celix_bundleContext_registerServiceWithOptions(ctx, ®_opts);
ASSERT_TRUE(svcId >= 0);
ASSERT_TRUE(result.get()); //should return true after waiting for the registered service.
celix_bundleContext_unregisterService(ctx, svcId);
}
void registerAndUseServiceWithIncorrectVersion(bool direct) {
struct calc {
int (*calc)(int);
};
const char *calcName = "calc";
struct calc svc;
svc.calc = [](int n) -> int {
return n * 42;
};
celix_service_use_options_t use_opts{};
use_opts.filter.serviceName = "calc";
use_opts.filter.versionRange = "[2,3)";
use_opts.direct = direct;
celix_service_registration_options_t reg_opts{};
reg_opts.serviceName = calcName;
reg_opts.serviceVersion = "1.5";
reg_opts.svc = &svc;
bool called = celix_bundleContext_useServiceWithOptions(ctx, &use_opts);
ASSERT_TRUE(!called); //service not avail.
std::future<bool> result{std::async([&]{
use_opts.waitTimeoutInSeconds = 1.0;
printf("Trying to call calc with timeout of %f\n", use_opts.waitTimeoutInSeconds);
bool calledAsync = celix_bundleContext_useServiceWithOptions(ctx, &use_opts);
printf("returned from use service with timeout. calc called %s.\n", calledAsync ? "true" : "false");
return calledAsync;
})};
long svcId = celix_bundleContext_registerServiceWithOptions(ctx, ®_opts);
ASSERT_TRUE(svcId >= 0);
ASSERT_TRUE(!result.get());
celix_bundleContext_unregisterService(ctx, svcId);
}
void registerAndUseServiceWithTimeout(bool direct) {
const int NR_ITERATIONS = 5; //NOTE this test is sensitive for triggering race condition in the celix framework, therefore is used a few times.
for (int i = 0; i < NR_ITERATIONS; ++i) {
printf("Iter %i\n", i);
struct calc {
int (*calc)(int);
};
const char *calcName = "calc";
struct calc svc;
svc.calc = [](int n) -> int {
return n * 42;
};
celix_service_use_options_t opts{};
opts.filter.serviceName = "calc";
opts.direct = direct;
bool called = celix_bundleContext_useServiceWithOptions(ctx, &opts);
EXPECT_FALSE(called); //service not avail.
std::future<bool> result{std::async([&] {
opts.waitTimeoutInSeconds = 2.0;
//printf("Trying to call calc with timeout of %f\n", opts.waitTimeoutInSeconds);
bool calledAsync = celix_bundleContext_useServiceWithOptions(ctx, &opts);
//printf("returned from use service with timeout. calc called %s.\n", calledAsync ? "true" : "false");
return calledAsync;
})};
long svcId = celix_bundleContext_registerService(ctx, &svc, calcName, nullptr);
EXPECT_TRUE(svcId >= 0);
EXPECT_TRUE(result.get()); //should return true after waiting for the registered service.
celix_bundleContext_unregisterService(ctx, svcId);
//check if timeout is triggered
std::future<bool> result2{std::async([&] {
opts.waitTimeoutInSeconds = 0.1;
//printf("Trying to call calc with timeout of %f\n", opts.waitTimeoutInSeconds);
bool calledAsync = celix_bundleContext_useServiceWithOptions(ctx, &opts);
//printf("returned from use service with timeout. calc called %s.\n", calledAsync ? "true" : "false");
return calledAsync;
})};
EXPECT_FALSE(result2.get()); //note service is away, so even with a wait the service is not found.
}
}
void registerAsyncAndUseServiceWithTimeout(bool direct) {
const int NR_ITERATIONS = 5; //NOTE this test is sensitive for triggering race condition in the celix framework, therefore is used a few times.
for (int i = 0; i < NR_ITERATIONS; ++i) {
printf("Iter %i\n", i);
struct calc {
int (*calc)(int);
};
const char *calcName = "calc";
struct calc svc;
svc.calc = [](int n) -> int {
return n * 42;
};
celix_service_use_options_t opts{};
opts.filter.serviceName = "calc";
opts.direct = direct;
bool called = celix_bundleContext_useServiceWithOptions(ctx, &opts);
EXPECT_FALSE(called); //service not avail.
std::future<bool> result{std::async([&] {
opts.waitTimeoutInSeconds = 2.0;
//printf("Trying to call calc with timeout of %f\n", opts.waitTimeoutInSeconds);
bool calledAsync = celix_bundleContext_useServiceWithOptions(ctx, &opts);
//printf("returned from use service with timeout. calc called %s.\n", calledAsync ? "true" : "false");
return calledAsync;
})};
long svcId = celix_bundleContext_registerServiceAsync(ctx, &svc, calcName, nullptr);
EXPECT_TRUE(svcId >= 0);
EXPECT_TRUE(result.get()); //should return true after waiting for the registered service.
celix_bundleContext_unregisterServiceAsync(ctx, svcId, nullptr, nullptr);
celix_bundleContext_waitForAsyncUnregistration(ctx, svcId);
//check if timeout is triggered
std::future<bool> result2{std::async([&] {
opts.waitTimeoutInSeconds = 0.1;
//printf("Trying to call calc with timeout of %f\n", opts.waitTimeoutInSeconds);
bool calledAsync = celix_bundleContext_useServiceWithOptions(ctx, &opts);
//printf("returned from use service with timeout. calc called %s.\n", calledAsync ? "true" : "false");
return calledAsync;
})};
EXPECT_FALSE(result2.get()); //note service is away, so even with a wait the service is not found.
}
}
};
TEST_F(CelixBundleContextServicesTests, registerService) {
struct calc {
int (*calc)(int);
};
const char *calcName = "calc";
calc svc;
svc.calc = [](int n) -> int {
return n * 42;
};
long svcId = celix_bundleContext_registerService(ctx, &svc, calcName, nullptr);
ASSERT_TRUE(svcId >= 0);
celix_bundleContext_unregisterService(ctx, svcId);
};
TEST_F(CelixBundleContextServicesTests, registerServiceAsync) {
struct calc {
int (*calc)(int);
};
const char *calcName = "calc";
calc svc;
svc.calc = [](int n) -> int {
return n * 42;
};
long svcId = celix_bundleContext_registerServiceAsync(ctx, &svc, calcName, nullptr);
ASSERT_TRUE(svcId >= 0);
celix_bundleContext_waitForAsyncRegistration(ctx, svcId);
ASSERT_GE(celix_bundleContext_findService(ctx, calcName), 0L);
celix_bundleContext_unregisterServiceAsync(ctx, svcId, nullptr, nullptr);
celix_bundleContext_waitForAsyncUnregistration(ctx, svcId);
ASSERT_LT(celix_bundleContext_findService(ctx, calcName), 0L);
};
TEST_F(CelixBundleContextServicesTests, incorrectUnregisterCalls) {
celix_bundleContext_unregisterService(ctx, 1);
celix_bundleContext_unregisterService(ctx, 2);
celix_bundleContext_unregisterService(ctx, -1);
celix_bundleContext_unregisterService(ctx, -2);
};
TEST_F(CelixBundleContextServicesTests, incorrectAsyncUnregisterCalls) {
celix_bundleContext_unregisterServiceAsync(ctx, 1, nullptr, nullptr);
celix_bundleContext_unregisterServiceAsync(ctx, 2, nullptr, nullptr);
celix_bundleContext_unregisterServiceAsync(ctx, -1, nullptr, nullptr);
celix_bundleContext_unregisterServiceAsync(ctx, -2, nullptr, nullptr);
};
TEST_F(CelixBundleContextServicesTests, UseServicesWithoutName) {
struct calc {
int (*calc)(int);
};
const char *calcName = "calc";
struct calc svc;
svc.calc = [](int n) -> int {
return n * 42;
};
long svcId1 = celix_bundleContext_registerService(ctx, &svc, calcName, nullptr);
EXPECT_TRUE(svcId1 >= 0);
auto use = [](void *handle, void *svc) {
EXPECT_TRUE(svc != nullptr);
int *total = static_cast<int*>(handle);
struct calc *calc = static_cast<struct calc*>(svc);
int tmp = calc->calc(1);
*total += tmp;
};
int total = 0;
auto count = celix_bundleContext_useServices(ctx, nullptr, &total, use);
EXPECT_EQ(0, count);
count = celix_bundleContext_useServices(ctx, calcName, &total, use);
EXPECT_EQ(1, count);
bool called = celix_bundleContext_useService(ctx, nullptr, &total, use);
EXPECT_FALSE(called);
called = celix_bundleContext_useService(ctx, calcName, &total, use);
EXPECT_TRUE(called);
called = celix_bundleContext_useServiceWithId(ctx, svcId1, nullptr, &total, use);
EXPECT_FALSE(called);
celix_service_use_options_t use_opts{};
use_opts.filter.serviceName = nullptr;
use_opts.filter.versionRange = nullptr;
use_opts.callbackHandle = &total;
use_opts.use = use;
called = celix_bundleContext_useServiceWithOptions(ctx, &use_opts);
ASSERT_FALSE(called);
count = celix_bundleContext_useServicesWithOptions(ctx, &use_opts);
EXPECT_EQ(0, count);
use_opts.filter.serviceName = calcName;
called = celix_bundleContext_useServiceWithOptions(ctx, &use_opts);
ASSERT_TRUE(called);
count = celix_bundleContext_useServicesWithOptions(ctx, &use_opts);
EXPECT_EQ(1, count);
celix_bundleContext_unregisterService(ctx, svcId1);
}
TEST_F(CelixBundleContextServicesTests, registerMultipleAndUseServices) {
struct calc {
int (*calc)(int);
};
const char *calcName = "calc";
struct calc svc;
svc.calc = [](int n) -> int {
return n * 42;
};
long svcId1 = celix_bundleContext_registerService(ctx, &svc, calcName, nullptr);
EXPECT_TRUE(svcId1 >= 0);
long svcId2 = celix_bundleContext_registerService(ctx, &svc, calcName, nullptr);
EXPECT_TRUE(svcId2 >= 0);
long svcId3 = celix_bundleContext_registerService(ctx, &svc, calcName, nullptr);
EXPECT_TRUE(svcId3 >= 0);
auto use = [](void *handle, void *svc) {
EXPECT_TRUE(svc != nullptr);
int *total = static_cast<int*>(handle);
struct calc *calc = static_cast<struct calc*>(svc);
int tmp = calc->calc(1);
*total += tmp;
};
int total = 0;
auto count = celix_bundleContext_useServices(ctx, "calc", &total, use);
EXPECT_EQ(3, count);
EXPECT_EQ(42 * 3, total);
celix_service_use_options_t use_opts{};
use_opts.filter.serviceName = calcName;
use_opts.filter.versionRange = nullptr;
use_opts.callbackHandle = &total;
use_opts.use = use;
use_opts.direct = false;
total = 0;
count = celix_bundleContext_useServicesWithOptions(ctx, &use_opts);
EXPECT_EQ(3, count);
EXPECT_EQ(42 * 3, total);
use_opts.direct = true;
total = 0;
count = celix_bundleContext_useServicesWithOptions(ctx, &use_opts);
EXPECT_EQ(3, count);
EXPECT_EQ(42 * 3, total);
celix_bundleContext_unregisterService(ctx, svcId3);
total = 0;
count = celix_bundleContext_useServices(ctx, "calc", &total, use);
EXPECT_EQ(2, count);
EXPECT_EQ(42 * 2, total);
use_opts.direct = false;
total = 0;
count = celix_bundleContext_useServicesWithOptions(ctx, &use_opts);
EXPECT_EQ(2, count);
EXPECT_EQ(42 * 2, total);
use_opts.direct = true;
total = 0;
count = celix_bundleContext_useServicesWithOptions(ctx, &use_opts);
EXPECT_EQ(2, count);
EXPECT_EQ(42 * 2, total);
total = 0;
bool called = celix_bundleContext_useService(ctx, "calc", &total, use);
EXPECT_TRUE(called);
EXPECT_EQ(42, total);
celix_bundleContext_unregisterService(ctx, svcId1);
celix_bundleContext_unregisterService(ctx, svcId2);
//NOTE superfluous (note prints errors)
celix_bundleContext_unregisterService(ctx, svcId1);
celix_bundleContext_unregisterService(ctx, svcId2);
};
TEST_F(CelixBundleContextServicesTests, useServiceInUseCallback) {
struct calc {
int (*calc)(int);
};
const char *calcName = "calc";
struct calc svc;
svc.calc = [](int n) -> int {
return n * 42;
};
long svcId1 = celix_bundleContext_registerService(ctx, &svc, calcName, nullptr);
EXPECT_TRUE(svcId1 >= 0);
long svcId2 = celix_bundleContext_registerService(ctx, &svc, calcName, nullptr);
EXPECT_TRUE(svcId2 >= 0);
long svcId3 = celix_bundleContext_registerService(ctx, &svc, calcName, nullptr);
EXPECT_TRUE(svcId3 >= 0);
auto use = [](void *handle, void *svc) {
celix_bundle_context_t *ctx = static_cast<celix_bundle_context_t *>(handle);
celix_service_use_options_t opts{};
opts.filter.serviceName = "calc";
opts.callbackHandle = svc; //avoid warning
opts.use = nullptr;
size_t count = celix_bundleContext_useServicesWithOptions(ctx, &opts);
EXPECT_EQ(3, count);
};
celix_service_use_options_t opts{};
opts.filter.serviceName = calcName;
opts.callbackHandle = ctx;
opts.use = use;
opts.direct = false;
bool called = celix_bundleContext_useServiceWithOptions(ctx, &opts);
EXPECT_TRUE(called);
celix_bundleContext_unregisterService(ctx, svcId1);
celix_bundleContext_unregisterService(ctx, svcId2);
celix_bundleContext_unregisterService(ctx, svcId3);
}
TEST_F(CelixBundleContextServicesTests, registerAndUseService) {
struct calc {
int (*calc)(int);
};
const char *calcName = "calc";
struct calc svc;
svc.calc = [](int n) -> int {
return n * 42;
};
long svcId = celix_bundleContext_registerService(ctx, &svc, calcName, nullptr);
ASSERT_TRUE(svcId >= 0);
int result = 0;
bool called = celix_bundleContext_useServiceWithId(ctx, svcId, calcName, &result, [](void *handle, void *svc) {
ASSERT_TRUE(svc != nullptr);
int *result = static_cast<int*>(handle);
struct calc *calc = static_cast<struct calc*>(svc);
int tmp = calc->calc(2);
*result = tmp;
});
ASSERT_TRUE(called);
ASSERT_EQ(84, result);
result = 0;
long nonExistingSvcId = 101;
called = celix_bundleContext_useServiceWithId(ctx, nonExistingSvcId, calcName, &result, [](void *handle, void *svc) {
int *result = static_cast<int*>(handle);
struct calc *calc = static_cast<struct calc*>(svc);
int tmp = calc->calc(2);
*result = tmp;
});
ASSERT_TRUE(!called);
ASSERT_EQ(0, result); //e.g. not called
celix_bundleContext_unregisterService(ctx, svcId);
};
TEST_F(CelixBundleContextServicesTests, registerAndUseServiceWithTimeout) {
registerAndUseServiceWithTimeout(false);
}
TEST_F(CelixBundleContextServicesTests, registerAndUseServiceDirectWithTimeout) {
registerAndUseServiceWithTimeout(true);
}
TEST_F(CelixBundleContextServicesTests, registerAsyncAndUseServiceWithTimeout) {
registerAsyncAndUseServiceWithTimeout(false);
}
TEST_F(CelixBundleContextServicesTests, registerAsyncAndUseServiceDirectWithTimeout) {
registerAsyncAndUseServiceWithTimeout(true);
}
TEST_F(CelixBundleContextServicesTests, registerAndUseServiceWithCorrectVersion) {
registerAndUseServiceWithCorrectVersion(false);
}
TEST_F(CelixBundleContextServicesTests, registerAndUseServiceDirectWithCorrectVersion) {
registerAndUseServiceWithCorrectVersion(true);
}
TEST_F(CelixBundleContextServicesTests, registerAndUseServiceWithIncorrectVersion) {
registerAndUseServiceWithIncorrectVersion(false);
}
TEST_F(CelixBundleContextServicesTests, registerAndUseServiceDirectWithIncorrectVersion) {
registerAndUseServiceWithIncorrectVersion(true);
}
TEST_F(CelixBundleContextServicesTests, registerAndUseWithForcedRaceCondition) {
struct calc {
int (*calc)(int);
};
const char *calcName = "calc";
struct calc svc;
svc.calc = [](int n) -> int {
return n * 42;
};
long svcId = celix_bundleContext_registerService(ctx, &svc, calcName, nullptr);
ASSERT_TRUE(svcId >= 0);
struct sync {
std::mutex mutex{};
std::condition_variable sync{};
bool inUseCall{false};
bool readyToExitUseCall{false};
bool unregister{false};
int result{0};
};
struct sync callInfo{};
auto use = [](void *handle, void *svc) {
ASSERT_TRUE(svc != nullptr);
struct sync *h = static_cast<struct sync*>(handle);
std::cout << "setting isUseCall to true and syncing on readyToExitUseCall" << std::endl;
std::unique_lock<std::mutex> lock(h->mutex);
h->inUseCall = true;
h->sync.notify_all();
h->sync.wait(lock, [h]{return h->readyToExitUseCall;});
lock.unlock();
std::cout << "Calling calc " << std::endl;
struct calc *calc = static_cast<struct calc *>(svc);
int tmp = calc->calc(2);
h->result = tmp;
};
auto call = [&] {
bool called = celix_bundleContext_useServiceWithId(ctx, svcId, calcName, &callInfo, use);
ASSERT_TRUE(called);
ASSERT_EQ(84, callInfo.result);
};
std::thread useThread{call};
std::thread unregisterThread{[&]{
std::cout << "syncing to wait if use function is called ..." << std::endl;
std::unique_lock<std::mutex> lock(callInfo.mutex);
callInfo.sync.wait(lock, [&]{return callInfo.inUseCall;});
lock.unlock();
std::cout << "trying to unregister ..." << std::endl;
celix_bundleContext_unregisterService(ctx, svcId);
std::cout << "done unregistering" << std::endl;
}};
//sleep 100 milli to give unregister a change to sink in
std::cout << "before sleep" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::cout << "after sleep" << std::endl;
std::cout << "setting readyToExitUseCall and notify" << std::endl;
std::unique_lock<std::mutex> lock(callInfo.mutex);
callInfo.readyToExitUseCall = true;
lock.unlock();
callInfo.sync.notify_all();
useThread.join();
std::cout << "use thread joined" << std::endl;
unregisterThread.join();
std::cout << "unregister thread joined" << std::endl;
};
TEST_F(CelixBundleContextServicesTests, servicesTrackerTest) {
int count = 0;
auto add = [](void *handle, void *svc) {
ASSERT_TRUE(svc != nullptr);
int *c = static_cast<int*>(handle);
*c += 1;
};
auto remove = [](void *handle, void *svc) {
ASSERT_TRUE(svc != nullptr);
int *c = static_cast<int*>(handle);
*c -= 1;
};
long trackerId = celix_bundleContext_trackServices(ctx, "calc", &count, add, remove);
ASSERT_TRUE(trackerId >= 0);
ASSERT_EQ(0, count);
long svcId1 = celix_bundleContext_registerService(ctx, (void*)0x100, "calc", nullptr);
ASSERT_TRUE(svcId1 >= 0);
ASSERT_EQ(1, count);
long svcId2 = celix_bundleContext_registerService(ctx, (void*)0x200, "calc", nullptr);
ASSERT_TRUE(svcId2 >= 0);
ASSERT_EQ(2, count);
celix_bundleContext_unregisterService(ctx, svcId1);
ASSERT_EQ(1, count);
celix_bundleContext_stopTracker(ctx, trackerId);
celix_bundleContext_unregisterService(ctx, svcId2);
}
TEST_F(CelixBundleContextServicesTests, servicesTrackerTestAsync) {
std::atomic<int> count {0};
auto add = [](void *handle, void *svc) {
ASSERT_TRUE(svc != nullptr);
auto *c = static_cast<std::atomic<int>*>(handle);
*c += 1;
};
auto remove = [](void *handle, void *svc) {
ASSERT_TRUE(svc != nullptr);
auto *c = static_cast<std::atomic<int>*>(handle);
*c -= 1;
};
long trackerId = celix_bundleContext_trackServicesAsync(ctx, "calc", &count, add, remove);
ASSERT_TRUE(trackerId >= 0);
ASSERT_EQ(0, count);
long svcId1 = celix_bundleContext_registerServiceAsync(ctx, (void*)0x100, "calc", nullptr);
celix_bundleContext_waitForAsyncRegistration(ctx, svcId1); //note also means the service tracker is done
ASSERT_TRUE(svcId1 >= 0);
ASSERT_EQ(1, count);
long svcId2 = celix_bundleContext_registerServiceAsync(ctx, (void*)0x200, "calc", nullptr);
celix_bundleContext_waitForAsyncRegistration(ctx, svcId2);
ASSERT_TRUE(svcId2 >= 0);
ASSERT_EQ(2, count);
celix_bundleContext_unregisterServiceAsync(ctx, svcId1, nullptr, nullptr);
celix_bundleContext_waitForAsyncUnregistration(ctx, svcId1);
ASSERT_EQ(1, count);
celix_bundleContext_stopTrackerAsync(ctx, trackerId, NULL, NULL);
celix_bundleContext_unregisterServiceAsync(ctx, svcId2, NULL, NULL);
celix_framework_waitForEmptyEventQueue(fw);
}
TEST_F(CelixBundleContextServicesTests, servicesTrackerInvalidArgsTest) {
long trackerId = celix_bundleContext_trackServices(nullptr, nullptr, nullptr, nullptr, nullptr);
ASSERT_TRUE(trackerId < 0); //required ctx missing
trackerId = celix_bundleContext_trackServices(ctx, "calc", nullptr, nullptr, nullptr);
ASSERT_TRUE(trackerId >= 0); //valid
celix_bundleContext_stopTracker(ctx, trackerId);
//opts
trackerId = celix_bundleContext_trackServicesWithOptions(nullptr, nullptr);
ASSERT_TRUE(trackerId < 0); //required ctx and opts missing
trackerId = celix_bundleContext_trackServicesWithOptions(ctx, nullptr);
ASSERT_TRUE(trackerId < 0); //required opts missing
celix_service_tracking_options_t opts{};
trackerId = celix_bundleContext_trackServicesWithOptions(ctx, &opts);
ASSERT_TRUE(trackerId >= 0); //valid with empty opts
celix_bundleContext_stopTracker(ctx, trackerId);
opts.filter.serviceName = "calc";
trackerId = celix_bundleContext_trackServicesWithOptions(ctx, &opts);
ASSERT_TRUE(trackerId >= 0); //valid
celix_bundleContext_stopTracker(ctx, trackerId);
}
TEST_F(CelixBundleContextServicesTests, servicesTrackerTestWithAlreadyRegisteredServices) {
int count = 0;
auto add = [](void *handle, void *svc) {
ASSERT_TRUE(svc != nullptr);
int *c = static_cast<int*>(handle);
*c += 1;
};
auto remove = [](void *handle, void *svc) {
ASSERT_TRUE(svc != nullptr);
int *c = static_cast<int*>(handle);
*c -= 1;
};
long svcId1 = celix_bundleContext_registerService(ctx, (void*)0x010, "calc", nullptr);
long svcId2 = celix_bundleContext_registerService(ctx, (void*)0x020, "calc", nullptr);
long trackerId = celix_bundleContext_trackServices(ctx, "calc", &count, add, remove);
ASSERT_TRUE(trackerId >= 0);
ASSERT_EQ(2, count);
long svcId3 = celix_bundleContext_registerService(ctx, (void*)0x100, "calc", nullptr);
ASSERT_TRUE(svcId1 >= 0);
ASSERT_EQ(3, count);
long svcId4 = celix_bundleContext_registerService(ctx, (void*)0x200, "calc", nullptr);
ASSERT_TRUE(svcId2 >= 0);
ASSERT_EQ(4, count);
celix_bundleContext_unregisterService(ctx, svcId1);
celix_bundleContext_unregisterService(ctx, svcId3);
ASSERT_EQ(2, count);
celix_bundleContext_stopTracker(ctx, trackerId);
celix_bundleContext_unregisterService(ctx, svcId2);
celix_bundleContext_unregisterService(ctx, svcId4);
}
TEST_F(CelixBundleContextServicesTests, servicesTrackerTestWithProperties) {
int count = 0;
auto add = [](void *handle, void *svc, const properties_t *props) {
ASSERT_TRUE(svc != nullptr);
ASSERT_TRUE(props != nullptr);
int *c = static_cast<int*>(handle);
*c += 1;
};
auto remove = [](void *handle, void *svc, const properties_t *props) {
ASSERT_TRUE(svc != nullptr);
ASSERT_TRUE(props != nullptr);
int *c = static_cast<int*>(handle);
*c -= 1;
};
long svcId1 = celix_bundleContext_registerService(ctx, (void*)0x100, "calc", nullptr);
celix_service_tracking_options_t opts{};
opts.filter.serviceName = "calc";
opts.callbackHandle = &count;
opts.addWithProperties = add;
opts.removeWithProperties = remove;
long trackerId = celix_bundleContext_trackServicesWithOptions(ctx, &opts);
ASSERT_TRUE(trackerId >= 0);
ASSERT_EQ(1, count);
long svcId2 = celix_bundleContext_registerService(ctx, (void*)0x200, "calc", nullptr);
ASSERT_TRUE(svcId1 >= 0);
ASSERT_EQ(2, count);
celix_bundleContext_unregisterService(ctx, svcId1);
celix_bundleContext_unregisterService(ctx, svcId2);
ASSERT_EQ(0, count);
celix_bundleContext_stopTracker(ctx, trackerId);
}
TEST_F(CelixBundleContextServicesTests, servicesTrackerTestWithOwner) {
int count = 0;
auto add = [](void *handle, void *svc, const properties_t *props, const bundle_t *svcOwner) {
ASSERT_TRUE(svc != nullptr);
ASSERT_TRUE(props != nullptr);
ASSERT_TRUE(celix_bundle_getId(svcOwner) >= 0);
int *c = static_cast<int*>(handle);
*c += 1;
};
auto remove = [](void *handle, void *svc, const properties_t *props, const bundle_t *svcOwner) {
ASSERT_TRUE(svc != nullptr);
ASSERT_TRUE(props != nullptr);
ASSERT_TRUE(celix_bundle_getId(svcOwner) >= 0);
int *c = static_cast<int*>(handle);
*c -= 1;
};
long svcId1 = celix_bundleContext_registerService(ctx, (void*)0x100, "calc", nullptr);
celix_service_tracking_options_t opts{};
opts.filter.serviceName = "calc";
opts.callbackHandle = &count;
opts.addWithOwner = add;
opts.removeWithOwner = remove;
long trackerId = celix_bundleContext_trackServicesWithOptions(ctx, &opts);
ASSERT_TRUE(trackerId >= 0);
ASSERT_EQ(1, count);
long svcId2 = celix_bundleContext_registerService(ctx, (void*)0x200, "calc", nullptr);
ASSERT_TRUE(svcId1 >= 0);
ASSERT_EQ(2, count);
celix_bundleContext_unregisterService(ctx, svcId1);
celix_bundleContext_unregisterService(ctx, svcId2);
ASSERT_EQ(0, count);
celix_bundleContext_stopTracker(ctx, trackerId);
}
TEST_F(CelixBundleContextServicesTests, serviceTrackerWithRaceConditionTest) {
struct calc {
int (*calc)(int);
};
const char *calcName = "calc";
struct calc svc;
svc.calc = [](int n) -> int {
return n * 42;
};
struct data {
std::mutex mutex{};
std::condition_variable sync{};
long svcId {-1};
bool inAddCall{false};
bool inRemoveCall{false};
bool readyToExit{false};
int result{0};
};
struct data data{};
auto add = [](void *handle, void *svc) {
ASSERT_TRUE(svc != nullptr);
struct data *d = static_cast<struct data*>(handle);
std::unique_lock<std::mutex> lock(d->mutex);
d->inAddCall = true;
d->sync.notify_all();
d->sync.wait(lock, [d]{return d->readyToExit;});
lock.unlock();
struct calc *calc = static_cast<struct calc *>(svc);
int tmp = calc->calc(2);
lock.lock();
d->result = tmp;
lock.unlock();
};
auto remove = [](void *handle, void *svc) {
ASSERT_TRUE(svc != nullptr);
struct data *d = static_cast<struct data*>(handle);
std::unique_lock<std::mutex> lock(d->mutex);
std::cout << "In Remove call. waiting for ready to exit" << std::endl;
d->inRemoveCall = true;
d->sync.notify_all();
d->sync.wait(lock, [d]{return d->readyToExit;});
lock.unlock();
};
long trackerId = celix_bundleContext_trackServices(ctx, calcName, &data, add, remove);
std::thread registerThread{[&]{
long id = celix_bundleContext_registerService(ctx, &svc, calcName, nullptr);
std::cout << "registered service with id " << id << std::endl;
std::lock_guard<std::mutex> lock{data.mutex};
data.svcId = id;
data.sync.notify_all();
}};
std::thread unregisterThread{[&]{
long id = -1;
std::unique_lock<std::mutex> lock(data.mutex);
data.sync.wait(lock, [&]{return data.svcId >= 0;});
id = data.svcId;
lock.unlock();
std::cout << "trying to unregister ... with id " << id << std::endl;
celix_bundleContext_unregisterService(ctx, id);
std::cout << "done unregistering" << std::endl;
}};
std::cout << "waiting till inAddCall" << std::endl;
std::unique_lock<std::mutex> lock{data.mutex};
data.sync.wait(lock, [&]{return data.inAddCall;});
data.readyToExit = true;
data.sync.notify_all();
lock.unlock();
//let unregister sink in.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
lock.lock();
std::cout << "triggering ready to exit" << std::endl;
data.readyToExit = true;
data.sync.notify_all();
lock.unlock();
registerThread.join();
unregisterThread.join();
std::cout << "threads joined" << std::endl;
ASSERT_EQ(84, data.result);
ASSERT_TRUE(data.inAddCall);
ASSERT_TRUE(data.inRemoveCall);
celix_bundleContext_stopTracker(ctx, trackerId);
};
TEST_F(CelixBundleContextServicesTests, useServiceDoesNotBlockInEventLoop) {
void *svc1 = (void*)0x100;
auto set = [](void *handle, void */*svc*/) {
celix_bundle_context_t *ctx = static_cast<celix_bundle_context_t *>(handle);
celix_service_use_options_t use_opts{};
use_opts.filter.serviceName = "NotAvailable";
use_opts.waitTimeoutInSeconds = 3600; // unacceptable long blocking
use_opts.callbackHandle = nullptr;
use_opts.use = [](void *handle, void *svc) {
FAIL() << "We shouldn't get here: (" << handle << "," << svc << ")";
};
bool called = celix_bundleContext_useServiceWithOptions(ctx, &use_opts);
ASSERT_FALSE(called);
};
long svcId1 = celix_bundleContext_registerService(ctx, svc1, "NA", nullptr);
ASSERT_GE(svcId1, 0);
celix_service_tracking_options_t opts{};
opts.callbackHandle = (void*)ctx;
opts.filter.serviceName = "NA";
opts.set = set;
long trackerId = celix_bundleContext_trackServicesWithOptionsAsync(ctx, &opts);
ASSERT_TRUE(trackerId >= 0);
void *svc2 = (void*)0x200; //no ranking
long svcId2 = celix_bundleContext_registerServiceAsync(ctx, svc2, "NotAvailable", nullptr);
ASSERT_GE(svcId2, 0);
celix_bundleContext_waitForAsyncTracker(ctx, trackerId);
celix_bundleContext_waitForAsyncRegistration(ctx, svcId2);
celix_bundleContext_unregisterService(ctx, svcId2);
celix_bundleContext_stopTracker(ctx, trackerId);
celix_bundleContext_unregisterService(ctx, svcId1);
}
TEST_F(CelixBundleContextServicesTests, servicesTrackerSetTest) {
int count = 0;
void *svc1 = (void*)0x100; //no ranking
void *svc2 = (void*)0x200; //no ranking
void *svc3 = (void*)0x300; //10 ranking
void *svc4 = (void*)0x400; //5 ranking
auto set = [](void *handle, void *svc) {
static int callCount = 0;
callCount += 1;
if (callCount == 1) {
//first time svc1 should be set (oldest service with equal ranking
ASSERT_EQ(0x100, (long)svc);
} else if (callCount == 2) {
ASSERT_EQ(0x300, (long)svc);
//second time svc3 should be set (highest ranking)
} else if (callCount == 3) {
//third time svc4 should be set (highest ranking
ASSERT_EQ(0x400, (long)svc);
}
int *c = static_cast<int*>(handle);
*c = callCount;
};
long svcId1 = celix_bundleContext_registerService(ctx, svc1, "NA", nullptr);
long svcId2 = celix_bundleContext_registerService(ctx, svc2, "NA", nullptr);
//starting tracker should lead to first set call
celix_service_tracking_options_t opts{};
opts.callbackHandle = (void*)&count;