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

Side by Side Diff: services/log/log_impl_unittest.cc

Issue 1447273002: Mojo Log service and a thread-safe client library. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: make min log level consistent with fallback logger, fix thread-safety, address trung's other commen… Created 5 years 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
OLDNEW
(Empty)
1 // Copyright 2015 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 <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8
9 #include <utility>
10
11 #include "base/files/scoped_file.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/test/test_timeouts.h"
14 #include "mojo/public/c/environment/logger.h"
15 #include "mojo/public/cpp/application/application_impl.h"
16 #include "mojo/public/cpp/application/application_test_base.h"
17 #include "mojo/public/cpp/system/functions.h"
18 #include "mojo/services/log/interfaces/entry.mojom.h"
19 #include "mojo/services/log/interfaces/log.mojom.h"
20 #include "services/log/log_impl.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace mojo {
24 namespace log {
25 namespace {
26
27 using base::MessageLoop;
28 using LogImplTest = mojo::test::ApplicationTestBase;
29
30 // We need to supply a ApplicationConnection to LogImpl::Create().
31 class TestApplicationConnection : public ApplicationConnection {
32 public:
33 const std::string& GetConnectionURL() override {
34 static std::string kConnectionURL = "mojo:log";
35 return kConnectionURL;
36 }
37
38 const std::string& GetRemoteApplicationURL() override {
39 static std::string kRemoteApplicationURL = "mojo:log_impl_unittest";
40 return kRemoteApplicationURL;
41 }
42
43 ServiceProvider* GetServiceProvider() override { return nullptr; }
44 void SetServiceConnectorForName(ServiceConnector* service_connector,
45 const std::string& name) override {}
46 };
47
48 // Tests the Log service implementation by calling its AddEntry and verifying
49 // the log message it prints to the supplied FILE stream.
50 TEST_F(LogImplTest, AddEntryOutput) {
51 // LogImpl will write to p[1] (which we wrap into a FILE*), we will EXPECT
52 // what's written by reading in p[0].
53 int p[2];
54 ASSERT_EQ(0, pipe(p));
55
56 base::ScopedFILE output_file(fdopen(p[1], "w"));
57 ASSERT_NE(nullptr, output_file.get());
58
59 LogPtr log;
60 TestApplicationConnection app_connection;
61 LogImpl::Create(&app_connection, GetProxy(&log), output_file.get());
62
63 Entry entry;
64 entry.log_level = MOJO_LOG_LEVEL_INFO;
65 entry.timestamp = GetTimeTicksNow();
66 entry.source_file = "file.ext";
67 entry.source_line = 0;
68 entry.message = "1234567890";
69 log->AddEntry(entry.Clone());
70
71 entry.source_line = 1;
72 log->AddEntry(entry.Clone());
73
74 entry.source_file.reset();
75 log->AddEntry(entry.Clone());
76
77 entry.message.reset();
78 log->AddEntry(entry.Clone());
79
80 log.reset();
81
82 MessageLoop::current()->PostDelayedTask(FROM_HERE,
83 MessageLoop::QuitWhenIdleClosure(),
84 TestTimeouts::tiny_timeout());
85
86 MessageLoop::current()->Run();
87 output_file.reset(); // This closes p[1].
88
89 const char expected_string[] =
90 "<mojo:log_impl_unittest> [INFO] file.ext: 1234567890\n"
91 "<mojo:log_impl_unittest> [INFO] file.ext:1: 1234567890\n"
92 "<mojo:log_impl_unittest> [INFO] 1234567890\n"
93 "<mojo:log_impl_unittest> [INFO] <no message>\n";
94 char actual_string[1024 * 10];
95 ssize_t nbytes = read(p[0], actual_string, strlen(expected_string));
96 EXPECT_EQ(static_cast<ssize_t>(strlen(expected_string)), nbytes);
viettrungluu 2015/12/17 18:34:39 This really should be |ASSERT_EQ|, since otherwise
vardhan 2015/12/17 23:35:42 Done.
97
98 close(p[0]);
99
100 actual_string[nbytes] = '\0';
101 EXPECT_STREQ(expected_string, actual_string);
102 }
103
104 } // namespace
105 } // namespace log
106 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698