Ewout van Bekkum | f010606 | 2021-05-06 14:08:33 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include <chrono> |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | #include "pw_sync/thread_notification.h" |
| 19 | |
| 20 | namespace pw::sync { |
| 21 | namespace { |
| 22 | |
| 23 | TEST(ThreadNotification, EmptyInitialState) { |
| 24 | ThreadNotification notification; |
| 25 | EXPECT_FALSE(notification.try_acquire()); |
| 26 | } |
| 27 | |
| 28 | // TODO(pwbug/291): Add real concurrency tests. |
| 29 | |
| 30 | TEST(ThreadNotification, Release) { |
| 31 | ThreadNotification notification; |
| 32 | notification.release(); |
| 33 | notification.release(); |
| 34 | notification.acquire(); |
| 35 | // Ensure it fails when empty. |
| 36 | EXPECT_FALSE(notification.try_acquire()); |
| 37 | } |
| 38 | |
| 39 | ThreadNotification empty_initial_notification; |
| 40 | TEST(ThreadNotification, EmptyInitialStateStatic) { |
| 41 | EXPECT_FALSE(empty_initial_notification.try_acquire()); |
| 42 | } |
| 43 | |
| 44 | ThreadNotification raise_notification; |
| 45 | TEST(ThreadNotification, ReleaseStatic) { |
| 46 | raise_notification.release(); |
| 47 | raise_notification.release(); |
| 48 | raise_notification.acquire(); |
| 49 | // Ensure it fails when empty. |
| 50 | EXPECT_FALSE(raise_notification.try_acquire()); |
| 51 | } |
| 52 | |
| 53 | } // namespace |
| 54 | } // namespace pw::sync |