Flutter Engine
The Flutter Engine
Functions
sync_switch_unittest.cc File Reference
#include "flutter/fml/synchronization/sync_switch.h"
#include <thread>
#include "gtest/gtest.h"

Go to the source code of this file.

Functions

 TEST (SyncSwitchTest, Basic)
 
 TEST (SyncSwitchTest, NoopIfUndefined)
 
 TEST (SyncSwitchTest, SharedLock)
 

Function Documentation

◆ TEST() [1/3]

TEST ( SyncSwitchTest  ,
Basic   
)

Definition at line 13 of file sync_switch_unittest.cc.

13 {
14 SyncSwitch sync_switch;
15 bool switch_value = false;
16 sync_switch.Execute(SyncSwitch::Handlers()
17 .SetIfTrue([&] { switch_value = true; })
18 .SetIfFalse([&] { switch_value = false; }));
19 EXPECT_FALSE(switch_value);
20 sync_switch.SetSwitch(true);
21 sync_switch.Execute(SyncSwitch::Handlers()
22 .SetIfTrue([&] { switch_value = true; })
23 .SetIfFalse([&] { switch_value = false; }));
24 EXPECT_TRUE(switch_value);
25}
void Execute(const Handlers &handlers) const
Definition: sync_switch.cc:25
void SetSwitch(bool value)
Definition: sync_switch.cc:34
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678

◆ TEST() [2/3]

TEST ( SyncSwitchTest  ,
NoopIfUndefined   
)

Definition at line 27 of file sync_switch_unittest.cc.

27 {
28 SyncSwitch sync_switch;
29 bool switch_value = false;
30 sync_switch.Execute(SyncSwitch::Handlers());
31 EXPECT_FALSE(switch_value);
32}

◆ TEST() [3/3]

TEST ( SyncSwitchTest  ,
SharedLock   
)

Definition at line 34 of file sync_switch_unittest.cc.

34 {
35 SyncSwitch sync_switch;
36 sync_switch.SetSwitch(true);
37 bool switch_value1 = false;
38 bool switch_value2 = false;
39
40 std::thread thread1([&] {
41 sync_switch.Execute(
42 SyncSwitch::Handlers()
43 .SetIfTrue([&] {
44 switch_value1 = true;
45
46 std::thread thread2([&]() {
47 sync_switch.Execute(
48 SyncSwitch::Handlers()
49 .SetIfTrue([&] { switch_value2 = true; })
50 .SetIfFalse([&] { switch_value2 = false; }));
51 });
52 thread2.join();
53 })
54 .SetIfFalse([&] { switch_value1 = false; }));
55 });
56 thread1.join();
57 EXPECT_TRUE(switch_value1);
58 EXPECT_TRUE(switch_value2);
59}