Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/Thread.cpp

Issue 29367003: Issue #4711 - Rewrite legacy thread facilities
Patch Set: Created Dec. 7, 2016, 4:44 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <gtest/gtest.h>
19 #include <queue>
20 #include <vector>
21
22 #include "../src/Thread.h"
23
24 namespace
25 {
26 class Mock : public AdblockPlus::Thread
27 {
28 public:
29 int timesCalled;
30 AdblockPlus::Mutex mutex;
31
32 Mock() : timesCalled(0)
33 {
34 }
35
36 void Run()
37 {
38 AdblockPlus::Sleep(5);
39 AdblockPlus::Lock lock(mutex);
40 timesCalled++;
41 }
42 };
43
44 class LockingMock : public AdblockPlus::Thread
45 {
46 public:
47 bool working;
48
49 LockingMock(std::vector<std::string>& log, AdblockPlus::Mutex& logMutex)
50 : log(log), logMutex(logMutex)
51 {
52 }
53
54 void Run()
55 {
56 AdblockPlus::Lock lock(logMutex);
57 log.push_back("started");
58 AdblockPlus::Sleep(5);
59 log.push_back("ended");
60 }
61
62 private:
63 const std::string name;
64 std::vector<std::string>& log;
65 AdblockPlus::Mutex& logMutex;
66 };
67 }
68
69 TEST(ThreadTest, Run)
70 {
71 Mock mock;
72 ASSERT_EQ(0, mock.timesCalled);
73 mock.Start();
74 mock.mutex.Lock();
75 ASSERT_EQ(0, mock.timesCalled);
76 mock.mutex.Unlock();
77 mock.Join();
78 ASSERT_EQ(1, mock.timesCalled);
79 }
80
81 TEST(ThreadTest, Mutex)
82 {
83 std::vector<std::string> log;
84 AdblockPlus::Mutex logMutex;
85 LockingMock mock1(log, logMutex);
86 LockingMock mock2(log, logMutex);
87 mock1.Start();
88 mock2.Start();
89 mock1.Join();
90 mock2.Join();
91 ASSERT_EQ("started", log[0]);
92 ASSERT_EQ("ended", log[1]);
93 ASSERT_EQ("started", log[2]);
94 ASSERT_EQ("ended", log[3]);
95 }
OLDNEW
« src/Thread.h ('K') | « test/Prefs.cpp ('k') | test/UpdateCheck.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld