• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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 "platform/api/trace_logging_platform.h"
6 
7 #ifndef PLATFORM_TEST_TRACE_LOGGING_HELPERS_H_
8 #define PLATFORM_TEST_TRACE_LOGGING_HELPERS_H_
9 
10 #include <chrono>
11 
12 #include "gmock/gmock.h"
13 #include "platform/base/trace_logging_activation.h"
14 #include "util/chrono_helpers.h"
15 #include "util/osp_logging.h"
16 
17 namespace openscreen {
18 
19 enum TraceHierarchyParts { kRoot = 0x1, kParent = 0x2, kCurrent = 0x4 };
20 
21 enum ArgumentId { kFirst, kSecond };
22 
23 class MockLoggingPlatform : public TraceLoggingPlatform {
24  public:
MockLoggingPlatform()25   MockLoggingPlatform() {
26     StartTracing(this);
27 
28     ON_CALL(*this, IsTraceLoggingEnabled(::testing::_))
29         .WillByDefault(::testing::Return(true));
30   }
31 
~MockLoggingPlatform()32   ~MockLoggingPlatform() override { StopTracing(); }
33 
34   MOCK_METHOD1(IsTraceLoggingEnabled, bool(TraceCategory::Value category));
35   MOCK_METHOD7(LogTrace,
36                void(const char*,
37                     const uint32_t,
38                     const char* file,
39                     Clock::time_point,
40                     Clock::time_point,
41                     TraceIdHierarchy ids,
42                     Error::Code));
43   MOCK_METHOD5(LogAsyncStart,
44                void(const char*,
45                     const uint32_t,
46                     const char* file,
47                     Clock::time_point,
48                     TraceIdHierarchy));
49   MOCK_METHOD5(LogAsyncEnd,
50                void(const uint32_t,
51                     const char* file,
52                     Clock::time_point,
53                     TraceId,
54                     Error::Code));
55 };
56 
57 // Methods to validate the results of platform-layer calls.
58 template <uint64_t milliseconds>
ValidateTraceTimestampDiff(const char * name,const uint32_t line,const char * file,Clock::time_point start_time,Clock::time_point end_time,TraceIdHierarchy ids,Error error)59 void ValidateTraceTimestampDiff(const char* name,
60                                 const uint32_t line,
61                                 const char* file,
62                                 Clock::time_point start_time,
63                                 Clock::time_point end_time,
64                                 TraceIdHierarchy ids,
65                                 Error error) {
66   const auto elapsed = to_milliseconds(end_time - start_time);
67   ASSERT_GE(static_cast<uint64_t>(elapsed.count()), milliseconds);
68 }
69 
70 template <Error::Code result>
ValidateTraceErrorCode(const char * name,const uint32_t line,const char * file,Clock::time_point start_time,Clock::time_point end_time,TraceIdHierarchy ids,Error error)71 void ValidateTraceErrorCode(const char* name,
72                             const uint32_t line,
73                             const char* file,
74                             Clock::time_point start_time,
75                             Clock::time_point end_time,
76                             TraceIdHierarchy ids,
77                             Error error) {
78   ASSERT_EQ(error.code(), result);
79 }
80 
81 template <TraceId Current,
82           TraceId Parent,
83           TraceId Root,
84           TraceHierarchyParts parts>
ValidateTraceIdHierarchyOnSyncTrace(const char * name,const uint32_t line,const char * file,Clock::time_point start_time,Clock::time_point end_time,TraceIdHierarchy ids,Error error)85 void ValidateTraceIdHierarchyOnSyncTrace(const char* name,
86                                          const uint32_t line,
87                                          const char* file,
88                                          Clock::time_point start_time,
89                                          Clock::time_point end_time,
90                                          TraceIdHierarchy ids,
91                                          Error error) {
92   if (parts & TraceHierarchyParts::kCurrent) {
93     ASSERT_EQ(ids.current, Current);
94   }
95   if (parts & TraceHierarchyParts::kParent) {
96     ASSERT_EQ(ids.parent, Parent);
97   }
98   if (parts & TraceHierarchyParts::kRoot) {
99     ASSERT_EQ(ids.root, Root);
100   }
101 }
102 
103 template <TraceId Current,
104           TraceId Parent,
105           TraceId Root,
106           TraceHierarchyParts parts>
ValidateTraceIdHierarchyOnAsyncTrace(const char * name,const uint32_t line,const char * file,Clock::time_point timestamp,TraceIdHierarchy ids)107 void ValidateTraceIdHierarchyOnAsyncTrace(const char* name,
108                                           const uint32_t line,
109                                           const char* file,
110                                           Clock::time_point timestamp,
111                                           TraceIdHierarchy ids) {
112   if (parts & TraceHierarchyParts::kCurrent) {
113     EXPECT_EQ(ids.current, Current);
114   }
115   if (parts & TraceHierarchyParts::kParent) {
116     EXPECT_EQ(ids.parent, Parent);
117   }
118   if (parts & TraceHierarchyParts::kRoot) {
119     EXPECT_EQ(ids.root, Root);
120   }
121 }
122 
123 }  // namespace openscreen
124 
125 #endif  // PLATFORM_TEST_TRACE_LOGGING_HELPERS_H_
126