@@ -494,6 +494,48 @@ func TestInvalidQueryTextAndPlanQuery(t *testing.T) {
494494 assert .NoError (t , errs )
495495}
496496
497+ func TestCacheIfNewPlan (t * testing.T ) {
498+ cfg := createDefaultConfig ().(* Config )
499+ cfg .Username = "sa"
500+ cfg .Password = "password"
501+ cfg .Port = 1433
502+ cfg .Server = "0.0.0.0"
503+ cfg .MetricsBuilderConfig .ResourceAttributes .SqlserverInstanceName .Enabled = true
504+ cfg .Events .DbServerTopQuery .Enabled = true
505+ assert .NoError (t , cfg .Validate ())
506+
507+ configureAllScraperMetricsAndEvents (cfg , false )
508+ cfg .Events .DbServerTopQuery .Enabled = true
509+ cfg .TopQueryCollection .CollectionInterval = cfg .ControllerConfig .CollectionInterval
510+
511+ scrapers := setupSQLServerLogsScrapers (receivertest .NewNopSettings (metadata .Type ), cfg )
512+ assert .NotNil (t , scrapers )
513+
514+ scraper := scrapers [0 ]
515+ assert .NotNil (t , scraper .planCache )
516+
517+ scraper .planCache .Add ("query-hash-1" , "plan-hash-1" )
518+
519+ //Test existing values.
520+ isPlan1New := scraper .cacheIfNewPlan ("query-hash-1" , "plan-hash-1" )
521+ assert .False (t , isPlan1New , "Should be False because query-hash-1 already exists in the cache" )
522+ value1 , _ := scraper .planCache .Get ("query-hash-1" )
523+ assert .Equal (t , "plan-hash-1" , value1 , "The existing value in cache should be 'plan-hash-1'" )
524+
525+ //Test adding new values.
526+ isPlan2New := scraper .cacheIfNewPlan ("query-hash-2" , "plan-hash-2" )
527+ assert .True (t , isPlan2New , "Should be true because 'query-hash-2' does not already exist" )
528+ value2 , _ := scraper .planCache .Get ("query-hash-2" )
529+ assert .Equal (t , "plan-hash-2" , value2 , "The new 'plan-hash-2' should have added into the cache" )
530+
531+ //Test updating existing values.
532+ isPlan3New := scraper .cacheIfNewPlan ("query-hash-1" , "plan-hash-3" )
533+ assert .True (t , isPlan3New , "Should be true because plan hash 'plan-hash-3' is new for 'query-hash-1'" )
534+ value3 , _ := scraper .planCache .Get ("query-hash-1" )
535+ assert .Equal (t , "plan-hash-3" , value3 , "The value for key 'query-hash-1' should be now updated to 'plan-hash-3'" )
536+
537+ }
538+
497539func TestRecordDatabaseSampleQuery (t * testing.T ) {
498540 tests := map [string ]struct {
499541 expectedFile string
0 commit comments