@@ -95,22 +95,26 @@ func TestShouldShip_ReasonAge(t *testing.T) {
9595func TestLifecycle (t * testing.T ) {
9696 reqID := "test-req-id"
9797 fnARN := "test-fn-arn"
98- txnID := "023d90ff77f13b9f"
9998 lambdaData := `{"log":{"message":"this is log"}}`
100- txnData := fmt .Sprintf (`{"transaction":{"id":"%s"}}` , txnID )
99+ txnData := fmt .Sprintf (`{"transaction":{"id":"%s"}}` , "023d90ff77f13b9f" )
101100 ts := time .Date (2022 , time .October , 1 , 1 , 1 , 1 , 0 , time .UTC )
102101 txnDur := time .Second
103102
103+ type agentInit struct {
104+ init bool
105+ withMetadata bool
106+ }
107+
104108 for _ , tc := range []struct {
105109 name string
106- agentInit bool
110+ agentInit agentInit
107111 receiveAgentRootTxn bool
108112 receiveLambdaLogRuntime bool
109113 expected string
110114 }{
111115 {
112116 name : "without_agent_init_without_root_txn" ,
113- agentInit : false ,
117+ agentInit : agentInit { init : false , withMetadata : false } ,
114118 receiveAgentRootTxn : false ,
115119 receiveLambdaLogRuntime : false ,
116120 // Without agent init no proxy txn is created if root txn is not reported
@@ -122,7 +126,7 @@ func TestLifecycle(t *testing.T) {
122126 },
123127 {
124128 name : "without_agent_init_with_root_txn" ,
125- agentInit : false ,
129+ agentInit : agentInit { init : false , withMetadata : false } ,
126130 receiveAgentRootTxn : true ,
127131 receiveLambdaLogRuntime : false ,
128132 expected : fmt .Sprintf (
@@ -133,8 +137,8 @@ func TestLifecycle(t *testing.T) {
133137 ),
134138 },
135139 {
136- name : "with_agent_init_with_root_txn " ,
137- agentInit : true ,
140+ name : "with_no_meta_agent_init_with_root_txn " ,
141+ agentInit : agentInit { init : true , withMetadata : false } ,
138142 receiveAgentRootTxn : true ,
139143 receiveLambdaLogRuntime : false ,
140144 expected : fmt .Sprintf (
@@ -145,8 +149,20 @@ func TestLifecycle(t *testing.T) {
145149 ),
146150 },
147151 {
148- name : "with_agent_init_without_root_txn_with_runtimeDone" ,
149- agentInit : true ,
152+ name : "with_meta_agent_init_with_root_txn" ,
153+ agentInit : agentInit {init : true , withMetadata : true },
154+ receiveAgentRootTxn : true ,
155+ receiveLambdaLogRuntime : false ,
156+ expected : fmt .Sprintf (
157+ "%s\n %s\n %s" ,
158+ metadata ,
159+ generateCompleteTxn (t , txnData , "success" , "" , txnDur ),
160+ lambdaData ,
161+ ),
162+ },
163+ {
164+ name : "with_no_meta_agent_init_without_root_txn_with_runtimeDone" ,
165+ agentInit : agentInit {init : true , withMetadata : false },
150166 receiveAgentRootTxn : false ,
151167 receiveLambdaLogRuntime : true ,
152168 // With agent init proxy txn is created if root txn is not reported.
@@ -159,8 +175,37 @@ func TestLifecycle(t *testing.T) {
159175 ),
160176 },
161177 {
162- name : "with_agent_init_without_root_txn" ,
163- agentInit : true ,
178+ name : "with_meta_agent_init_without_root_txn_with_runtimeDone" ,
179+ agentInit : agentInit {init : true , withMetadata : true },
180+ receiveAgentRootTxn : false ,
181+ receiveLambdaLogRuntime : true ,
182+ // With agent init proxy txn is created if root txn is not reported.
183+ // Details in runtimeDone event is used to find the result of the txn.
184+ expected : fmt .Sprintf (
185+ "%s\n %s\n %s" ,
186+ metadata ,
187+ lambdaData ,
188+ generateCompleteTxn (t , txnData , "failure" , "failure" , txnDur ),
189+ ),
190+ },
191+ {
192+ name : "with_no_meta_agent_init_without_root_txn" ,
193+ agentInit : agentInit {init : true , withMetadata : false },
194+ receiveAgentRootTxn : false ,
195+ receiveLambdaLogRuntime : false ,
196+ // With agent init proxy txn is created if root txn is not reported.
197+ // If runtimeDone event is not available `timeout` is used as the
198+ // result of the transaction.
199+ expected : fmt .Sprintf (
200+ "%s\n %s\n %s" ,
201+ metadata ,
202+ lambdaData ,
203+ generateCompleteTxn (t , txnData , "timeout" , "failure" , txnDur ),
204+ ),
205+ },
206+ {
207+ name : "with_meta_agent_init_without_root_txn" ,
208+ agentInit : agentInit {init : true , withMetadata : true },
164209 receiveAgentRootTxn : false ,
165210 receiveLambdaLogRuntime : false ,
166211 // With agent init proxy txn is created if root txn is not reported.
@@ -179,8 +224,12 @@ func TestLifecycle(t *testing.T) {
179224 // NEXT API response creates a new invocation cache
180225 b .RegisterInvocation (reqID , fnARN , ts .Add (txnDur ).UnixMilli (), ts )
181226 // Agent creates and registers a partial transaction in the extn
182- if tc .agentInit {
183- require .NoError (t , b .OnAgentInit (reqID , txnID , []byte (txnData )))
227+ if tc .agentInit .init {
228+ initData := txnData
229+ if tc .agentInit .withMetadata {
230+ initData = fmt .Sprintf ("%s\n %s" , metadata , txnData )
231+ }
232+ require .NoError (t , b .OnAgentInit (reqID , "" , []byte (initData )))
184233 }
185234 // Agent sends a request with metadata
186235 require .NoError (t , b .AddAgentData (APMData {
@@ -209,17 +258,18 @@ func TestLifecycle(t *testing.T) {
209258 }
210259}
211260
212- func TestIsTransactionEvent (t * testing.T ) {
261+ func TestFindEventType (t * testing.T ) {
213262 for _ , tc := range []struct {
214263 body []byte
215- expected bool
264+ expected eventType
216265 }{
217- {body : []byte (`{}` ), expected : false },
218- {body : []byte (`{"tran":{}}` ), expected : false },
219- {body : []byte (`{"span":{}}` ), expected : false },
220- {body : []byte (`{"transaction":{}}` ), expected : true },
266+ {body : []byte (`{}` ), expected : otherEvent },
267+ {body : []byte (`{"tran":{}}` ), expected : otherEvent },
268+ {body : []byte (`{"span":{}}` ), expected : otherEvent },
269+ {body : []byte (`{"metadata":{}}\n{"transaction":{}}` ), expected : metadataEvent },
270+ {body : []byte (`{"transaction":{}}` ), expected : transactionEvent },
221271 } {
222- assert .Equal (t , tc .expected , isTransactionEvent (tc .body ))
272+ assert .Equal (t , tc .expected , findEventType (tc .body ))
223273 }
224274}
225275
0 commit comments