| 26 | #include "platform/threads/thread.h" |
| 27 | |
| 28 | TEST(Semaphore, BasicSynchronization) |
| 29 | { |
| 30 | Semaphore *sem1 = new Semaphore(1); |
| 31 | Semaphore *sem2 = new Semaphore(1); |
| 32 | |
| 33 | // Test that we can do non-blocking acquires that succeed. |
| 34 | EXPECT_TRUE(sem1->acquire(false)) |
| 35 | << "Should succeed at acquiring a new semaphore with count 1."; |
| 36 | EXPECT_TRUE(sem2->acquire(false)) |
| 37 | << "This one should succeed too, see previous test."; |
| 38 | |
| 39 | // Test that we can do non-blocking acquires that fail. |
| 40 | EXPECT_FALSE(sem1->acquire(false)) |
| 41 | << "Should failed, as we've already got the sem."; |
| 42 | sem1->release(); |
| 43 | EXPECT_FALSE(sem2->acquire(false)) |
| 44 | << "Should also fail."; |
| 45 | sem2->release(); |
| 46 | |
| 47 | // Test that we can do blocking acquires that succeed. |
| 48 | EXPECT_TRUE(sem1->acquire(true)) |
| 49 | << "Should succeed as we just released."; |
| 50 | EXPECT_TRUE(sem2->acquire(true)) |
| 51 | << "Should succeed as we just released."; |
| 52 | |
| 53 | // Clean up. |
| 54 | delete sem1; |
| 55 | delete sem2; |
| 56 | } |
| 57 | |
| 58 | TEST(Semaphore, MultiThreadSynchronization) |
| 59 | { |