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 authored and Furkan IKIZ committed Feb 25, 2025
1 parent 9e8e9fb commit 59443d1
Show file tree
Hide file tree
Showing 2 changed files with 33 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 @@ -239,6 +239,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
23 changes: 23 additions & 0 deletions v3/newrelic/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ 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)
}
})
}
}

func TestTransaction_MethodsWithNilTransaction(t *testing.T) {
var nilTxn *Transaction

Expand Down

0 comments on commit 59443d1

Please sign in to comment.