Skip to content

Commit 741fb71

Browse files
provider: use synctest in time based tests #1136
1 parent ef61e17 commit 741fb71

File tree

8 files changed

+1067
-1362
lines changed

8 files changed

+1067
-1362
lines changed

.github/workflows/go-test.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,5 @@ concurrency:
1616
jobs:
1717
go-test:
1818
uses: ipdxco/unified-github-workflows/.github/workflows/[email protected]
19-
with:
20-
go-versions: '["1.23.x", "1.24.x"]'
2119
secrets:
2220
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
module github.com/libp2p/go-libp2p-kad-dht
22

3-
go 1.23.8
3+
go 1.24
44

55
require (
6-
github.com/filecoin-project/go-clock v0.1.0
76
github.com/gammazero/deque v1.0.0
87
github.com/google/gopacket v1.1.19
98
github.com/google/uuid v1.6.0
@@ -48,6 +47,7 @@ require (
4847
github.com/davecgh/go-spew v1.1.1 // indirect
4948
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
5049
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
50+
github.com/filecoin-project/go-clock v0.1.0 // indirect
5151
github.com/flynn/noise v1.1.0 // indirect
5252
github.com/francoispqt/gojay v1.2.13 // indirect
5353
github.com/go-logr/logr v1.4.2 // indirect

provider/internal/connectivity/connectivity.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"sync"
55
"sync/atomic"
66
"time"
7-
8-
"github.com/filecoin-project/go-clock"
97
)
108

119
const (
@@ -43,7 +41,6 @@ type ConnectivityChecker struct {
4341

4442
online atomic.Bool
4543

46-
clock clock.Clock
4744
lastCheck time.Time
4845
onlineCheckInterval time.Duration // minimum check interval when online
4946

@@ -64,7 +61,6 @@ func New(checkFunc func() bool, opts ...Option) (*ConnectivityChecker, error) {
6461
c := &ConnectivityChecker{
6562
done: make(chan struct{}),
6663
checkFunc: checkFunc,
67-
clock: cfg.clock,
6864
onlineCheckInterval: cfg.onlineCheckInterval,
6965
onOffline: cfg.onOffline,
7066
onOnline: cfg.onOnline,
@@ -139,7 +135,7 @@ func (c *ConnectivityChecker) TriggerCheck() {
139135
c.mutex.Unlock()
140136
return
141137
}
142-
if c.online.Load() && c.clock.Now().Sub(c.lastCheck) < c.onlineCheckInterval {
138+
if c.online.Load() && time.Since(c.lastCheck) < c.onlineCheckInterval {
143139
c.mutex.Unlock()
144140
return // last check was too recent
145141
}
@@ -148,7 +144,7 @@ func (c *ConnectivityChecker) TriggerCheck() {
148144
defer c.mutex.Unlock()
149145

150146
if c.checkFunc() {
151-
c.lastCheck = c.clock.Now()
147+
c.lastCheck = time.Now()
152148
return
153149
}
154150

@@ -165,13 +161,20 @@ func (c *ConnectivityChecker) TriggerCheck() {
165161
func (c *ConnectivityChecker) probeLoop(init bool) {
166162
var offlineC <-chan time.Time
167163
if !init {
168-
offlineTimer := c.clock.Timer(c.offlineDelay)
169-
defer offlineTimer.Stop()
170-
offlineC = offlineTimer.C
164+
if c.offlineDelay == 0 {
165+
if c.onOffline != nil {
166+
// Online -> Offline
167+
c.onOffline()
168+
}
169+
} else {
170+
offlineTimer := time.NewTimer(c.offlineDelay)
171+
defer offlineTimer.Stop()
172+
offlineC = offlineTimer.C
173+
}
171174
}
172175

173176
delay := initialBackoffDelay
174-
timer := c.clock.Timer(delay)
177+
timer := time.NewTimer(delay)
175178
defer timer.Stop()
176179
for {
177180
select {
@@ -202,7 +205,7 @@ func (c *ConnectivityChecker) probe() bool {
202205
// Node is back Online.
203206
c.online.Store(true)
204207

205-
c.lastCheck = c.clock.Now()
208+
c.lastCheck = time.Now()
206209
if c.onOnline != nil {
207210
c.onOnline()
208211
}

0 commit comments

Comments
 (0)