github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/connect_reply_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestCreateFullTxnNameBasic(t *testing.T) {
    10  	emptyReply := ConnectReplyDefaults()
    11  
    12  	tcs := []struct {
    13  		input      string
    14  		background bool
    15  		expect     string
    16  	}{
    17  		{"", true, "WebTransaction/Go/"},
    18  		{"/", true, "WebTransaction/Go/"},
    19  		{"hello", true, "WebTransaction/Go/hello"},
    20  		{"/hello", true, "WebTransaction/Go/hello"},
    21  
    22  		{"", false, "OtherTransaction/Go/"},
    23  		{"/", false, "OtherTransaction/Go/"},
    24  		{"hello", false, "OtherTransaction/Go/hello"},
    25  		{"/hello", false, "OtherTransaction/Go/hello"},
    26  	}
    27  
    28  	for _, tc := range tcs {
    29  		if out := CreateFullTxnName(tc.input, emptyReply, tc.background); out != tc.expect {
    30  			t.Error(tc.input, tc.background, out, tc.expect)
    31  		}
    32  	}
    33  }
    34  
    35  func TestCreateFullTxnNameURLRulesIgnore(t *testing.T) {
    36  	js := `[{
    37  		"match_expression":".*zip.*$",
    38  		"ignore":true
    39  	}]`
    40  	reply := ConnectReplyDefaults()
    41  	err := json.Unmarshal([]byte(js), &reply.URLRules)
    42  	if nil != err {
    43  		t.Fatal(err)
    44  	}
    45  	if out := CreateFullTxnName("/zap/zip/zep", reply, true); out != "" {
    46  		t.Error(out)
    47  	}
    48  }
    49  
    50  func TestCreateFullTxnNameTxnRulesIgnore(t *testing.T) {
    51  	js := `[{
    52  		"match_expression":"^WebTransaction/Go/zap/zip/zep$",
    53  		"ignore":true
    54  	}]`
    55  	reply := ConnectReplyDefaults()
    56  	err := json.Unmarshal([]byte(js), &reply.TxnNameRules)
    57  	if nil != err {
    58  		t.Fatal(err)
    59  	}
    60  	if out := CreateFullTxnName("/zap/zip/zep", reply, true); out != "" {
    61  		t.Error(out)
    62  	}
    63  }
    64  
    65  func TestCreateFullTxnNameAllRules(t *testing.T) {
    66  	js := `{
    67  		"url_rules":[
    68  			{"match_expression":"zip","each_segment":true,"replacement":"zoop"}
    69  		],
    70  		"transaction_name_rules":[
    71  			{"match_expression":"WebTransaction/Go/zap/zoop/zep",
    72  			 "replacement":"WebTransaction/Go/zap/zoop/zep/zup/zyp"}
    73  		],
    74  		"transaction_segment_terms":[
    75  			{"prefix": "WebTransaction/Go/",
    76  			 "terms": ["zyp", "zoop", "zap"]}
    77  		]
    78  	}`
    79  	reply := ConnectReplyDefaults()
    80  	err := json.Unmarshal([]byte(js), &reply)
    81  	if nil != err {
    82  		t.Fatal(err)
    83  	}
    84  	if out := CreateFullTxnName("/zap/zip/zep", reply, true); out != "WebTransaction/Go/zap/zoop/*/zyp" {
    85  		t.Error(out)
    86  	}
    87  }
    88  
    89  func TestCalculateApdexThreshold(t *testing.T) {
    90  	reply := ConnectReplyDefaults()
    91  	threshold := CalculateApdexThreshold(reply, "WebTransaction/Go/hello")
    92  	if threshold != 500*time.Millisecond {
    93  		t.Error("default apdex threshold", threshold)
    94  	}
    95  
    96  	reply = ConnectReplyDefaults()
    97  	reply.ApdexThresholdSeconds = 1.3
    98  	reply.KeyTxnApdex = map[string]float64{
    99  		"WebTransaction/Go/zip": 2.2,
   100  		"WebTransaction/Go/zap": 2.3,
   101  	}
   102  	threshold = CalculateApdexThreshold(reply, "WebTransaction/Go/hello")
   103  	if threshold != 1300*time.Millisecond {
   104  		t.Error(threshold)
   105  	}
   106  	threshold = CalculateApdexThreshold(reply, "WebTransaction/Go/zip")
   107  	if threshold != 2200*time.Millisecond {
   108  		t.Error(threshold)
   109  	}
   110  }
   111  
   112  func TestIsTrusted(t *testing.T) {
   113  	for _, test := range []struct {
   114  		id       int
   115  		trusted  string
   116  		expected bool
   117  	}{
   118  		{1, `[]`, false},
   119  		{1, `[2, 3]`, false},
   120  		{1, `[1]`, true},
   121  		{1, `[1, 2, 3]`, true},
   122  	} {
   123  		trustedAccounts := make(trustedAccountSet)
   124  		if err := json.Unmarshal([]byte(test.trusted), &trustedAccounts); err != nil {
   125  			t.Fatal(err)
   126  		}
   127  
   128  		if actual := trustedAccounts.IsTrusted(test.id); test.expected != actual {
   129  			t.Errorf("failed asserting whether %d is trusted by %v: expected %v; got %v", test.id, test.trusted, test.expected, actual)
   130  		}
   131  	}
   132  }