Flat combining: Add lock holder with deferred option. Minor fixes.
[folly.git] / folly / experimental / flat_combining / test / FlatCombiningTest.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/experimental/flat_combining/test/FlatCombiningTestHelpers.h>
18
19 #include <folly/portability/GTest.h>
20 #include <glog/logging.h>
21
22 #include <mutex>
23
24 using namespace folly::test;
25
26 constexpr int LINES = 5;
27 constexpr int NUM_RECS = 20;
28 constexpr int WORK = 0;
29 constexpr int ITERS = 100;
30
31 static std::vector<int> nthr = {1, 10, 20};
32
33 struct Params {
34   bool combining, simple, dedicated, tc, syncop;
35 };
36
37 class FlatCombiningTest : public ::testing::TestWithParam<Params> {};
38
39 TEST(FlatCombiningTest, lock_holder) {
40   folly::FcSimpleExample<> ex(10);
41   {
42     std::unique_lock<std::mutex> l;
43     ex.holdLock(l);
44     CHECK(l.owns_lock());
45   }
46   {
47     std::unique_lock<std::mutex> l;
48     ex.holdLock(l, std::defer_lock);
49     CHECK(l.try_lock());
50   }
51   CHECK(ex.tryExclusive());
52   ex.releaseExclusive();
53 }
54
55 TEST_P(FlatCombiningTest, combining) {
56   Params p = GetParam();
57   for (auto n : nthr) {
58     run_test(
59         n,
60         LINES,
61         NUM_RECS,
62         WORK,
63         ITERS,
64         p.combining,
65         p.simple,
66         p.dedicated,
67         p.tc,
68         p.syncop,
69         true,
70         true);
71   }
72 }
73
74 TEST_P(FlatCombiningTest, more_threads_than_records) {
75   int n = 20;
76   int num_recs = 1;
77
78   Params p = GetParam();
79   run_test(
80       n,
81       LINES,
82       num_recs,
83       WORK,
84       ITERS,
85       p.combining,
86       p.simple,
87       p.dedicated,
88       p.tc,
89       p.syncop,
90       true,
91       true);
92 }
93
94 constexpr Params params[] = {
95     {false, false, false, false, false}, // no combining
96     // simple combining
97     //  dedicated
98     {true, true, true, false, true}, // no-tc sync
99     {true, true, true, false, false}, // no-tc async
100     {true, true, true, true, true}, // tc sync
101     {true, true, true, true, false}, // tc async
102     //   no dedicated
103     {true, true, false, false, true}, // no-tc sync
104     {true, true, false, false, false}, // no-tc async
105     {true, true, false, true, true}, // tc sync
106     {true, true, false, true, false}, // tc async
107     // custom combining
108     //  dedicated
109     {true, false, true, false, true}, // no-tc sync
110     {true, false, true, false, false}, // no-tc async
111     {true, false, true, true, true}, // tc sync
112     {true, false, true, true, false}, // tc async
113     //   no dedicated
114     {true, false, false, false, true}, // no-tc sync
115     {true, false, false, false, false}, // no-tc async
116     {true, false, false, true, true}, // tc sync
117     {true, false, false, true, false}, // tc async
118 };
119
120 INSTANTIATE_TEST_CASE_P(Foo, FlatCombiningTest, ::testing::ValuesIn(params));