Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1225)

Side by Side Diff: services/resource_coordinator/tracing/recorder_unittest.cc

Issue 2886203002: tracing: the recorder implementation (Closed)
Patch Set: rebase Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « services/resource_coordinator/tracing/recorder.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium 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 "services/resource_coordinator/tracing/recorder.h"
6
7 #include "base/bind.h"
8 #include "base/callback_forward.h"
9 #include "base/memory/ptr_util.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/threading/thread_task_runner_handle.h"
13 #include "base/values.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace tracing {
17
18 class RecorderTest : public testing::Test {
19 public:
20 void SetUp() override { message_loop_.reset(new base::MessageLoop()); }
21
22 void TearDown() override {
23 recorder_.reset();
24 message_loop_.reset();
25 }
26
27 void CreateRecorder(mojom::RecorderRequest request,
28 bool is_array,
29 const base::Closure& callback) {
30 recorder_.reset(new Recorder(std::move(request), is_array, callback,
31 base::ThreadTaskRunnerHandle::Get()));
32 }
33
34 void CreateRecorder(bool is_array, const base::Closure& callback) {
35 CreateRecorder(nullptr, is_array, callback);
36 }
37
38 void AddChunk(const std::string& chunk) { recorder_->AddChunk(chunk); }
39
40 void AddMetadata(std::unique_ptr<base::DictionaryValue> metadata) {
41 recorder_->AddMetadata(std::move(metadata));
42 }
43
44 std::unique_ptr<Recorder> recorder_;
45
46 private:
47 std::unique_ptr<base::MessageLoop> message_loop_;
48 };
49
50 TEST_F(RecorderTest, AddChunkArray) {
51 size_t num_calls = 0;
52 CreateRecorder(true /* is_array */,
53 base::BindRepeating([](size_t* num_calls) { (*num_calls)++; },
54 base::Unretained(&num_calls)));
55 AddChunk("chunk1");
56 AddChunk("chunk2");
57 AddChunk("chunk3");
58 EXPECT_EQ("chunk1,chunk2,chunk3", recorder_->data());
59
60 // Verify that the recorder has called the callback every time it received a
61 // chunk.
62 EXPECT_EQ(3u, num_calls);
63 }
64
65 TEST_F(RecorderTest, AddChunkString) {
66 size_t num_calls = 0;
67 CreateRecorder(false /* is_array */,
68 base::BindRepeating([](size_t* num_calls) { (*num_calls)++; },
69 base::Unretained(&num_calls)));
70 AddChunk("chunk1");
71 AddChunk("chunk2");
72 AddChunk("chunk3");
73 EXPECT_EQ("chunk1chunk2chunk3", recorder_->data());
74 EXPECT_EQ(3u, num_calls);
75 }
76
77 TEST_F(RecorderTest, AddMetadata) {
78 CreateRecorder(true /* is_array */, base::BindRepeating([] {}));
79
80 auto dict1 = base::MakeUnique<base::DictionaryValue>();
81 dict1->SetString("network-type", "Ethernet");
82 AddMetadata(std::move(dict1));
83
84 auto dict2 = base::MakeUnique<base::DictionaryValue>();
85 dict2->SetString("os-name", "CrOS");
86 AddMetadata(std::move(dict2));
87
88 EXPECT_EQ(2u, recorder_->metadata().size());
89 std::string net;
90 EXPECT_TRUE(recorder_->metadata().GetString("network-type", &net));
91 EXPECT_EQ("Ethernet", net);
92 std::string os;
93 EXPECT_TRUE(recorder_->metadata().GetString("os-name", &os));
94 EXPECT_EQ("CrOS", os);
95 }
96
97 TEST_F(RecorderTest, OnConnectionError) {
98 base::RunLoop run_loop;
99 size_t num_calls = 0;
100 {
101 mojom::RecorderPtr ptr;
102 auto request = MakeRequest(&ptr);
103 CreateRecorder(std::move(request), false /* is_array */,
104 base::BindRepeating(
105 [](size_t* num_calls, base::Closure quit_closure) {
106 (*num_calls)++;
107 quit_closure.Run();
108 },
109 base::Unretained(&num_calls), run_loop.QuitClosure()));
110 }
111 // |ptr| is deleted at this point and so the recorder should notify us that
112 // the client is not going to send any more data by running the callback.
113 run_loop.Run();
114 EXPECT_EQ(1u, num_calls);
115 }
116
117 } // namespace tracing
OLDNEW
« no previous file with comments | « services/resource_coordinator/tracing/recorder.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698