Skip to content
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
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module github.com/ipfs/go-peertaskqueue

go 1.24.0
go 1.25

require (
github.com/filecoin-project/go-clock v0.1.0
github.com/ipfs/go-ipfs-pq v0.0.3
github.com/libp2p/go-libp2p v0.43.0
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ github.com/decred/dcrd/crypto/blake256 v1.1.0 h1:zPMNGQCm0g4QTY27fOCorQW7EryeQ/U
github.com/decred/dcrd/crypto/blake256 v1.1.0/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 h1:NMZiJj8QnKe1LgsbDayM4UoHwbvwDRwnI3hwNaAHRnc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0/go.mod h1:ZXNYxsqcloTdSy/rNShjYzMhyjf0LaoftYK0p+A3h40=
github.com/filecoin-project/go-clock v0.1.0 h1:SFbYIM75M8NnFm1yMHhN9Ahy3W5bEZV9gd6MPfXbKVU=
github.com/filecoin-project/go-clock v0.1.0/go.mod h1:4uB/O4PvOjlx1VCMdZ9MyDZXRm//gkj1ELEbxfI1AZs=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/ipfs/go-cid v0.5.0 h1:goEKKhaGm0ul11IHA7I6p1GmKz8kEYniqFopaB5Otwg=
Expand Down
6 changes: 2 additions & 4 deletions peertracker/peertracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import (
"math"
"math/bits"
"sync"
"time"

"github.com/filecoin-project/go-clock"
pq "github.com/ipfs/go-ipfs-pq"
"github.com/ipfs/go-peertaskqueue/peertask"
"github.com/libp2p/go-libp2p/core/peer"
)

var clockInstance = clock.New()

// TaskMerger is an interface that is used to merge new tasks into the active
// and pending queues
type TaskMerger interface {
Expand Down Expand Up @@ -214,7 +212,7 @@ func (p *PeerTracker) PushTasks(tasks ...peertask.Task) {
// When truncation happen we will keep older tasks in the queue to avoid some infinite
// tasks rotations if we are continously receiving work faster than we process it.
func (p *PeerTracker) PushTasksTruncated(n uint, tasks ...peertask.Task) {
now := clockInstance.Now()
now := time.Now()

p.activelk.Lock()
defer p.activelk.Unlock()
Expand Down
104 changes: 50 additions & 54 deletions peertracker/peertracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"reflect"
"sort"
"testing"
"testing/synctest"
"time"

"github.com/filecoin-project/go-clock"
"github.com/ipfs/go-peertaskqueue/peertask"
"github.com/ipfs/go-peertaskqueue/testutil"
)
Expand Down Expand Up @@ -670,58 +670,54 @@ func TestRemoveActive(t *testing.T) {
}

func TestPushPopEqualTaskPriorities(t *testing.T) {
partner := testutil.GeneratePeers(1)[0]
clock := clock.NewMock()
oldClock := clockInstance
clockInstance = clock
t.Cleanup(func() {
clockInstance = oldClock
synctest.Test(t, func(t *testing.T) {
partner := testutil.GeneratePeers(1)[0]
tracker := New(partner, &DefaultTaskMerger{}, 1)

tasks := []peertask.Task{
{
Topic: "1",
Priority: 10,
Work: 1,
},
{
Topic: "2",
Priority: 10,
Work: 1,
},
{
Topic: "3",
Priority: 10,
Work: 1,
},
}
tracker.PushTasks(tasks[0])
time.Sleep(10 * time.Millisecond)
tracker.PushTasks(tasks[1])
time.Sleep(10 * time.Millisecond)
tracker.PushTasks(tasks[2])
popped, _ := tracker.PopTasks(1)
if len(popped) != 1 {
t.Fatal("Expected 1 task")
}
if popped[0].Topic != "1" {
t.Fatal("Expected first task")
}
tracker.TaskDone(popped[0])
popped, _ = tracker.PopTasks(1)
if len(popped) != 1 {
t.Fatal("Expected 1 task")
}
if popped[0].Topic != "2" {
t.Fatal("Expected second task")
}
tracker.TaskDone(popped[0])
popped, _ = tracker.PopTasks(1)
if len(popped) != 1 {
t.Fatal("Expected 1 task")
}
if popped[0].Topic != "3" {
t.Fatal("Expected third task")
}
})
tracker := New(partner, &DefaultTaskMerger{}, 1)

tasks := []peertask.Task{
{
Topic: "1",
Priority: 10,
Work: 1,
},
{
Topic: "2",
Priority: 10,
Work: 1,
},
{
Topic: "3",
Priority: 10,
Work: 1,
},
}
tracker.PushTasks(tasks[0])
clock.Add(10 * time.Millisecond)
tracker.PushTasks(tasks[1])
clock.Add(10 * time.Millisecond)
tracker.PushTasks(tasks[2])
popped, _ := tracker.PopTasks(1)
if len(popped) != 1 {
t.Fatal("Expected 1 task")
}
if popped[0].Topic != "1" {
t.Fatal("Expected first task")
}
tracker.TaskDone(popped[0])
popped, _ = tracker.PopTasks(1)
if len(popped) != 1 {
t.Fatal("Expected 1 task")
}
if popped[0].Topic != "2" {
t.Fatal("Expected second task")
}
tracker.TaskDone(popped[0])
popped, _ = tracker.PopTasks(1)
if len(popped) != 1 {
t.Fatal("Expected 1 task")
}
if popped[0].Topic != "3" {
t.Fatal("Expected third task")
}
}
Loading