aboutsummaryrefslogtreecommitdiff
path: root/pw_sync/pw_sync_private/borrow_lockable_tests.h
blob: 26fe811943ea96fbfaa5df7d6afacb75748d98e4 (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
// Copyright 2023 The Pigweed Authors
//
// Licensed 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
//
//     https://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.
#pragma once

/// This file contains tests that can be used to verify a lock type can be
/// used in `pw::sync::Borrowable` to borrow types that use external locking.
///
/// Locks must at least meet C++'s \em BasicLockable named requirement. Tests
/// should be added using the `ADD_BORROWABLE_...` macros from this file.
///
/// * If a lock is not \em TimedLockable, use `ADD_BORROWABLE_LOCK_TESTS`, e.g.
///   `ADD_BORROWABLE_LOCK_TESTS(MyLock);`.
///
/// * If a lock is \em TimedLockable, use `ADD_BORROWABLE_TIMED_LOCK_TESTS` and
///   provide the appropriate clock, e.g.
///   `ADD_BORROWABLE_TIMED_LOCK_TESTS(MyLock, pw::chrono::SystemClock);`
///
/// * If the default test suite name is not suitable, use the `..._NAMED_TESTS`
///   variants, e.g.
///   `ADD_BORROWABLE_LOCK_NAMED_TESTS(MyTestSuite, pw::my_module::Lock);`.

#include "gtest/gtest.h"
#include "pw_sync/borrow.h"
#include "pw_sync/lock_traits.h"

namespace pw::sync {

// Test fixtures.

/// Simple struct that wraps a value.
struct Base {
  static constexpr int kInitialValue = 24;
  int base_value = kInitialValue;
};

/// Simple struct that derives from `Base` and wraps a value.
struct Derived : public Base {
  static constexpr int kInitialValue = 42;
  int value = kInitialValue;
};

/// Checks if a type has a method `locked()`.
///
/// Several fake locks are used in testing that simply update a bool instead of
/// actually locking. The lock state for these types can be accessed using
/// `locked()`.
///
/// @{
template <typename Lock, typename = void>
struct has_locked : std::false_type {};

template <typename Lock>
struct has_locked<Lock, std::void_t<decltype(std::declval<Lock>().locked())>>
    : std::true_type {};
/// @}

/// Checks if a lock's state matches the expected state.
///
/// This method can check fake locks used for testing as well as locks that
/// meet C++'s \em Lockable named requirement. This method is a no-op for lock
/// types that only meet \em BasicLockable.
///
/// @param[in]  lock      The lock to check.
/// @param[in]  expected  Indicates if the lock is expected to be locked.
template <typename Lock>
void CheckLocked(Lock& lock, bool expected) {
  if constexpr (has_locked<Lock>::value) {
    EXPECT_EQ(lock.locked(), expected);
  } else if constexpr (is_lockable_v<Lock>) {
    bool locked = !lock.try_lock();
    EXPECT_EQ(locked, expected);
    if (!locked) {
      lock.unlock();
    }
  }
}

template <typename Lock>
void TestAcquire() {
  Lock lock;
  Derived derived;
  Borrowable<Derived, Lock> borrowable(derived, lock);
  {
    BorrowedPointer<Derived, Lock> borrowed = borrowable.acquire();
    CheckLocked(lock, true);  // Ensure the lock is held.
    EXPECT_EQ(borrowed->value, Derived::kInitialValue);
    borrowed->value = 13;
  }
  CheckLocked(lock, false);  // Ensure the lock is released.
  EXPECT_EQ(derived.value, 13);
}

template <typename Lock>
void TestConstAcquire() {
  Lock lock;
  Derived derived;
  Borrowable<Derived, Lock> borrowable(derived, lock);
  const Borrowable<Derived, Lock> const_borrowable(borrowable);
  {
    BorrowedPointer<Derived, Lock> borrowed = const_borrowable.acquire();
    CheckLocked(lock, true);  // Ensure the lock is held.
    EXPECT_EQ(borrowed->value, Derived::kInitialValue);
    borrowed->value = 13;
  }
  CheckLocked(lock, false);  // Ensure the lock is released.
  EXPECT_EQ(derived.value, 13);
}

template <typename Lock>
void TestRepeatedAcquire() {
  Lock lock;
  Derived derived;
  Borrowable<Derived, Lock> borrowable(derived, lock);
  {
    BorrowedPointer<Derived, Lock> borrowed = borrowable.acquire();
    CheckLocked(lock, true);  // Ensure the lock is held.
    EXPECT_EQ(borrowed->value, Derived::kInitialValue);
    borrowed->value = 13;
  }
  CheckLocked(lock, false);  // Ensure the lock is released.
  {
    BorrowedPointer<Derived, Lock> borrowed = borrowable.acquire();
    CheckLocked(lock, true);  // Ensure the lock is held.
    EXPECT_EQ(borrowed->value, 13);
  }
  CheckLocked(lock, false);  // Ensure the lock is released.
}

template <typename Lock>
void TestMoveable() {
  Lock lock;
  Derived derived;
  Borrowable<Derived, Lock> borrowable(derived, lock);
  Borrowable<Derived, Lock> moved = std::move(borrowable);
  {
    BorrowedPointer<Derived, Lock> borrowed = moved.acquire();
    CheckLocked(lock, true);  // Ensure the lock is held.
    EXPECT_EQ(borrowed->value, Derived::kInitialValue);
    borrowed->value = 13;
  }
  CheckLocked(lock, false);  // Ensure the lock is released.
}

template <typename Lock>
void TestCopyable() {
  Lock lock;
  Derived derived;
  Borrowable<Derived, Lock> borrowable(derived, lock);
  const Borrowable<Derived, Lock>& intermediate = borrowable;
  Borrowable<Derived, Lock> copied(intermediate);
  {
    BorrowedPointer<Derived, Lock> borrowed = copied.acquire();
    CheckLocked(lock, true);  // Ensure the lock is held.
    EXPECT_EQ(borrowed->value, Derived::kInitialValue);
    borrowed->value = 13;
  }
  CheckLocked(lock, false);  // Ensure the lock is released.
  EXPECT_EQ(derived.value, 13);
}

template <typename Lock>
void TestCopyableCovariant() {
  Lock lock;
  Derived derived;
  Borrowable<Derived, Lock> borrowable(derived, lock);
  const Borrowable<Derived, Lock>& intermediate = borrowable;
  Borrowable<Base, Lock> copied_base(intermediate);
  {
    BorrowedPointer<Base, Lock> borrowed = copied_base.acquire();
    CheckLocked(lock, true);  // Ensure the lock is held.
    EXPECT_EQ(borrowed->base_value, Base::kInitialValue);
    borrowed->base_value = 13;
  }
  CheckLocked(lock, false);  // Ensure the lock is released.
  EXPECT_EQ(derived.base_value, 13);
}

template <typename Lock>
void TestTryAcquireSuccess() {
  Lock lock;
  Derived derived;
  Borrowable<Derived, Lock> borrowable(derived, lock);
  if constexpr (is_lockable_v<Lock>) {
    std::optional<BorrowedPointer<Derived, Lock>> maybe_borrowed =
        borrowable.try_acquire();
    ASSERT_TRUE(maybe_borrowed.has_value());
    CheckLocked(lock, true);  // Ensure the lock is held.
    EXPECT_EQ(maybe_borrowed.value()->value, Derived::kInitialValue);
  }
  CheckLocked(lock, false);  // Ensure the lock is released.
}

template <typename Lock>
void TestTryAcquireFailure() {
  Lock lock;
  Derived derived;
  Borrowable<Derived, Lock> borrowable(derived, lock);
  lock.lock();
  CheckLocked(lock, true);  // Ensure the lock is held.
  if constexpr (is_lockable_v<Lock>) {
    std::optional<BorrowedPointer<Derived, Lock>> maybe_borrowed =
        borrowable.try_acquire();
    EXPECT_FALSE(maybe_borrowed.has_value());
  }
  CheckLocked(lock, true);  // Ensure the lock is held.
  lock.unlock();
}

template <typename Lock>
void TestTryAcquireForSuccess() {
  Lock lock;
  Derived derived;
  Borrowable<Derived, Lock> borrowable(derived, lock);
  if constexpr (is_lockable_for_v<Lock, decltype(std::chrono::seconds(0))>) {
    std::optional<BorrowedPointer<Derived, Lock>> maybe_borrowed =
        borrowable.try_acquire_for(std::chrono::seconds(0));
    ASSERT_TRUE(maybe_borrowed.has_value());
    CheckLocked(lock, true);  // Ensure the lock is held.
    EXPECT_EQ(maybe_borrowed.value()->value, Derived::kInitialValue);
  }
  CheckLocked(lock, false);  // Ensure the lock is released.
}

template <typename Lock>
void TestTryAcquireForFailure() {
  Lock lock;
  Derived derived;
  Borrowable<Derived, Lock> borrowable(derived, lock);
  lock.lock();
  CheckLocked(lock, true);  // Ensure the lock is held.
  if constexpr (is_lockable_for_v<Lock, decltype(std::chrono::seconds(0))>) {
    std::optional<BorrowedPointer<Derived, Lock>> maybe_borrowed =
        borrowable.try_acquire_for(std::chrono::seconds(0));
    EXPECT_FALSE(maybe_borrowed.has_value());
  }
  CheckLocked(lock, true);  // Ensure the lock is held.
  lock.unlock();
}

/// Fake clock for use with non-timed locks.
///
/// This clock is guaranteed to fail `is_lockable_until<Lock, Clock>` and as
/// such is suitable to make the `TestTryAcquireUntilSuccess` and
/// `TestTryAcquireUntilFailure` tests pass trivially for lock types that do not
/// meet C++'s \em TimedLockable named requirement for any clock.
struct NoClock {
  using duration = void;
  using time_point = void;
};

template <typename Lock, typename Clock = NoClock>
void TestTryAcquireUntilSuccess() {
  Lock lock;
  Derived derived;
  Borrowable<Derived, Lock> borrowable(derived, lock);
  if constexpr (is_lockable_until_v<Lock, Clock>) {
    std::optional<BorrowedPointer<Derived, Lock>> maybe_borrowed =
        borrowable.try_acquire_until(Clock::time_point());
    ASSERT_TRUE(maybe_borrowed.has_value());
    CheckLocked(lock, true);  // Ensure the lock is held.
    EXPECT_EQ(maybe_borrowed.value()->value, Derived::kInitialValue);
  }
  CheckLocked(lock, false);  // Ensure the lock is released.
}

template <typename Lock, typename Clock = NoClock>
void TestTryAcquireUntilFailure() {
  Lock lock;
  Derived derived;
  Borrowable<Derived, Lock> borrowable(derived, lock);
  lock.lock();
  CheckLocked(lock, true);  // Ensure the lock is held.
  if constexpr (is_lockable_until_v<Lock, Clock>) {
    std::optional<BorrowedPointer<Derived, Lock>> maybe_borrowed =
        borrowable.try_acquire_until(Clock::time_point());
    EXPECT_FALSE(maybe_borrowed.has_value());
  }
  CheckLocked(lock, true);  // Ensure the lock is held.
  lock.unlock();
}

/// Register borrowable non-timed lock tests.
#define PW_SYNC_ADD_BORROWABLE_LOCK_TESTS(lock) \
  PW_SYNC_ADD_BORROWABLE_TIMED_LOCK_TESTS(lock, NoClock)

/// Register borrowable non-timed lock tests in a named test suite.
#define PW_SYNC_ADD_BORROWABLE_LOCK_NAMED_TESTS(name, lock) \
  PW_SYNC_ADD_BORROWABLE_TIMED_LOCK_NAMED_TESTS(name, lock, NoClock)

/// Register all borrowable lock tests.
#define PW_SYNC_ADD_BORROWABLE_TIMED_LOCK_TESTS(lock, clock) \
  PW_SYNC_ADD_BORROWABLE_TIMED_LOCK_NAMED_TESTS(             \
      Borrowable##lock##Test, lock, clock)

/// Register all borrowable lock tests in a named test suite.
#define PW_SYNC_ADD_BORROWABLE_TIMED_LOCK_NAMED_TESTS(name, lock, clock) \
  TEST(name, Acquire) { TestAcquire<lock>(); }                           \
  TEST(name, ConstAcquire) { TestConstAcquire<lock>(); }                 \
  TEST(name, RepeatedAcquire) { TestRepeatedAcquire<lock>(); }           \
  TEST(name, Moveable) { TestMoveable<lock>(); }                         \
  TEST(name, Copyable) { TestCopyable<lock>(); }                         \
  TEST(name, CopyableCovariant) { TestCopyableCovariant<lock>(); }       \
  TEST(name, TryAcquireForSuccess) { TestTryAcquireForSuccess<lock>(); } \
  TEST(name, TryAcquireForFailure) { TestTryAcquireForFailure<lock>(); } \
  TEST(name, TryAcquireUntilSuccess) {                                   \
    TestTryAcquireUntilSuccess<lock, clock>();                           \
  }                                                                      \
  TEST(name, TryAcquireUntilFailure) {                                   \
    TestTryAcquireUntilFailure<lock, clock>();                           \
  }                                                                      \
  static_assert(true, "trailing semicolon")

}  // namespace pw::sync