use generic set in sig-node

This commit is contained in:
Keisuke Ishigami 2025-03-11 20:00:15 +09:00
parent e9a3d99f37
commit cdac61b902
2 changed files with 15 additions and 15 deletions

View File

@ -81,7 +81,7 @@ func (h *TimedQueue) Pop() interface{} {
type UniqueQueue struct {
lock sync.Mutex
queue TimedQueue
set sets.String
set sets.Set[string]
}
// Add a new value to the queue if it wasn't added before, or was
@ -189,7 +189,7 @@ func (q *UniqueQueue) Clear() {
q.queue = make(TimedQueue, 0)
}
if len(q.set) > 0 {
q.set = sets.NewString()
q.set = sets.New[string]()
}
}
@ -207,7 +207,7 @@ func NewRateLimitedTimedQueue(limiter flowcontrol.RateLimiter) *RateLimitedTimed
return &RateLimitedTimedQueue{
queue: UniqueQueue{
queue: TimedQueue{},
set: sets.NewString(),
set: sets.New[string](),
},
limiter: limiter,
}

View File

@ -35,7 +35,7 @@ func CheckQueueEq(lhs []string, rhs TimedQueue) bool {
return true
}
func CheckSetEq(lhs, rhs sets.String) bool {
func CheckSetEq(lhs, rhs sets.Set[string]) bool {
return lhs.IsSuperset(rhs) && rhs.IsSuperset(lhs)
}
@ -49,7 +49,7 @@ func TestUniqueQueueGet(t *testing.T) {
queue := UniqueQueue{
queue: TimedQueue{},
set: sets.NewString(),
set: sets.New[string](),
}
queue.Add(TimedValue{Value: "first", UID: "11111", AddedAt: now(), ProcessAt: now()})
queue.Add(TimedValue{Value: "second", UID: "22222", AddedAt: now(), ProcessAt: now()})
@ -63,7 +63,7 @@ func TestUniqueQueueGet(t *testing.T) {
t.Errorf("Invalid queue. Got %v, expected %v", queue.queue, queuePattern)
}
setPattern := sets.NewString("first", "second", "third")
setPattern := sets.New[string]("first", "second", "third")
if len(queue.set) != len(setPattern) {
t.Fatalf("Map %v should have length %d", queue.set, len(setPattern))
}
@ -80,7 +80,7 @@ func TestUniqueQueueGet(t *testing.T) {
t.Errorf("Invalid queue. Got %v, expected %v", queue.queue, queuePattern)
}
setPattern = sets.NewString("second", "third")
setPattern = sets.New[string]("second", "third")
if len(queue.set) != len(setPattern) {
t.Fatalf("Map %v should have length %d", queue.set, len(setPattern))
}
@ -103,7 +103,7 @@ func TestAddNode(t *testing.T) {
t.Errorf("Invalid queue. Got %v, expected %v", evictor.queue.queue, queuePattern)
}
setPattern := sets.NewString("first", "second", "third")
setPattern := sets.New[string]("first", "second", "third")
if len(evictor.queue.set) != len(setPattern) {
t.Fatalf("Map %v should have length %d", evictor.queue.set, len(setPattern))
}
@ -134,7 +134,7 @@ func TestDelNode(t *testing.T) {
t.Errorf("Invalid queue. Got %v, expected %v", evictor.queue.queue, queuePattern)
}
setPattern := sets.NewString("second", "third")
setPattern := sets.New[string]("second", "third")
if len(evictor.queue.set) != len(setPattern) {
t.Fatalf("Map %v should have length %d", evictor.queue.set, len(setPattern))
}
@ -156,7 +156,7 @@ func TestDelNode(t *testing.T) {
t.Errorf("Invalid queue. Got %v, expected %v", evictor.queue.queue, queuePattern)
}
setPattern = sets.NewString("first", "third")
setPattern = sets.New[string]("first", "third")
if len(evictor.queue.set) != len(setPattern) {
t.Fatalf("Map %v should have length %d", evictor.queue.set, len(setPattern))
}
@ -178,7 +178,7 @@ func TestDelNode(t *testing.T) {
t.Errorf("Invalid queue. Got %v, expected %v", evictor.queue.queue, queuePattern)
}
setPattern = sets.NewString("first", "second")
setPattern = sets.New[string]("first", "second")
if len(evictor.queue.set) != len(setPattern) {
t.Fatalf("Map %v should have length %d", evictor.queue.set, len(setPattern))
}
@ -194,14 +194,14 @@ func TestTry(t *testing.T) {
evictor.Add("third", "33333")
evictor.Remove("second")
deletedMap := sets.NewString()
deletedMap := sets.New[string]()
logger, _ := ktesting.NewTestContext(t)
evictor.Try(logger, func(value TimedValue) (bool, time.Duration) {
deletedMap.Insert(value.Value)
return true, 0
})
setPattern := sets.NewString("first", "third")
setPattern := sets.New[string]("first", "third")
if len(deletedMap) != len(setPattern) {
t.Fatalf("Map %v should have length %d", evictor.queue.set, len(setPattern))
}
@ -371,14 +371,14 @@ func TestAddAfterTry(t *testing.T) {
evictor.Add("third", "33333")
evictor.Remove("second")
deletedMap := sets.NewString()
deletedMap := sets.New[string]()
logger, _ := ktesting.NewTestContext(t)
evictor.Try(logger, func(value TimedValue) (bool, time.Duration) {
deletedMap.Insert(value.Value)
return true, 0
})
setPattern := sets.NewString("first", "third")
setPattern := sets.New[string]("first", "third")
if len(deletedMap) != len(setPattern) {
t.Fatalf("Map %v should have length %d", evictor.queue.set, len(setPattern))
}