ipfs-cluster/informer/pinqueue/pinqueue_test.go
Hector Sanjuan 4daece2b98 Feat: add a new "pinqueue" informer component
This new component broadcasts metrics about the current size of the pinqueue,
which can in turn be used to inform allocations.

It has a weight_bucket_size option that serves to divide the actual size by a
given factor. This allows considering peers with similar queue sizes to have
the same weight.

Additionally, some changes have been made to the balanced allocator so that a
combination of tags, pinqueue sizes and free-spaces can be used. When
allocating by [<tag>, pinqueue, freespace], the allocator will prioritize
choosing peers with the smallest pin queue weight first, and of those with the
same weight, it will allocate based on freespace.
2022-06-16 17:43:29 +02:00

80 lines
1.5 KiB
Go

package pinqueue
import (
"context"
"testing"
rpc "github.com/libp2p/go-libp2p-gorpc"
)
type mockService struct{}
func (mock *mockService) PinQueueSize(ctx context.Context, in struct{}, out *int64) error {
*out = 42
return nil
}
func mockRPCClient(t *testing.T) *rpc.Client {
s := rpc.NewServer(nil, "mock")
c := rpc.NewClientWithServer(nil, "mock", s)
err := s.RegisterName("PinTracker", &mockService{})
if err != nil {
t.Fatal(err)
}
return c
}
func Test(t *testing.T) {
ctx := context.Background()
cfg := &Config{}
cfg.Default()
cfg.WeightBucketSize = 0
inf, err := New(cfg)
if err != nil {
t.Fatal(err)
}
metrics := inf.GetMetrics(ctx)
if len(metrics) != 1 {
t.Fatal("expected 1 metric")
}
m := metrics[0]
if m.Valid {
t.Error("metric should be invalid")
}
inf.SetClient(mockRPCClient(t))
metrics = inf.GetMetrics(ctx)
if len(metrics) != 1 {
t.Fatal("expected 1 metric")
}
m = metrics[0]
if !m.Valid {
t.Error("metric should be valid")
}
if m.Value != "42" {
t.Error("bad metric value", m.Value)
}
if m.Partitionable {
t.Error("should not be a partitionable metric")
}
if m.Weight != -42 {
t.Error("weight should be -42")
}
cfg.WeightBucketSize = 5
inf, err = New(cfg)
if err != nil {
t.Fatal(err)
}
inf.SetClient(mockRPCClient(t))
metrics = inf.GetMetrics(ctx)
if len(metrics) != 1 {
t.Fatal("expected 1 metric")
}
m = metrics[0]
if m.Weight != -8 {
t.Error("weight should be -8, not", m.Weight)
}
}