aboutsummaryrefslogtreecommitdiff
path: root/fcp/tracing/test/tracing_test.cc
blob: 037e9661704f49612ba8283b84bd95dec55b18d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <memory>
#include <string>
#include <thread>  // NOLINT(build/c++11)

#include "gtest/gtest.h"
#include "fcp/tracing/test/tracing_schema.h"
#include "fcp/tracing/test_tracing_recorder.h"
#include "fcp/tracing/tracing_severity.h"
#include "flatbuffers/flatbuffers.h"

namespace fcp {
namespace {

using flatbuffers::FlatBufferBuilder;
using flatbuffers::GetRoot;
using testing::_;
using testing::ElementsAre;
using testing::Eq;
using testing::Gt;
using testing::Not;
using testing::SizeIs;
using testing::UnorderedElementsAre;

TEST(Tracing, TraitsTag) {
  EXPECT_EQ(TracingTraits<SpanWithId>::kTag.str(), "SWID");
  EXPECT_EQ(TracingTraits<EventBar>::kTag.str(), "EBAR");
}

TEST(Tracing, TracingSeverity) {
  EXPECT_EQ(TracingTraits<SpanWithId>::kSeverity, fcp::TracingSeverity::kInfo);
  EXPECT_EQ(TracingTraits<ErrorEvent>::kSeverity, fcp::TracingSeverity::kError);
  EXPECT_EQ(TracingTraits<EventWithNoData>::kSeverity,
            fcp::TracingSeverity::kWarning);
}

TEST(Tracing, TraitsCreate) {
  FlatBufferBuilder fbb_foo;
  fbb_foo.Finish(TracingTraits<EventFoo>::Create(222, 333, &fbb_foo));
  auto foo = GetRoot<EventFoo>(fbb_foo.GetBufferPointer());
  EXPECT_EQ(foo->first(), 222);
  EXPECT_EQ(foo->second(), 333);

  // Creating a flat buffer involving a string field has different codegen
  // path, testing this as well:
  FlatBufferBuilder fbb_bar;
  fbb_bar.Finish(
      TracingTraits<EventBar>::Create(444, "Hello world!", &fbb_bar));
  auto bar = GetRoot<EventBar>(fbb_bar.GetBufferPointer());
  EXPECT_EQ(bar->first(), 444);
  EXPECT_EQ(bar->second()->str(), "Hello world!");

  // Also make sure that a flatbuf involving a string field can be created using
  // a std::string.
  FlatBufferBuilder fbb_baz;
  std::string hello_str = "Hello world!";
  fbb_baz.Finish(TracingTraits<EventBar>::Create(444, hello_str, &fbb_baz));
  auto baz = GetRoot<EventBar>(fbb_baz.GetBufferPointer());
  EXPECT_EQ(baz->first(), 444);
  EXPECT_EQ(baz->second()->str(), "Hello world!");
}

TEST(Tracing, TraitsCreateFieldOrder) {
  int first_i = -333;
  int second_i = 444;
  FlatBufferBuilder fbb_foo;
  fbb_foo.Finish(
      TracingTraits<FieldOrder>::Create(first_i, second_i, "hello", &fbb_foo));
  auto foo = GetRoot<FieldOrder>(fbb_foo.GetBufferPointer());
  EXPECT_EQ(foo->fieldz(), first_i);
  EXPECT_EQ(foo->fieldy(), second_i);
  EXPECT_EQ(foo->fieldx()->str(), "hello");

  FlatBufferBuilder fbb_bar;
  fbb_bar.Finish(TracingTraits<OrderWithIds>::Create("hello", first_i, second_i,
                                                     &fbb_bar));
  auto bar = GetRoot<OrderWithIds>(fbb_bar.GetBufferPointer());
  EXPECT_EQ(bar->fieldz(), first_i);
  EXPECT_EQ(bar->fieldy(), second_i);
  EXPECT_EQ(bar->fieldx()->str(), "hello");
}

TEST(Tracing, TraitsCreateAllTypes) {
  FlatBufferBuilder fbb;
  std::int8_t byte = -1;
  std::uint8_t ubyte = 1;
  std::int16_t short_i = -256;
  std::uint16_t ushort_i = 256;
  int i = -333;
  unsigned int ui = 444;
  float f = 1.1;
  std::int64_t li = -4294967296;
  std::uint64_t uli = 4294967296;
  double d = 12312318.99999999;
  fbb.Finish(TracingTraits<AllTypes>::Create(byte, ubyte, true, short_i,
                                             ushort_i, i, ui, f, li, uli, d,
                                             "hello", &fbb));
  auto foo = GetRoot<AllTypes>(fbb.GetBufferPointer());
  EXPECT_EQ(foo->fieldz(), byte);
  EXPECT_EQ(foo->fieldy(), ubyte);
  EXPECT_EQ(foo->fieldx(), true);
  EXPECT_EQ(foo->fieldw(), short_i);
  EXPECT_EQ(foo->fieldv(), ushort_i);
  EXPECT_EQ(foo->fieldu(), i);
  EXPECT_EQ(foo->fieldt(), ui);
  EXPECT_EQ(foo->fields(), f);
  EXPECT_EQ(foo->fieldr(), li);
  EXPECT_EQ(foo->fieldq(), uli);
  EXPECT_EQ(foo->fieldp(), d);
  EXPECT_EQ(foo->fieldo()->str(), "hello");
}

TEST(Tracing, TraitsCreateEnum) {
  FlatBufferBuilder fbb;
  fbb.Finish(TracingTraits<ColorEnum>::Create(Color_Blue, &fbb));
  auto foo = GetRoot<ColorEnum>(fbb.GetBufferPointer());
  EXPECT_EQ(foo->color(), Color_Blue);
}

TEST(Tracing, TraitsCreateDeprecatedField) {
  FlatBufferBuilder fbb_foo;
  fbb_foo.Finish(TracingTraits<DeprecatedInt>::Create(222, &fbb_foo));
  auto foo = GetRoot<EventFoo>(fbb_foo.GetBufferPointer());
  EXPECT_EQ(foo->second(), 222);
}

TEST(Tracing, LookupTraitByTag) {
  EXPECT_EQ(TracingTraitsBase::Lookup(TracingTag("SWID"))->Name(),
            "SpanWithId");
  EXPECT_EQ(TracingTraitsBase::Lookup(TracingTag("EBAR"))->Name(), "EventBar");
}

TEST(Tracing, IntegrationTest) {
  TestTracingRecorder tracing_recorder;
  {
    TracingSpan<SpanWithId> inner(111);
    Trace<EventFoo>(222, 333);
    Trace<EventBar>(444, "Hello world!");
    Trace<EventFoo>(555, 666);
  }
  {
    TracingSpan<SpanWithNoData> inner;
    Trace<EventWithNoData>();
  }
  EXPECT_THAT(
      tracing_recorder.root(),
      ElementsAre(AllOf(IsSpan<SpanWithId>(),
                        ElementsAre(IsEvent<EventFoo>(222, 333),
                                    IsEvent<EventBar>(444, "Hello world!"),
                                    IsEvent<EventFoo>(555, 666))),
                  AllOf(IsSpan<SpanWithNoData>(),
                        ElementsAre(IsEvent<EventWithNoData>()))))
      << "Tracing span/events structure and content must match";
}

TEST(Tracing, UnscopedSpanIntegrationTest) {
  TestTracingRecorder tracing_recorder;
  auto outer = std::make_unique<UnscopedTracingSpan<SpanWithId>>(111);
  auto inner =
      std::make_unique<UnscopedTracingSpan<SpanWithId>>(outer->Ref(), 222);
  {
    TracingSpan<SpanWithNoData> child_of_inner(inner->Ref());
    Trace<EventFoo>(333, 444);
  }
  {
    TracingSpan<SpanWithNoData> another_child_of_inner(inner->Ref());
    Trace<EventFoo>(555, 666);
    Trace<EventBar>(inner->Ref(), 1, "Trace in unscoped span!");
    Trace<EventBar>(another_child_of_inner.Ref(), 1,
                    "Trace in explicitly specified tracing span!");
  }
  TracingSpan<SpanWithNoData> unrelated_span;
  Trace<EventBar>(777, "Hello world!");

  EXPECT_THAT(
      tracing_recorder.root(),
      ElementsAre(
          AllOf(IsSpan<SpanWithId>(111),
                ElementsAre(AllOf(
                    IsSpan<SpanWithId>(222),
                    ElementsAre(
                        AllOf(IsSpan<SpanWithNoData>(),
                              ElementsAre(IsEvent<EventFoo>(333, 444))),
                        AllOf(IsSpan<SpanWithNoData>(),
                              ElementsAre(IsEvent<EventFoo>(555, 666),
                                          IsEvent<EventBar>(
                                              1,
                                              "Trace in explicitly specified "
                                              "tracing span!"))),
                        IsEvent<EventBar>(1, "Trace in unscoped span!"))))),
          AllOf(IsSpan<SpanWithNoData>(),
                ElementsAre(IsEvent<EventBar>(777, "Hello world!")))))
      << "Tracing span/events structure and content must match";
}

TEST(Tracing, ThreadingUnscopedIntegrationTest) {
  TestTracingRecorder tracing_recorder;
  auto outer = std::make_unique<UnscopedTracingSpan<SpanWithId>>(111);
  std::thread thread1([ref = outer->Ref()]() {
    TracingSpan<SpanWithNoData> child_of_outer(ref);
    Trace<EventFoo>(333, 444);
  });
  std::thread thread2([ref = outer->Ref()]() {
    TracingSpan<SpanWithNoData> another_child_of_outer(ref);
    Trace<EventFoo>(555, 666);
    Trace<EventBar>(ref, 1, "Trace in unscoped span!");
    Trace<EventBar>(another_child_of_outer.Ref(), 1, "Trace in local span!");
  });
  TracingSpan<SpanWithNoData> unrelated_span;
  Trace<EventBar>(777, "Hello world!");
  thread1.join();
  thread2.join();

  EXPECT_THAT(
      tracing_recorder.root(),
      ElementsAre(AllOf(IsSpan<SpanWithId>(111),
                        UnorderedElementsAre(
                            AllOf(IsSpan<SpanWithNoData>(),
                                  ElementsAre(IsEvent<EventFoo>(333, 444))),
                            AllOf(IsSpan<SpanWithNoData>(),
                                  ElementsAre(IsEvent<EventFoo>(555, 666),
                                              IsEvent<EventBar>(
                                                  1, "Trace in local span!"))),
                            IsEvent<EventBar>(1, "Trace in unscoped span!"))),
                  AllOf(IsSpan<SpanWithNoData>(),
                        ElementsAre(IsEvent<EventBar>(777, "Hello world!")))))
      << "Tracing span/events structure and content must match";
}

TEST(Tracing, ThreadingScopedIntegrationTest) {
  TestTracingRecorder tracing_recorder;
  TracingSpan<SpanWithId> outer(111);
  std::thread thread1([ref = outer.Ref()]() {
    TracingSpan<SpanWithNoData> child_of_outer(ref);
    Trace<EventFoo>(333, 444);
  });
  std::thread thread2([ref = outer.Ref()]() {
    TracingSpan<SpanWithNoData> another_child_of_outer(ref);
    Trace<EventFoo>(555, 666);
    Trace<EventBar>(ref, 1, "Trace in unscoped span!");
    Trace<EventBar>(1, "Trace in local span!");
  });
  TracingSpan<SpanWithNoData> unrelated_span;
  Trace<EventBar>(777, "Hello world!");
  thread1.join();
  thread2.join();

  EXPECT_THAT(
      tracing_recorder.root(),
      ElementsAre(AllOf(
          IsSpan<SpanWithId>(111),
          UnorderedElementsAre(
              AllOf(IsSpan<SpanWithNoData>(),
                    ElementsAre(IsEvent<EventFoo>(333, 444))),
              AllOf(IsSpan<SpanWithNoData>(),
                    ElementsAre(IsEvent<EventFoo>(555, 666),
                                IsEvent<EventBar>(1, "Trace in local span!"))),
              IsEvent<EventBar>(1, "Trace in unscoped span!"),
              AllOf(IsSpan<SpanWithNoData>(),
                    ElementsAre(IsEvent<EventBar>(777, "Hello world!")))))))
      << "Tracing span/events structure and content must match";
}

TEST(Tracing, AdvancedMatching) {
  TestTracingRecorder tracing_recorder;
  {
    TracingSpan<SpanWithId> span(111);
    Trace<EventBar>(222, "Hello world!");
  }

  auto span = tracing_recorder.root()[0];
  auto event = span[0];
  EXPECT_THAT(span, IsSpan<SpanWithId>());
  EXPECT_THAT(event, IsEvent<EventBar>());
  EXPECT_THAT(span,
              AllOf(IsSpan<SpanWithId>(), ElementsAre(IsEvent<EventBar>())));
  EXPECT_THAT(span, IsSpan<SpanWithId>(_));
  EXPECT_THAT(span, IsSpan<SpanWithId>(Eq(111)));
  EXPECT_THAT(span, IsSpan<SpanWithId>(Gt(100)));
  EXPECT_THAT(event, IsEvent<EventBar>(Eq(222), Eq("Hello world!")));
  EXPECT_THAT(event, IsEvent<EventBar>(_, Eq("Hello world!")));
  EXPECT_THAT(event, IsEvent<EventBar>(_, _));
  EXPECT_THAT(event, IsEvent<EventBar>(Eq(222), _));
  EXPECT_THAT(event, IsEvent<EventBar>(Not(Eq(666)), _));
  EXPECT_THAT(event, Not(IsEvent<EventBar>(Eq(666), _)));
  EXPECT_THAT(event, Not(IsEvent<EventFoo>()));
}

TEST(Tracing, MultipleRecorders) {
  // NOTE: it is not a recommended scenario to have multiple instances of
  // TestTracingRecorder per unit test, but this code path is enforced to
  // ensure correct behavior of cleaning up global state so it is not carried
  // over between tests.
  {
    TestTracingRecorder tracing_recorder;
    Trace<EventFoo>(222, 333);
    EXPECT_THAT(tracing_recorder.root()[0], IsEvent<EventFoo>(222, 333));
  }
  {
    TestTracingRecorder tracing_recorder;
    Trace<EventFoo>(444, 555);
    EXPECT_THAT(tracing_recorder.root()[0], IsEvent<EventFoo>(444, 555));
  }
}

TEST(Tracing, TraceError) {
  TestTracingRecorder tracing_recorder;
  tracing_recorder.ExpectError<ErrorEvent>();
  {
    TracingSpan<SpanWithId> inner(111);
    Error err = TraceError<ErrorEvent>("there was a bug");
    (void)err;
  }
}

TEST(Tracing, FindOnlySpan) {
  TestTracingRecorder tracing_recorder;
  {
    TracingSpan<SpanWithNoData> outer;
    { TracingSpan<SpanWithId> inner(111); }
    EXPECT_EQ(tracing_recorder.FindOnlySpan<SpanWithId>().data()->id(), 111);
  }
}

TEST(Tracing, FindAllSpans) {
  TestTracingRecorder tracing_recorder;
  {
    TracingSpan<SpanWithNoData> outer;
    {
      TracingSpan<SpanWithId> inner1(111);
      { TracingSpan<SpanWithId> inner2(222); }
    }
    EXPECT_THAT(tracing_recorder.FindAllSpans<SpanWithId>(),
                ElementsAre(IsSpan<SpanWithId>(), IsSpan<SpanWithId>()));
    EXPECT_THAT(tracing_recorder.FindAllSpans<SpanNeverLogged>(), SizeIs(0));
  }
}

TEST(Tracing, FindOnlyEvent) {
  TestTracingRecorder tracing_recorder;
  {
    TracingSpan<SpanWithNoData> outer;
    { Trace<EventFoo>(111, 222); }
    EXPECT_EQ(tracing_recorder.FindOnlyEvent<EventFoo>().data()->first(), 111);
    EXPECT_EQ(tracing_recorder.FindOnlyEvent<EventFoo>().data()->second(), 222);
  }
}

TEST(Tracing, FindAllEvents) {
  TestTracingRecorder tracing_recorder;
  {
    TracingSpan<SpanWithNoData> outer;
    {
      Trace<EventFoo>(111, 222);
      TracingSpan<SpanWithNoData> inner;
      { Trace<EventFoo>(333, 444); }
    }
    EXPECT_THAT(tracing_recorder.FindAllEvents<EventFoo>(),
                ElementsAre(IsEvent<EventFoo>(), IsEvent<EventFoo>()));
    EXPECT_THAT(tracing_recorder.FindAllEvents<EventNeverLogged>(), SizeIs(0));
  }
}
TEST(Tracing, CreateJsonString) {
  FlatBufferBuilder fbb_foo;
  fbb_foo.Finish(TracingTraits<EventFoo>::Create(222, 333, &fbb_foo));
  auto foo_buf = fbb_foo.GetBufferPointer();
  auto foo = GetRoot<EventFoo>(fbb_foo.GetBufferPointer());
  EXPECT_EQ(foo->first(), 222);
  EXPECT_EQ(foo->second(), 333);

  TracingTraits<EventFoo> tracing_traits;
  std::string expected = "{\n  first: 222,\n  second: 333\n}\n";
  std::string json_gen = tracing_traits.JsonStringFormat(foo_buf);
  EXPECT_EQ(expected, json_gen);
}

// TODO(team) Add Testing for when the flatbuf has a package name

}  // namespace
}  // namespace fcp