proxygen
TraceEventTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
14 
17 
18 #include <string>
19 #include <vector>
20 
21 using namespace proxygen;
22 
23 TEST(TraceEventTest, IntegralDataIntegralValue) {
25 
26  int64_t data(13);
27  traceEvent.addMeta(TraceFieldType::Protocol, data);
28 
29  ASSERT_EQ(data,
31 }
32 
33 TEST(TraceEventTest, IntegralDataStringValue) {
35 
36  int64_t intData(13);
37  traceEvent.addMeta(TraceFieldType::Protocol, intData);
38 
39  std::string strData(std::to_string(intData));
40 
41  ASSERT_EQ(strData,
43 }
44 
45 TEST(TraceEventTest, IntegralDataVectorValue) {
47 
48  int64_t data(13);
49  traceEvent.addMeta(TraceFieldType::Protocol, data);
50 
52  traceEvent.getTraceFieldDataAs<std::vector<std::string>>(
54  Exception);
55 }
56 
57 TEST(TraceEventTest, StringDataIntegralValueConvertible) {
59 
60  int64_t intData(13);
61  std::string strData(std::to_string(intData));
62  traceEvent.addMeta(TraceFieldType::Protocol, strData);
63 
64  ASSERT_EQ(intData,
66 }
67 
68 TEST(TraceEventTest, StringDataIntegralValueNonConvertible) {
70 
71  std::string data("Abc");
72  traceEvent.addMeta(TraceFieldType::Protocol, data);
73 
76 }
77 
78 TEST(TraceEventTest, StringDataStringValue) {
80 
81  std::string data("Abc");
82  traceEvent.addMeta(TraceFieldType::Protocol, data);
83 
84  ASSERT_EQ(data,
86 }
87 
88 
89 TEST(TraceEventTest, StringDataVectorValue) {
91 
92  std::string data("Abc");
93  traceEvent.addMeta(TraceFieldType::Protocol, data);
94 
96  traceEvent.getTraceFieldDataAs<std::vector<std::string>>(
98  Exception);
99 }
100 
101 TEST(TraceEventTest, VectorDataIntegralValue) {
103 
104  std::vector<std::string> data;
105  data.push_back("Abc");
106  data.push_back("Hij");
107  data.push_back("Xyz");
108  traceEvent.addMeta(TraceFieldType::Protocol, data);
109 
110  ASSERT_THROW(
112  Exception);
113 }
114 
115 TEST(TraceEventTest, VectorDataStringValue) {
117 
118  std::vector<std::string> data;
119  data.push_back("A");
120  data.push_back("B");
121  data.push_back("C");
122  traceEvent.addMeta(TraceFieldType::Protocol, data);
123 
124  ASSERT_EQ(
125  "[\"A\",\"B\",\"C\"]",
127 }
128 
129 TEST(TraceEventTest, VectorDataVectorValue) {
131 
132  std::vector<std::string> data;
133  data.push_back("A");
134  data.push_back("B");
135  data.push_back("C");
136  traceEvent.addMeta(TraceFieldType::Protocol, data);
137 
138  std::vector<std::string> extractedData(
139  traceEvent.getTraceFieldDataAs<std::vector<std::string>>(
141 
142  EXPECT_THAT(extractedData, testing::ContainerEq(data));
143 }
144 
145 TEST(TraceEventTest, IteratorValueTypeCheckInteger) {
147  int64_t intData(13);
148  traceEvent.addMeta(TraceFieldType::Protocol, intData);
149 
150  auto itr = traceEvent.getMetaDataItr();
151  ASSERT_TRUE(itr.isValid());
152  ASSERT_EQ(TraceFieldType::Protocol, itr.getKey());
153  ASSERT_EQ(typeid(int64_t), itr.type());
154 
155  itr.next();
156  ASSERT_FALSE(itr.isValid());
157 }
158 
159 TEST(TraceEventTest, IteratorValueTypeCheckString) {
161  std::string strData("abc");
162  traceEvent.addMeta(TraceFieldType::Protocol, strData);
163 
164  auto itr = traceEvent.getMetaDataItr();
165  ASSERT_TRUE(itr.isValid());
166  ASSERT_EQ(TraceFieldType::Protocol, itr.getKey());
167  ASSERT_EQ(typeid(std::string), itr.type());
168 
169  itr.next();
170  ASSERT_FALSE(itr.isValid());
171 }
172 
173 TEST(TraceEventTest, IteratorValueTypeCheckStringArray) {
175  std::vector<std::string> arrData;
176  arrData.push_back("A");
177  arrData.push_back("B");
178  arrData.push_back("C");
179  traceEvent.addMeta(TraceFieldType::Protocol, arrData);
180 
181  auto itr = traceEvent.getMetaDataItr();
182  ASSERT_TRUE(itr.isValid());
183  ASSERT_EQ(TraceFieldType::Protocol, itr.getKey());
184  ASSERT_EQ(typeid(std::vector<std::string>), itr.type());
185 
186  itr.next();
187  ASSERT_FALSE(itr.isValid());
188 }
189 
190 // To
191 TEST(TraceEventTest, IntegerValueToString) {
193  traceEvent.start(TimePoint(std::chrono::milliseconds(100)));
194  traceEvent.end(TimePoint(std::chrono::milliseconds(200)));
195  int64_t intData(13);
196  traceEvent.addMeta(TraceFieldType::Protocol, intData);
197 
198  std::ostringstream out;
199  out << "TraceEvent(";
200  out << "type='TotalRequest', ";
201  out << "id='" << traceEvent.getID() << "', ";
202  out << "parentID='1', ";
203  out << "start='100', ";
204  out << "end='200', ";
205  out << "metaData='{protocol: 13, }')";
206 
207  ASSERT_EQ(out.str(), traceEvent.toString());
208 }
209 
210 TEST(TraceEventTest, StringValueToString) {
212  traceEvent.start(TimePoint(std::chrono::milliseconds(100)));
213  traceEvent.end(TimePoint(std::chrono::milliseconds(200)));
214  std::string strData("abc");
215  traceEvent.addMeta(TraceFieldType::Protocol, strData);
216 
217  std::ostringstream out;
218  out << "TraceEvent(";
219  out << "type='TotalRequest', ";
220  out << "id='" << traceEvent.getID() << "', ";
221  out << "parentID='1', ";
222  out << "start='100', ";
223  out << "end='200', ";
224  out << "metaData='{protocol: abc, }')";
225 
226  ASSERT_EQ(out.str(), traceEvent.toString());
227 }
228 
229 TEST(TraceEventTest, StringArrayValueToString) {
231  traceEvent.start(TimePoint(std::chrono::milliseconds(100)));
232  traceEvent.end(TimePoint(std::chrono::milliseconds(200)));
233  std::vector<std::string> arrData;
234  arrData.push_back("A");
235  arrData.push_back("B");
236  arrData.push_back("C");
237  traceEvent.addMeta(TraceFieldType::Protocol, arrData);
238 
239  std::ostringstream out;
240  out << "TraceEvent(";
241  out << "type='TotalRequest', ";
242  out << "id='" << traceEvent.getID() << "', ";
243  out << "parentID='1', ";
244  out << "start='100', ";
245  out << "end='200', ";
246  out << "metaData='{protocol: [\"A\",\"B\",\"C\"], }')";
247 
248  ASSERT_EQ(out.str(), traceEvent.toString());
249 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
std::string toString() const
Definition: TraceEvent.cpp:83
Iterator getMetaDataItr() const
Definition: TraceEvent.h:213
PolymorphicMatcher< internal::ContainerEqMatcher< GTEST_REMOVE_CONST_(Container)> > ContainerEq(const Container &rhs)
void end(const TimeUtil &tm)
Definition: TraceEvent.cpp:47
uint32_t getID() const
Definition: TraceEvent.h:182
bool addMeta(TraceFieldType key, T &&value)
Definition: TraceEvent.h:218
TEST(TraceEventTest, IntegralDataIntegralValue)
#define ASSERT_ANY_THROW(statement)
Definition: gtest.h:1853
#define ASSERT_THROW(statement, expected_exception)
Definition: gtest.h:1849
SteadyClock::time_point TimePoint
Definition: Time.h:25
#define EXPECT_THAT(value, matcher)
T getTraceFieldDataAs(TraceFieldType field) const
Definition: TraceEvent.h:199
const char * string
Definition: Conv.cpp:212
void start(const TimeUtil &tm)
Definition: TraceEvent.cpp:37
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43