Skip to content

Commit

Permalink
feat(transaction-is-ended): add IsEnded() method (#995)
Browse files Browse the repository at this point in the history
  • Loading branch information
frknikiz committed Feb 1, 2025
1 parent 5867ad9 commit 49b889a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions v3/newrelic/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ func (txn *Transaction) SetWebRequestHTTP(r *http.Request) {
txn.SetWebRequest(wr)
}

// IsEnded returns transaction end status.
// If the transaction is nil, the thread is nil, or the transaction is finished, it returns true.
// Otherwise, it returns thread.finished value.
func (txn *Transaction) IsEnded() bool {
if txn == nil || txn.thread == nil || txn.thread.txn == nil {
return true
}
return txn.thread.txn.finished
}

func transport(r *http.Request) TransportType {
if strings.HasPrefix(r.Proto, "HTTP") {
if r.TLS != nil {
Expand Down
26 changes: 26 additions & 0 deletions v3/newrelic/transaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package newrelic

import "testing"

func TestIsEnded(t *testing.T) {
tests := []struct {
name string
txn *Transaction
expected bool
}{
{"txn is nil", nil, true},
{"thread is nil", &Transaction{thread: nil}, true},
{"txn.thread.txn is nil", &Transaction{thread: &thread{}}, true},
{"txn.thread.txn.finished is true", &Transaction{thread: &thread{txn: &txn{finished: true}}}, true},
{"txn.thread.txn.finished is false", &Transaction{thread: &thread{txn: &txn{finished: false}}}, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.txn.IsEnded()
if result != tt.expected {
t.Errorf("IsEnded() = %v; want %v", result, tt.expected)
}
})
}
}

0 comments on commit 49b889a

Please sign in to comment.