Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transaction-is-ended): add IsEnded() method (#995) #998

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}
}
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 nilTransaction(txn) {
return true
}
return txn.thread.IsEnded()
}

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
Loading