github.com/newrelic/go-agent@v3.26.0+incompatible/internal/rules_cache_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package internal
     5  
     6  import "testing"
     7  
     8  func TestRulesCache(t *testing.T) {
     9  	testcases := []struct {
    10  		input  string
    11  		isWeb  bool
    12  		output string
    13  	}{
    14  		{input: "name1", isWeb: true, output: "WebTransaction/Go/name1"},
    15  		{input: "name1", isWeb: false, output: "OtherTransaction/Go/name1"},
    16  		{input: "name2", isWeb: true, output: "WebTransaction/Go/name2"},
    17  		{input: "name3", isWeb: true, output: "WebTransaction/Go/name3"},
    18  		{input: "zap/123/zip", isWeb: false, output: "OtherTransaction/Go/zap/*/zip"},
    19  		{input: "zap/45/zip", isWeb: false, output: "OtherTransaction/Go/zap/*/zip"},
    20  	}
    21  
    22  	cache := newRulesCache(len(testcases))
    23  	for _, tc := range testcases {
    24  		// Test that nothing is in the cache before population.
    25  		if out := cache.find(tc.input, tc.isWeb); out != "" {
    26  			t.Error(out, tc.input, tc.isWeb)
    27  		}
    28  	}
    29  	for _, tc := range testcases {
    30  		cache.set(tc.input, tc.isWeb, tc.output)
    31  	}
    32  	for _, tc := range testcases {
    33  		// Test that everything is now in the cache as expected.
    34  		if out := cache.find(tc.input, tc.isWeb); out != tc.output {
    35  			t.Error(out, tc.input, tc.isWeb, tc.output)
    36  		}
    37  	}
    38  }
    39  
    40  func TestRulesCacheLimit(t *testing.T) {
    41  	cache := newRulesCache(1)
    42  	cache.set("name1", true, "WebTransaction/Go/name1")
    43  	cache.set("name1", false, "OtherTransaction/Go/name1")
    44  	if out := cache.find("name1", true); out != "WebTransaction/Go/name1" {
    45  		t.Error(out)
    46  	}
    47  	if out := cache.find("name1", false); out != "" {
    48  		t.Error(out)
    49  	}
    50  }
    51  
    52  func TestRulesCacheNil(t *testing.T) {
    53  	var cache *rulesCache
    54  	// No panics should happen if the rules cache pointer is nil.
    55  	if out := cache.find("name1", true); "" != out {
    56  		t.Error(out)
    57  	}
    58  	cache.set("name1", false, "OtherTransaction/Go/name1")
    59  }