Skip to content

Commit

Permalink
atomic and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
boks1971 committed Sep 16, 2024
1 parent 6977e58 commit 2879810
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
21 changes: 21 additions & 0 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,10 @@ func TestCandidatePairStats(t *testing.T) {
p := a.findPair(hostLocal, prflxRemote)
p.state = CandidatePairStateFailed

for i := 0; i < 10; i++ {
p.UpdateRoundTripTime(time.Duration(i+1) * time.Second)
}

stats := a.GetCandidatePairsStats()
if len(stats) != 4 {
t.Fatal("expected 4 candidate pairs stats")
Expand Down Expand Up @@ -766,6 +770,23 @@ func TestCandidatePairStats(t *testing.T) {
t.Fatalf("expected host-prflx pair to have state failed, it has state %s instead",
prflxPairStat.State.String())
}

expectedCurrentRoundTripTime := time.Duration(10) * time.Second
if prflxPairStat.CurrentRoundTripTime != expectedCurrentRoundTripTime.Seconds() {
t.Fatalf("expected current round trip time to be %f, it is %f instead",
expectedCurrentRoundTripTime.Seconds(), prflxPairStat.CurrentRoundTripTime)
}

expectedTotalRoundTripTime := time.Duration(55) * time.Second
if prflxPairStat.TotalRoundTripTime != expectedTotalRoundTripTime.Seconds() {
t.Fatalf("expected total round trip time to be %f, it is %f instead",
expectedTotalRoundTripTime.Seconds(), prflxPairStat.TotalRoundTripTime)
}

if prflxPairStat.ResponsesReceived != 10 {
t.Fatalf("expected responses received to be 10, it is %d instead",
prflxPairStat.ResponsesReceived)
}
}

func TestLocalCandidateStats(t *testing.T) {
Expand Down
42 changes: 23 additions & 19 deletions candidatepair.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package ice

import (
"fmt"
"sync"
"sync/atomic"
"time"

"github.com/pion/stun/v3"
Expand All @@ -32,9 +32,8 @@ type CandidatePair struct {
nominateOnBindingSuccess bool

// stats
statsMu sync.RWMutex
currentRoundTripTime time.Duration
totalRoundTripTime time.Duration
currentRoundTripTime atomic.Pointer[time.Duration]
totalRoundTripTime atomic.Pointer[time.Duration]
responsesReceived uint64
}

Expand Down Expand Up @@ -112,37 +111,42 @@ func (a *Agent) sendSTUN(msg *stun.Message, local, remote Candidate) {
// UpdateRoundTripTime sets the current round time of this pair and
// accumulates total round trip time and responses received
func (p *CandidatePair) UpdateRoundTripTime(rtt time.Duration) {
p.statsMu.Lock()
defer p.statsMu.Unlock()
p.currentRoundTripTime.Store(&rtt)

Check warning on line 114 in candidatepair.go

View check run for this annotation

Codecov / codecov/patch

candidatepair.go#L113-L114

Added lines #L113 - L114 were not covered by tests

p.currentRoundTripTime = rtt
p.totalRoundTripTime += rtt
p.responsesReceived++
prevTotalRoundTripTime := p.totalRoundTripTime.Load()
totalRoundTripTime := rtt
if prevTotalRoundTripTime != nil {
totalRoundTripTime += *prevTotalRoundTripTime

Check warning on line 119 in candidatepair.go

View check run for this annotation

Codecov / codecov/patch

candidatepair.go#L116-L119

Added lines #L116 - L119 were not covered by tests
}
p.totalRoundTripTime.CompareAndSwap(prevTotalRoundTripTime, &totalRoundTripTime)

Check warning on line 121 in candidatepair.go

View check run for this annotation

Codecov / codecov/patch

candidatepair.go#L121

Added line #L121 was not covered by tests

atomic.AddUint64(&p.responsesReceived, 1)

Check warning on line 123 in candidatepair.go

View check run for this annotation

Codecov / codecov/patch

candidatepair.go#L123

Added line #L123 was not covered by tests
}

// CurrentRoundTripTime returns the current round trip time in seconds
// https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime
func (p *CandidatePair) CurrentRoundTripTime() float64 {
p.statsMu.RLock()
defer p.statsMu.RUnlock()
crtt := p.currentRoundTripTime.Load()
if crtt != nil {
return crtt.Seconds()

Check warning on line 131 in candidatepair.go

View check run for this annotation

Codecov / codecov/patch

candidatepair.go#L128-L131

Added lines #L128 - L131 were not covered by tests
}

return p.currentRoundTripTime.Seconds()
return 0

Check warning on line 134 in candidatepair.go

View check run for this annotation

Codecov / codecov/patch

candidatepair.go#L134

Added line #L134 was not covered by tests
}

// TotalRoundTripTime returns the current round trip time in seconds
// https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-totalroundtriptime
func (p *CandidatePair) TotalRoundTripTime() float64 {
p.statsMu.RLock()
defer p.statsMu.RUnlock()
trtt := p.totalRoundTripTime.Load()
if trtt != nil {
return trtt.Seconds()

Check warning on line 142 in candidatepair.go

View check run for this annotation

Codecov / codecov/patch

candidatepair.go#L139-L142

Added lines #L139 - L142 were not covered by tests
}

return p.totalRoundTripTime.Seconds()
return 0

Check warning on line 145 in candidatepair.go

View check run for this annotation

Codecov / codecov/patch

candidatepair.go#L145

Added line #L145 was not covered by tests
}

// ResponsesReceived returns the total number of connectivity responses received
// https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-responsesreceived
func (p *CandidatePair) ResponsesReceived() uint64 {
p.statsMu.RLock()
defer p.statsMu.RUnlock()

return p.responsesReceived
return atomic.LoadUint64(&p.responsesReceived)

Check warning on line 151 in candidatepair.go

View check run for this annotation

Codecov / codecov/patch

candidatepair.go#L150-L151

Added lines #L150 - L151 were not covered by tests
}

0 comments on commit 2879810

Please sign in to comment.