Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
sync_switch_unittest.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/fml/synchronization/sync_switch.h"
6
7#include <thread>
8
9#include "gtest/gtest.h"
10
11using fml::SyncSwitch;
12
13TEST(SyncSwitchTest, Basic) {
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}
26
27TEST(SyncSwitchTest, NoopIfUndefined) {
28 SyncSwitch sync_switch;
29 bool switch_value = false;
30 sync_switch.Execute(SyncSwitch::Handlers());
31 EXPECT_FALSE(switch_value);
32}
33
34TEST(SyncSwitchTest, SharedLock) {
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}
#define TEST(S, s, D, expected)
void Execute(const Handlers &handlers) const
void SetSwitch(bool value)
#define EXPECT_TRUE(handle)
Definition unit_test.h:685