Skip to content

Commit

Permalink
feat(transaction-is-ended): add IsEnded() method to thread
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 59443d1 commit 73b5bba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
8 changes: 8 additions & 0 deletions v3/newrelic/internal_txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ type thread struct {
thread *tracingThread
}

func (thd *thread) IsEnded() bool {
txn := thd.txn
txn.Lock()
defer txn.Unlock()

return txn.finished
}

func (txn *txn) markStart(now time.Time) {
txn.Start = now
// The mainThread is considered active now.
Expand Down
29 changes: 29 additions & 0 deletions v3/newrelic/internal_txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,3 +1018,32 @@ func TestPanicNilRecovery(t *testing.T) {
},
})
}

func TestIsEndedInternal(t *testing.T) {
tests := []struct {
name string
txn *txn
expected bool
}{
{
name: "finished transaction",
txn: &txn{finished: true},
expected: true,
},
{
name: "unfinished transaction",
txn: &txn{finished: false},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
thread := &thread{txn: tt.txn}
result := thread.IsEnded()
if result != tt.expected {
t.Errorf("IsEnded() = %v; want %v", result, tt.expected)
}
})
}
}
4 changes: 2 additions & 2 deletions v3/newrelic/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ func (txn *Transaction) SetWebRequestHTTP(r *http.Request) {
// 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 {
if nilTransaction(txn) {
return true
}
return txn.thread.txn.finished
return txn.thread.IsEnded()
}

func transport(r *http.Request) TransportType {
Expand Down

0 comments on commit 73b5bba

Please sign in to comment.