25
25
26
26
// Testing parameters.
27
27
// These are set by each test to force certain outcomes.
28
- // Since the tests may be run in parellel, these should probably
29
- // be thread_local.
30
- static thread_local bool HasExitError = false ;
31
- static thread_local std::string ExitMsg = " " ;
32
- static thread_local bool HasVendorConfig = false ;
33
- static thread_local bool SanitizeData = false ;
34
- static thread_local std::string Buffer = " " ;
35
- static thread_local std::vector<llvm::json::Object> EmittedJsons;
36
- static thread_local std::string ExpectedUuid = " " ;
28
+ // Since the tests may run in parallel, each test will have
29
+ // its own TestContext populated.
30
+ struct TestContext {
31
+ // Controlling whether there should be an Exit error (if so, what the
32
+ // expected exit message/description should be).
33
+ bool HasExitError = false ;
34
+ std::string ExitMsg = " " ;
35
+
36
+ // Controllilng whether there is a vendor-provided config for
37
+ // Telemetry.
38
+ bool HasVendorConfig = false ;
39
+
40
+ // Controlling whether the data should be sanitized.
41
+ bool SanitizeData = false ;
42
+
43
+ // These two fields data emitted by the framework for later
44
+ // verifications by the tests.
45
+ std::string Buffer = " " ;
46
+ std::vector<llvm::json::Object> EmittedJsons;
47
+
48
+ // The expected Uuid generated by the fake tool.
49
+ std::string ExpectedUuid = " " ;
50
+ };
51
+
52
+ // This is set by the test body.
53
+ static thread_local TestContext *CurrentContext = nullptr ;
37
54
38
55
namespace llvm {
39
56
namespace telemetry {
@@ -289,7 +306,7 @@ class JsonStreamDestination : public Destination {
289
306
// send the data to some internal storage.
290
307
// For testing purposes, we just queue up the entries to
291
308
// the vector for validation.
292
- EmittedJsons.push_back (std::move (O));
309
+ CurrentContext-> EmittedJsons .push_back (std::move (O));
293
310
return Error::success ();
294
311
}
295
312
bool ShouldSanitize;
@@ -305,19 +322,19 @@ class TestTelemeter : public Telemeter {
305
322
<< " \n " ;
306
323
if (!config->EnableTelemetry )
307
324
return nullptr ;
308
- ExpectedUuid = nextUuid ();
325
+ CurrentContext-> ExpectedUuid = nextUuid ();
309
326
std::unique_ptr<TestTelemeter> Telemeter =
310
- std::make_unique<TestTelemeter>(ExpectedUuid);
327
+ std::make_unique<TestTelemeter>(CurrentContext-> ExpectedUuid );
311
328
// Set up Destination based on the given config.
312
329
for (const std::string &Dest : config->AdditionalDestinations ) {
313
330
// The destination(s) are ALSO defined by vendor, so it should understand
314
331
// what the name of each destination signifies.
315
332
if (Dest == JSON_DEST) {
316
- Telemeter->addDestination (
317
- new vendor_code::JsonStreamDestination ( SanitizeData));
333
+ Telemeter->addDestination (new vendor_code::JsonStreamDestination (
334
+ CurrentContext-> SanitizeData ));
318
335
} else if (Dest == STRING_DEST) {
319
- Telemeter->addDestination (
320
- new vendor_code::StringDestination ( SanitizeData, Buffer));
336
+ Telemeter->addDestination (new vendor_code::StringDestination (
337
+ CurrentContext-> SanitizeData , CurrentContext-> Buffer ));
321
338
} else {
322
339
llvm_unreachable (
323
340
llvm::Twine (" unknown destination: " , Dest).str ().c_str ());
@@ -431,7 +448,7 @@ std::shared_ptr<llvm::telemetry::Config> GetTelemetryConfig() {
431
448
// #endif
432
449
//
433
450
// But for unit testing, we use the testing params defined at the top.
434
- if (HasVendorConfig) {
451
+ if (CurrentContext-> HasVendorConfig ) {
435
452
llvm::telemetry::vendor_code::ApplyVendorSpecificConfigs (Config.get ());
436
453
}
437
454
return Config;
@@ -468,8 +485,8 @@ void AtToolExit(std::string ToolName, vendor_code::TestTelemeter *T) {
468
485
T->makeDefaultTelemetryInfo <vendor_code::ExitEvent>();
469
486
Entry.Stats = {ExitTime, ExitCompleteTime};
470
487
471
- if (HasExitError) {
472
- Entry.ExitDesc = {1 , ExitMsg};
488
+ if (CurrentContext-> HasExitError ) {
489
+ Entry.ExitDesc = {1 , CurrentContext-> ExitMsg };
473
490
}
474
491
T->logExit (ToolName, &Entry);
475
492
}
@@ -491,7 +508,11 @@ static std::string ValueToString(const json::Value *V) {
491
508
492
509
// Without vendor's implementation, telemetry is not enabled by default.
493
510
TEST (TelemetryTest, TelemetryDefault) {
494
- HasVendorConfig = false ;
511
+ // Preset some test params.
512
+ TestContext Context;
513
+ Context.HasVendorConfig = false ;
514
+ CurrentContext = &Context;
515
+
495
516
std::shared_ptr<llvm::telemetry::Config> Config = GetTelemetryConfig ();
496
517
auto Tool = vendor_code::TestTelemeter::createInstance (Config.get ());
497
518
@@ -502,10 +523,12 @@ TEST(TelemetryTest, TelemetryEnabled) {
502
523
const std::string ToolName = " TelemetryTest" ;
503
524
504
525
// Preset some test params.
505
- HasVendorConfig = true ;
506
- SanitizeData = false ;
507
- Buffer.clear ();
508
- EmittedJsons.clear ();
526
+ TestContext Context;
527
+ Context.HasVendorConfig = true ;
528
+ Context.SanitizeData = false ;
529
+ Context.Buffer .clear ();
530
+ Context.EmittedJsons .clear ();
531
+ CurrentContext = &Context;
509
532
510
533
std::shared_ptr<llvm::telemetry::Config> Config = GetTelemetryConfig ();
511
534
@@ -520,56 +543,59 @@ TEST(TelemetryTest, TelemetryEnabled) {
520
543
AtToolExit (ToolName, Tool.get ());
521
544
522
545
// Check that the Tool uses the expected UUID.
523
- EXPECT_STREQ (Tool->getUuid ().c_str (), ExpectedUuid.c_str ());
546
+ EXPECT_STREQ (Tool->getUuid ().c_str (), CurrentContext-> ExpectedUuid .c_str ());
524
547
525
548
// Check that the StringDestination emitted properly
526
549
{
527
550
std::string ExpectedBuffer =
528
- (" SessionId:" + llvm::Twine (ExpectedUuid) + " \n " +
529
- " MagicStartupMsg:One_" + llvm::Twine (ToolName) + " \n " + " SessionId:" +
530
- llvm::Twine (ExpectedUuid) + " \n " + " MSG_0:Two\n " + " MSG_1:Deux\n " +
531
- " MSG_2:Zwei\n " + " SessionId:" + llvm::Twine (ExpectedUuid) + " \n " +
551
+ (" SessionId:" + llvm::Twine (CurrentContext->ExpectedUuid ) + " \n " +
552
+ " MagicStartupMsg:One_" + llvm::Twine (ToolName) + " \n " +
553
+ " SessionId:" + llvm::Twine (CurrentContext->ExpectedUuid ) + " \n " +
554
+ " MSG_0:Two\n " + " MSG_1:Deux\n " + " MSG_2:Zwei\n " +
555
+ " SessionId:" + llvm::Twine (CurrentContext->ExpectedUuid ) + " \n " +
532
556
" MagicExitMsg:Three_" + llvm::Twine (ToolName) + " \n " )
533
557
.str ();
534
558
535
- EXPECT_STREQ (ExpectedBuffer.c_str (), Buffer.c_str ());
559
+ EXPECT_STREQ (ExpectedBuffer.c_str (), CurrentContext-> Buffer .c_str ());
536
560
}
537
561
538
562
// Check that the JsonDestination emitted properly
539
563
{
540
564
541
565
// There should be 3 events emitted by the Telemeter (start, midpoint, exit)
542
- EXPECT_EQ (3 , EmittedJsons.size ());
566
+ EXPECT_EQ (3 , CurrentContext-> EmittedJsons .size ());
543
567
544
- const json::Value *StartupEntry = EmittedJsons[0 ].get (" Startup" );
568
+ const json::Value *StartupEntry =
569
+ CurrentContext->EmittedJsons [0 ].get (" Startup" );
545
570
ASSERT_NE (StartupEntry, nullptr );
546
- EXPECT_STREQ (( " [[ \" SessionId \" , \" " + llvm::Twine (ExpectedUuid) +
547
- " \" ],[ \" MagicStartupMsg \" ,\" One_ " + llvm::Twine (ToolName ) +
548
- " \" ]]" )
549
- .str ()
550
- .c_str (),
551
- ValueToString (StartupEntry).c_str ());
571
+ EXPECT_STREQ (
572
+ ( " [[ \" SessionId \" ,\" " + llvm::Twine (CurrentContext-> ExpectedUuid ) +
573
+ " \" ],[ \" MagicStartupMsg \" , \" One_ " + llvm::Twine (ToolName) + " \" ]]" )
574
+ .str ()
575
+ .c_str (),
576
+ ValueToString (StartupEntry).c_str ());
552
577
553
- const json::Value *MidpointEntry = EmittedJsons[1 ].get (" Midpoint" );
578
+ const json::Value *MidpointEntry =
579
+ CurrentContext->EmittedJsons [1 ].get (" Midpoint" );
554
580
ASSERT_NE (MidpointEntry, nullptr );
555
581
// TODO: This is a bit flaky in that the json string printer sort the
556
582
// entries (for now), so the "UUID" field is put at the end of the array
557
583
// even though it was emitted first.
558
584
EXPECT_STREQ ((" {\" MSG_0\" :\" Two\" ,\" MSG_1\" :\" Deux\" ,\" MSG_2\" :\" Zwei\" ,"
559
585
" \" SessionId\" :\" " +
560
- llvm::Twine (ExpectedUuid) + " \" }" )
586
+ llvm::Twine (CurrentContext-> ExpectedUuid ) + " \" }" )
561
587
.str ()
562
588
.c_str (),
563
589
ValueToString (MidpointEntry).c_str ());
564
590
565
- const json::Value *ExitEntry = EmittedJsons[2 ].get (" Exit" );
591
+ const json::Value *ExitEntry = CurrentContext-> EmittedJsons [2 ].get (" Exit" );
566
592
ASSERT_NE (ExitEntry, nullptr );
567
- EXPECT_STREQ (( " [[ \" SessionId \" , \" " + llvm::Twine (ExpectedUuid) +
568
- " \" ],[ \" MagicExitMsg \" ,\" Three_ " + llvm::Twine (ToolName ) +
569
- " \" ]]" )
570
- .str ()
571
- .c_str (),
572
- ValueToString (ExitEntry).c_str ());
593
+ EXPECT_STREQ (
594
+ ( " [[ \" SessionId \" ,\" " + llvm::Twine (CurrentContext-> ExpectedUuid ) +
595
+ " \" ],[ \" MagicExitMsg \" , \" Three_ " + llvm::Twine (ToolName) + " \" ]]" )
596
+ .str ()
597
+ .c_str (),
598
+ ValueToString (ExitEntry).c_str ());
573
599
}
574
600
}
575
601
@@ -579,10 +605,12 @@ TEST(TelemetryTest, TelemetryEnabledSanitizeData) {
579
605
const std::string ToolName = " TelemetryTest_SanitizedData" ;
580
606
581
607
// Preset some test params.
582
- HasVendorConfig = true ;
583
- SanitizeData = true ;
584
- Buffer.clear ();
585
- EmittedJsons.clear ();
608
+ TestContext Context;
609
+ Context.HasVendorConfig = true ;
610
+ Context.SanitizeData = true ;
611
+ Context.Buffer .clear ();
612
+ Context.EmittedJsons .clear ();
613
+ CurrentContext = &Context;
586
614
587
615
std::shared_ptr<llvm::telemetry::Config> Config = GetTelemetryConfig ();
588
616
@@ -599,51 +627,53 @@ TEST(TelemetryTest, TelemetryEnabledSanitizeData) {
599
627
// Check that the StringDestination emitted properly
600
628
{
601
629
// The StringDestination should have removed the odd-positioned msgs.
602
-
603
630
std::string ExpectedBuffer =
604
- (" SessionId:" + llvm::Twine (ExpectedUuid) + " \n " +
631
+ (" SessionId:" + llvm::Twine (CurrentContext-> ExpectedUuid ) + " \n " +
605
632
" MagicStartupMsg:One_" + llvm::Twine (ToolName) + " \n " +
606
- " SessionId:" + llvm::Twine (ExpectedUuid) + " \n " + " MSG_0:Two\n " +
607
- " MSG_1:\n " + // <<< was sanitized away.
608
- " MSG_2:Zwei\n " + " SessionId:" + llvm::Twine (ExpectedUuid) + " \n " +
633
+ " SessionId:" + llvm::Twine (CurrentContext->ExpectedUuid ) + " \n " +
634
+ " MSG_0:Two\n " + " MSG_1:\n " + // <<< was sanitized away.
635
+ " MSG_2:Zwei\n " +
636
+ " SessionId:" + llvm::Twine (CurrentContext->ExpectedUuid ) + " \n " +
609
637
" MagicExitMsg:Three_" + llvm::Twine (ToolName) + " \n " )
610
638
.str ();
611
- EXPECT_STREQ (ExpectedBuffer.c_str (), Buffer.c_str ());
639
+ EXPECT_STREQ (ExpectedBuffer.c_str (), CurrentContext-> Buffer .c_str ());
612
640
}
613
641
614
642
// Check that the JsonDestination emitted properly
615
643
{
616
644
617
645
// There should be 3 events emitted by the Telemeter (start, midpoint, exit)
618
- EXPECT_EQ (3 , EmittedJsons.size ());
646
+ EXPECT_EQ (3 , CurrentContext-> EmittedJsons .size ());
619
647
620
- const json::Value *StartupEntry = EmittedJsons[0 ].get (" Startup" );
648
+ const json::Value *StartupEntry =
649
+ CurrentContext->EmittedJsons [0 ].get (" Startup" );
621
650
ASSERT_NE (StartupEntry, nullptr );
622
- EXPECT_STREQ (( " [[ \" SessionId \" , \" " + llvm::Twine (ExpectedUuid) +
623
- " \" ],[ \" MagicStartupMsg \" ,\" One_ " + llvm::Twine (ToolName ) +
624
- " \" ]]" )
625
- .str ()
626
- .c_str (),
627
- ValueToString (StartupEntry).c_str ());
651
+ EXPECT_STREQ (
652
+ ( " [[ \" SessionId \" ,\" " + llvm::Twine (CurrentContext-> ExpectedUuid ) +
653
+ " \" ],[ \" MagicStartupMsg \" , \" One_ " + llvm::Twine (ToolName) + " \" ]]" )
654
+ .str ()
655
+ .c_str (),
656
+ ValueToString (StartupEntry).c_str ());
628
657
629
- const json::Value *MidpointEntry = EmittedJsons[1 ].get (" Midpoint" );
658
+ const json::Value *MidpointEntry =
659
+ CurrentContext->EmittedJsons [1 ].get (" Midpoint" );
630
660
ASSERT_NE (MidpointEntry, nullptr );
631
661
// The JsonDestination should have removed the even-positioned msgs.
632
662
EXPECT_STREQ (
633
663
(" {\" MSG_0\" :\"\" ,\" MSG_1\" :\" Deux\" ,\" MSG_2\" :\"\" ,\" SessionId\" :\" " +
634
- llvm::Twine (ExpectedUuid) + " \" }" )
664
+ llvm::Twine (CurrentContext-> ExpectedUuid ) + " \" }" )
635
665
.str ()
636
666
.c_str (),
637
667
ValueToString (MidpointEntry).c_str ());
638
668
639
- const json::Value *ExitEntry = EmittedJsons[2 ].get (" Exit" );
669
+ const json::Value *ExitEntry = CurrentContext-> EmittedJsons [2 ].get (" Exit" );
640
670
ASSERT_NE (ExitEntry, nullptr );
641
- EXPECT_STREQ (( " [[ \" SessionId \" , \" " + llvm::Twine (ExpectedUuid) +
642
- " \" ],[ \" MagicExitMsg \" ,\" Three_ " + llvm::Twine (ToolName ) +
643
- " \" ]]" )
644
- .str ()
645
- .c_str (),
646
- ValueToString (ExitEntry).c_str ());
671
+ EXPECT_STREQ (
672
+ ( " [[ \" SessionId \" ,\" " + llvm::Twine (CurrentContext-> ExpectedUuid ) +
673
+ " \" ],[ \" MagicExitMsg \" , \" Three_ " + llvm::Twine (ToolName) + " \" ]]" )
674
+ .str ()
675
+ .c_str (),
676
+ ValueToString (ExitEntry).c_str ());
647
677
}
648
678
}
649
679
0 commit comments