Synchronization.cpp (1849B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "TestCommon.h" 5 6 #include <AppInstallerSynchronization.h> 7 8 using namespace AppInstaller::Synchronization; 9 10 TEST_CASE("CPIL_BlocksOthers", "[CrossProcessInstallLock]") 11 { 12 wil::unique_event signal; 13 signal.create(); 14 AppInstaller::ProgressCallback progress; 15 16 { 17 CrossProcessInstallLock mainThreadLock; 18 mainThreadLock.Acquire(progress); 19 20 std::thread otherThread([&signal, &progress]() { 21 CrossProcessInstallLock otherThreadLock; 22 otherThreadLock.Acquire(progress); 23 signal.SetEvent(); 24 }); 25 // In the event of bugs, we don't want to block the test waiting forever 26 otherThread.detach(); 27 28 REQUIRE(!signal.wait(500)); 29 } 30 31 // Upon release of the writer, the other thread should signal 32 REQUIRE(signal.wait(500)); 33 } 34 35 TEST_CASE("CPIL_CancelEndsWait", "[CrossProcessInstallLock]") 36 { 37 wil::unique_event signal; 38 signal.create(); 39 AppInstaller::ProgressCallback progress; 40 41 CrossProcessInstallLock mainThreadLock; 42 mainThreadLock.Acquire(progress); 43 44 std::optional<bool> otherThreadAcquireResult = std::nullopt; 45 46 std::thread otherThread([&]() { 47 CrossProcessInstallLock otherThreadLock; 48 otherThreadAcquireResult = otherThreadLock.Acquire(progress); 49 signal.SetEvent(); 50 }); 51 // In the event of bugs, we don't want to block the test waiting forever 52 otherThread.detach(); 53 54 REQUIRE(!signal.wait(500)); 55 56 progress.Cancel(); 57 58 // Upon release of the writer, the other thread should signal 59 REQUIRE(signal.wait(500)); 60 61 REQUIRE(otherThreadAcquireResult.has_value()); 62 REQUIRE(!otherThreadAcquireResult.value()); 63 }