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

     1  package newrelic
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/lulzWill/go-agent/internal"
     8  	"github.com/lulzWill/go-agent/internal/cat"
     9  )
    10  
    11  func TestShouldSaveTrace(t *testing.T) {
    12  	for _, tc := range []struct {
    13  		name          string
    14  		expected      bool
    15  		synthetics    bool
    16  		tracerEnabled bool
    17  		collectTraces bool
    18  		duration      time.Duration
    19  		threshold     time.Duration
    20  	}{
    21  		{
    22  			name:          "insufficient duration, all disabled",
    23  			expected:      false,
    24  			synthetics:    false,
    25  			tracerEnabled: false,
    26  			collectTraces: false,
    27  			duration:      1 * time.Second,
    28  			threshold:     2 * time.Second,
    29  		},
    30  		{
    31  			name:          "insufficient duration, only synthetics enabled",
    32  			expected:      true,
    33  			synthetics:    true,
    34  			tracerEnabled: false,
    35  			collectTraces: false,
    36  			duration:      1 * time.Second,
    37  			threshold:     2 * time.Second,
    38  		},
    39  		{
    40  			name:          "insufficient duration, only tracer enabled",
    41  			expected:      false,
    42  			synthetics:    false,
    43  			tracerEnabled: true,
    44  			collectTraces: false,
    45  			duration:      1 * time.Second,
    46  			threshold:     2 * time.Second,
    47  		},
    48  		{
    49  			name:          "insufficient duration, only collect traces enabled",
    50  			expected:      false,
    51  			synthetics:    false,
    52  			tracerEnabled: false,
    53  			collectTraces: true,
    54  			duration:      1 * time.Second,
    55  			threshold:     2 * time.Second,
    56  		},
    57  		{
    58  			name:          "insufficient duration, all normal flags enabled",
    59  			expected:      false,
    60  			synthetics:    false,
    61  			tracerEnabled: true,
    62  			collectTraces: true,
    63  			duration:      1 * time.Second,
    64  			threshold:     2 * time.Second,
    65  		},
    66  		{
    67  			name:          "insufficient duration, all flags enabled",
    68  			expected:      true,
    69  			synthetics:    true,
    70  			tracerEnabled: true,
    71  			collectTraces: true,
    72  			duration:      1 * time.Second,
    73  			threshold:     2 * time.Second,
    74  		},
    75  		{
    76  			name:          "sufficient duration, all disabled",
    77  			expected:      false,
    78  			synthetics:    false,
    79  			tracerEnabled: false,
    80  			collectTraces: false,
    81  			duration:      3 * time.Second,
    82  			threshold:     2 * time.Second,
    83  		},
    84  		{
    85  			name:          "sufficient duration, only synthetics enabled",
    86  			expected:      true,
    87  			synthetics:    true,
    88  			tracerEnabled: false,
    89  			collectTraces: false,
    90  			duration:      3 * time.Second,
    91  			threshold:     2 * time.Second,
    92  		},
    93  		{
    94  			name:          "sufficient duration, only tracer enabled",
    95  			expected:      false,
    96  			synthetics:    false,
    97  			tracerEnabled: true,
    98  			collectTraces: false,
    99  			duration:      3 * time.Second,
   100  			threshold:     2 * time.Second,
   101  		},
   102  		{
   103  			name:          "sufficient duration, only collect traces enabled",
   104  			expected:      false,
   105  			synthetics:    false,
   106  			tracerEnabled: false,
   107  			collectTraces: true,
   108  			duration:      3 * time.Second,
   109  			threshold:     2 * time.Second,
   110  		},
   111  		{
   112  			name:          "sufficient duration, all normal flags enabled",
   113  			expected:      true,
   114  			synthetics:    false,
   115  			tracerEnabled: true,
   116  			collectTraces: true,
   117  			duration:      3 * time.Second,
   118  			threshold:     2 * time.Second,
   119  		},
   120  		{
   121  			name:          "sufficient duration, all flags enabled",
   122  			expected:      true,
   123  			synthetics:    true,
   124  			tracerEnabled: true,
   125  			collectTraces: true,
   126  			duration:      3 * time.Second,
   127  			threshold:     2 * time.Second,
   128  		},
   129  	} {
   130  		txn := &txn{}
   131  		txn.Config.TransactionTracer.Enabled = tc.tracerEnabled
   132  		txn.Config.TransactionTracer.Threshold.Duration = tc.threshold
   133  		txn.Reply = &internal.ConnectReply{CollectTraces: tc.collectTraces}
   134  		txn.Duration = tc.duration
   135  		if tc.synthetics {
   136  			txn.CrossProcess.Synthetics = &cat.SyntheticsHeader{}
   137  			txn.CrossProcess.SetSynthetics(tc.synthetics)
   138  		}
   139  
   140  		if actual := txn.shouldSaveTrace(); actual != tc.expected {
   141  			t.Errorf("%s: unexpected shouldSaveTrace value; expected %v; got %v", tc.name, tc.expected, actual)
   142  		}
   143  	}
   144  }