2525
2626// Testing parameters.
2727// 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 ;
3754
3855namespace llvm {
3956namespace telemetry {
@@ -289,7 +306,7 @@ class JsonStreamDestination : public Destination {
289306 // send the data to some internal storage.
290307 // For testing purposes, we just queue up the entries to
291308 // the vector for validation.
292- EmittedJsons.push_back (std::move (O));
309+ CurrentContext-> EmittedJsons .push_back (std::move (O));
293310 return Error::success ();
294311 }
295312 bool ShouldSanitize;
@@ -305,19 +322,19 @@ class TestTelemeter : public Telemeter {
305322 << " \n " ;
306323 if (!config->EnableTelemetry )
307324 return nullptr ;
308- ExpectedUuid = nextUuid ();
325+ CurrentContext-> ExpectedUuid = nextUuid ();
309326 std::unique_ptr<TestTelemeter> Telemeter =
310- std::make_unique<TestTelemeter>(ExpectedUuid);
327+ std::make_unique<TestTelemeter>(CurrentContext-> ExpectedUuid );
311328 // Set up Destination based on the given config.
312329 for (const std::string &Dest : config->AdditionalDestinations ) {
313330 // The destination(s) are ALSO defined by vendor, so it should understand
314331 // what the name of each destination signifies.
315332 if (Dest == JSON_DEST) {
316- Telemeter->addDestination (
317- new vendor_code::JsonStreamDestination ( SanitizeData));
333+ Telemeter->addDestination (new vendor_code::JsonStreamDestination (
334+ CurrentContext-> SanitizeData ));
318335 } 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 ));
321338 } else {
322339 llvm_unreachable (
323340 llvm::Twine (" unknown destination: " , Dest).str ().c_str ());
@@ -431,7 +448,7 @@ std::shared_ptr<llvm::telemetry::Config> GetTelemetryConfig() {
431448 // #endif
432449 //
433450 // But for unit testing, we use the testing params defined at the top.
434- if (HasVendorConfig) {
451+ if (CurrentContext-> HasVendorConfig ) {
435452 llvm::telemetry::vendor_code::ApplyVendorSpecificConfigs (Config.get ());
436453 }
437454 return Config;
@@ -468,8 +485,8 @@ void AtToolExit(std::string ToolName, vendor_code::TestTelemeter *T) {
468485 T->makeDefaultTelemetryInfo <vendor_code::ExitEvent>();
469486 Entry.Stats = {ExitTime, ExitCompleteTime};
470487
471- if (HasExitError) {
472- Entry.ExitDesc = {1 , ExitMsg};
488+ if (CurrentContext-> HasExitError ) {
489+ Entry.ExitDesc = {1 , CurrentContext-> ExitMsg };
473490 }
474491 T->logExit (ToolName, &Entry);
475492}
@@ -491,7 +508,11 @@ static std::string ValueToString(const json::Value *V) {
491508
492509// Without vendor's implementation, telemetry is not enabled by default.
493510TEST (TelemetryTest, TelemetryDefault) {
494- HasVendorConfig = false ;
511+ // Preset some test params.
512+ TestContext Context;
513+ Context.HasVendorConfig = false ;
514+ CurrentContext = &Context;
515+
495516 std::shared_ptr<llvm::telemetry::Config> Config = GetTelemetryConfig ();
496517 auto Tool = vendor_code::TestTelemeter::createInstance (Config.get ());
497518
@@ -502,10 +523,12 @@ TEST(TelemetryTest, TelemetryEnabled) {
502523 const std::string ToolName = " TelemetryTest" ;
503524
504525 // 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;
509532
510533 std::shared_ptr<llvm::telemetry::Config> Config = GetTelemetryConfig ();
511534
@@ -520,56 +543,59 @@ TEST(TelemetryTest, TelemetryEnabled) {
520543 AtToolExit (ToolName, Tool.get ());
521544
522545 // 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 ());
524547
525548 // Check that the StringDestination emitted properly
526549 {
527550 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 " +
532556 " MagicExitMsg:Three_" + llvm::Twine (ToolName) + " \n " )
533557 .str ();
534558
535- EXPECT_STREQ (ExpectedBuffer.c_str (), Buffer.c_str ());
559+ EXPECT_STREQ (ExpectedBuffer.c_str (), CurrentContext-> Buffer .c_str ());
536560 }
537561
538562 // Check that the JsonDestination emitted properly
539563 {
540564
541565 // 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 ());
543567
544- const json::Value *StartupEntry = EmittedJsons[0 ].get (" Startup" );
568+ const json::Value *StartupEntry =
569+ CurrentContext->EmittedJsons [0 ].get (" Startup" );
545570 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 ());
552577
553- const json::Value *MidpointEntry = EmittedJsons[1 ].get (" Midpoint" );
578+ const json::Value *MidpointEntry =
579+ CurrentContext->EmittedJsons [1 ].get (" Midpoint" );
554580 ASSERT_NE (MidpointEntry, nullptr );
555581 // TODO: This is a bit flaky in that the json string printer sort the
556582 // entries (for now), so the "UUID" field is put at the end of the array
557583 // even though it was emitted first.
558584 EXPECT_STREQ ((" {\" MSG_0\" :\" Two\" ,\" MSG_1\" :\" Deux\" ,\" MSG_2\" :\" Zwei\" ,"
559585 " \" SessionId\" :\" " +
560- llvm::Twine (ExpectedUuid) + " \" }" )
586+ llvm::Twine (CurrentContext-> ExpectedUuid ) + " \" }" )
561587 .str ()
562588 .c_str (),
563589 ValueToString (MidpointEntry).c_str ());
564590
565- const json::Value *ExitEntry = EmittedJsons[2 ].get (" Exit" );
591+ const json::Value *ExitEntry = CurrentContext-> EmittedJsons [2 ].get (" Exit" );
566592 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 ());
573599 }
574600}
575601
@@ -579,10 +605,12 @@ TEST(TelemetryTest, TelemetryEnabledSanitizeData) {
579605 const std::string ToolName = " TelemetryTest_SanitizedData" ;
580606
581607 // 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;
586614
587615 std::shared_ptr<llvm::telemetry::Config> Config = GetTelemetryConfig ();
588616
@@ -599,51 +627,53 @@ TEST(TelemetryTest, TelemetryEnabledSanitizeData) {
599627 // Check that the StringDestination emitted properly
600628 {
601629 // The StringDestination should have removed the odd-positioned msgs.
602-
603630 std::string ExpectedBuffer =
604- (" SessionId:" + llvm::Twine (ExpectedUuid) + " \n " +
631+ (" SessionId:" + llvm::Twine (CurrentContext-> ExpectedUuid ) + " \n " +
605632 " 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 " +
609637 " MagicExitMsg:Three_" + llvm::Twine (ToolName) + " \n " )
610638 .str ();
611- EXPECT_STREQ (ExpectedBuffer.c_str (), Buffer.c_str ());
639+ EXPECT_STREQ (ExpectedBuffer.c_str (), CurrentContext-> Buffer .c_str ());
612640 }
613641
614642 // Check that the JsonDestination emitted properly
615643 {
616644
617645 // 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 ());
619647
620- const json::Value *StartupEntry = EmittedJsons[0 ].get (" Startup" );
648+ const json::Value *StartupEntry =
649+ CurrentContext->EmittedJsons [0 ].get (" Startup" );
621650 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 ());
628657
629- const json::Value *MidpointEntry = EmittedJsons[1 ].get (" Midpoint" );
658+ const json::Value *MidpointEntry =
659+ CurrentContext->EmittedJsons [1 ].get (" Midpoint" );
630660 ASSERT_NE (MidpointEntry, nullptr );
631661 // The JsonDestination should have removed the even-positioned msgs.
632662 EXPECT_STREQ (
633663 (" {\" MSG_0\" :\"\" ,\" MSG_1\" :\" Deux\" ,\" MSG_2\" :\"\" ,\" SessionId\" :\" " +
634- llvm::Twine (ExpectedUuid) + " \" }" )
664+ llvm::Twine (CurrentContext-> ExpectedUuid ) + " \" }" )
635665 .str ()
636666 .c_str (),
637667 ValueToString (MidpointEntry).c_str ());
638668
639- const json::Value *ExitEntry = EmittedJsons[2 ].get (" Exit" );
669+ const json::Value *ExitEntry = CurrentContext-> EmittedJsons [2 ].get (" Exit" );
640670 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 ());
647677 }
648678}
649679
0 commit comments