github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/segment_terms_test.go (about) 1 package internal 2 3 import ( 4 "encoding/json" 5 "strings" 6 "testing" 7 8 "github.com/lulzWill/go-agent/internal/crossagent" 9 ) 10 11 func TestCrossAgentSegmentTerms(t *testing.T) { 12 var tcs []struct { 13 Testname string `json:"testname"` 14 Rules segmentRules `json:"transaction_segment_terms"` 15 Tests []struct { 16 Input string `json:"input"` 17 Expected string `json:"expected"` 18 } `json:"tests"` 19 } 20 21 err := crossagent.ReadJSON("transaction_segment_terms.json", &tcs) 22 if err != nil { 23 t.Fatal(err) 24 } 25 26 for _, tc := range tcs { 27 for _, test := range tc.Tests { 28 out := tc.Rules.apply(test.Input) 29 if out != test.Expected { 30 t.Fatal(tc.Testname, test.Input, out, test.Expected) 31 } 32 } 33 } 34 } 35 36 func TestSegmentTerms(t *testing.T) { 37 js := `[ 38 { 39 "prefix":"WebTransaction\/Uri", 40 "terms":[ 41 "two", 42 "Users", 43 "willhf", 44 "dev", 45 "php", 46 "one", 47 "alpha", 48 "zap" 49 ] 50 } 51 ]` 52 var rules segmentRules 53 if err := json.Unmarshal([]byte(js), &rules); nil != err { 54 t.Fatal(err) 55 } 56 57 out := rules.apply("WebTransaction/Uri/pen/two/pencil/dev/paper") 58 if out != "WebTransaction/Uri/*/two/*/dev/*" { 59 t.Fatal(out) 60 } 61 } 62 63 func TestEmptySegmentTerms(t *testing.T) { 64 var rules segmentRules 65 66 input := "my/name" 67 out := rules.apply(input) 68 if out != input { 69 t.Error(input, out) 70 } 71 } 72 73 func BenchmarkSegmentTerms(b *testing.B) { 74 js := `[ 75 { 76 "prefix":"WebTransaction\/Uri", 77 "terms":[ 78 "two", 79 "Users", 80 "willhf", 81 "dev", 82 "php", 83 "one", 84 "alpha", 85 "zap" 86 ] 87 } 88 ]` 89 var rules segmentRules 90 if err := json.Unmarshal([]byte(js), &rules); nil != err { 91 b.Fatal(err) 92 } 93 94 b.ResetTimer() 95 b.ReportAllocs() 96 97 input := "WebTransaction/Uri/pen/two/pencil/dev/paper" 98 expected := "WebTransaction/Uri/*/two/*/dev/*" 99 for i := 0; i < b.N; i++ { 100 out := rules.apply(input) 101 if out != expected { 102 b.Fatal(out, expected) 103 } 104 } 105 } 106 107 func TestCollapsePlaceholders(t *testing.T) { 108 testcases := []struct { 109 input string 110 expect string 111 }{ 112 {input: "", expect: ""}, 113 {input: "/", expect: "/"}, 114 {input: "*", expect: "*"}, 115 {input: "*/*", expect: "*"}, 116 {input: "a/b/c", expect: "a/b/c"}, 117 {input: "*/*/*", expect: "*"}, 118 {input: "a/*/*/*/b", expect: "a/*/b"}, 119 {input: "a/b/*/*/*/", expect: "a/b/*/"}, 120 {input: "a/b/*/*/*", expect: "a/b/*"}, 121 {input: "*/*/a/b/*/*/*", expect: "*/a/b/*"}, 122 {input: "*/*/a/b/*/c/*/*/d/e/*/*/*", expect: "*/a/b/*/c/*/d/e/*"}, 123 {input: "a/*/b", expect: "a/*/b"}, 124 } 125 126 for _, tc := range testcases { 127 segments := strings.Split(tc.input, "/") 128 segments = collapsePlaceholders(segments) 129 out := strings.Join(segments, "/") 130 if out != tc.expect { 131 t.Error(tc.input, tc.expect, out) 132 } 133 } 134 }