github.com/pinpoint-apm/pinpoint-go-agent@v1.4.1-0.20240110120318-a50c2eb18c8c/agent_test.go (about)

     1  package pinpoint
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"testing"
     6  )
     7  
     8  func Test_agent_NewAgentError(t *testing.T) {
     9  	tests := []struct {
    10  		name string
    11  	}{
    12  		{"1"},
    13  	}
    14  	for _, tt := range tests {
    15  		t.Run(tt.name, func(t *testing.T) {
    16  			a, err := NewAgent(nil)
    17  			assert.Equal(t, NoopAgent(), a, "noop agent")
    18  			assert.Error(t, err, "error")
    19  		})
    20  	}
    21  }
    22  
    23  func Test_agent_NewAgent(t *testing.T) {
    24  	type args struct {
    25  		config *Config
    26  	}
    27  
    28  	opts := []ConfigOption{
    29  		WithAppName("test"),
    30  		WithAgentId("testagent"),
    31  	}
    32  	c, _ := NewConfig(opts...)
    33  	c.offGrpc = true
    34  
    35  	tests := []struct {
    36  		name string
    37  		args args
    38  	}{
    39  		{"1", args{c}},
    40  	}
    41  	for _, tt := range tests {
    42  		t.Run(tt.name, func(t *testing.T) {
    43  			c := tt.args.config
    44  			a, err := NewAgent(c)
    45  			agent := a.(*agent)
    46  			assert.NoError(t, err, "NewAgent")
    47  			assert.Equal(t, "test", agent.appName, "ApplicationName")
    48  			assert.Equal(t, "testagent", agent.agentID, "AgentID")
    49  			assert.Equal(t, int32(ServiceTypeGoApp), agent.appType, "ApplicationType")
    50  			assert.Greater(t, agent.startTime, int64(0), "StartTime")
    51  			assert.Equal(t, globalAgent, a, "global agent")
    52  
    53  			agent.startTime = 12345
    54  			agent.enable = true
    55  			assert.Equal(t, "testagent^12345^1", agent.generateTransactionId().String(), "generateTransactionId")
    56  
    57  			a.Shutdown()
    58  			assert.Equal(t, NoopAgent(), globalAgent, "global agent")
    59  			assert.Equal(t, false, a.Enable(), "Enable")
    60  
    61  			span := agent.NewSpanTracer("test", "/")
    62  			assert.Equal(t, NoopTracer(), span, "NewSpanTracer")
    63  		})
    64  	}
    65  }
    66  
    67  func Test_agent_GlobalAgent(t *testing.T) {
    68  	type args struct {
    69  		config *Config
    70  	}
    71  
    72  	opts := []ConfigOption{
    73  		WithAppName("testGlobal"),
    74  		WithAgentId("testGlobalAgent"),
    75  	}
    76  	c, _ := NewConfig(opts...)
    77  	c.offGrpc = true
    78  	a, _ := NewAgent(c)
    79  	agent := a.(*agent)
    80  	agent.enable = true
    81  	defer a.Shutdown()
    82  
    83  	tests := []struct {
    84  		name string
    85  		args args
    86  	}{
    87  		{"1", args{c}},
    88  	}
    89  	for _, tt := range tests {
    90  		t.Run(tt.name, func(t *testing.T) {
    91  			assert.Equal(t, globalAgent, a, "global agent")
    92  			assert.NotEqual(t, globalAgent, NoopAgent(), "global agent")
    93  
    94  			a, err := NewAgent(c)
    95  			assert.Error(t, err, "NewAgent")
    96  			assert.Equal(t, globalAgent, a, "global agent")
    97  		})
    98  	}
    99  }
   100  
   101  func Test_agent_NewSpanTracer(t *testing.T) {
   102  	type args struct {
   103  		agent Agent
   104  	}
   105  
   106  	opts := []ConfigOption{
   107  		WithAppName("test"),
   108  		WithAgentId("testagent"),
   109  	}
   110  	c, _ := NewConfig(opts...)
   111  	c.offGrpc = true
   112  	a, _ := NewAgent(c)
   113  	agent := a.(*agent)
   114  	agent.enable = true
   115  	defer a.Shutdown()
   116  
   117  	tests := []struct {
   118  		name string
   119  		args args
   120  	}{
   121  		{"1", args{agent}},
   122  	}
   123  	for _, tt := range tests {
   124  		t.Run(tt.name, func(t *testing.T) {
   125  			agent := tt.args.agent
   126  			span := agent.NewSpanTracer("test", "/")
   127  
   128  			txid := span.TransactionId()
   129  			assert.Equal(t, "testagent", txid.AgentId, "AgentId")
   130  			assert.Greater(t, txid.StartTime, int64(0), "StartTime")
   131  			assert.Greater(t, txid.Sequence, int64(0), "Sequence")
   132  
   133  			spanid := span.SpanId()
   134  			assert.NotEqual(t, int64(0), spanid, "spanId")
   135  		})
   136  	}
   137  }
   138  
   139  func Test_agent_NewSpanTracerWithReader(t *testing.T) {
   140  	type args struct {
   141  		agent  Agent
   142  		reader DistributedTracingContextReader
   143  	}
   144  
   145  	opts := []ConfigOption{
   146  		WithAppName("test"),
   147  		WithAgentId("testagent"),
   148  	}
   149  	c, _ := NewConfig(opts...)
   150  	c.offGrpc = true
   151  	a, _ := NewAgent(c)
   152  	agent := a.(*agent)
   153  	agent.enable = true
   154  	defer a.Shutdown()
   155  
   156  	m := map[string]string{
   157  		HeaderTraceId:      "t123456^12345^1",
   158  		HeaderSpanId:       "67890",
   159  		HeaderParentSpanId: "123",
   160  	}
   161  
   162  	tests := []struct {
   163  		name string
   164  		args args
   165  	}{
   166  		{"1", args{agent, &DistributedTracingContextMap{m}}},
   167  	}
   168  	for _, tt := range tests {
   169  		t.Run(tt.name, func(t *testing.T) {
   170  			agent := tt.args.agent
   171  			span := agent.NewSpanTracerWithReader("test", "/", tt.args.reader)
   172  
   173  			txId := span.TransactionId()
   174  			assert.Equal(t, "t123456", txId.AgentId, "AgentId")
   175  			assert.Equal(t, int64(12345), txId.StartTime, "StartTime")
   176  			assert.Equal(t, int64(1), txId.Sequence, "Sequence")
   177  			assert.Equal(t, int64(67890), span.SpanId(), "SpanId")
   178  		})
   179  	}
   180  }