108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
package dht
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func newReplicationManagerForTest(t *testing.T) *ReplicationManager {
|
|
t.Helper()
|
|
|
|
cfg := &ReplicationConfig{
|
|
ReplicationFactor: 3,
|
|
ReprovideInterval: time.Hour,
|
|
CleanupInterval: time.Hour,
|
|
ProviderTTL: 30 * time.Minute,
|
|
MaxProvidersPerKey: 5,
|
|
EnableAutoReplication: false,
|
|
EnableReprovide: false,
|
|
MaxConcurrentReplications: 1,
|
|
}
|
|
|
|
rm := NewReplicationManager(context.Background(), nil, cfg)
|
|
t.Cleanup(func() {
|
|
if rm.reprovideTimer != nil {
|
|
rm.reprovideTimer.Stop()
|
|
}
|
|
if rm.cleanupTimer != nil {
|
|
rm.cleanupTimer.Stop()
|
|
}
|
|
rm.cancel()
|
|
})
|
|
return rm
|
|
}
|
|
|
|
func TestAddContentRegistersKey(t *testing.T) {
|
|
rm := newReplicationManagerForTest(t)
|
|
|
|
if err := rm.AddContent("ucxl://example/path", 512, 1); err != nil {
|
|
t.Fatalf("expected AddContent to succeed, got error: %v", err)
|
|
}
|
|
|
|
rm.keysMutex.RLock()
|
|
record, ok := rm.contentKeys["ucxl://example/path"]
|
|
rm.keysMutex.RUnlock()
|
|
|
|
if !ok {
|
|
t.Fatal("expected content key to be registered")
|
|
}
|
|
|
|
if record.Size != 512 {
|
|
t.Fatalf("expected size 512, got %d", record.Size)
|
|
}
|
|
}
|
|
|
|
func TestRemoveContentClearsTracking(t *testing.T) {
|
|
rm := newReplicationManagerForTest(t)
|
|
|
|
if err := rm.AddContent("ucxl://example/path", 512, 1); err != nil {
|
|
t.Fatalf("AddContent returned error: %v", err)
|
|
}
|
|
|
|
if err := rm.RemoveContent("ucxl://example/path"); err != nil {
|
|
t.Fatalf("RemoveContent returned error: %v", err)
|
|
}
|
|
|
|
rm.keysMutex.RLock()
|
|
_, exists := rm.contentKeys["ucxl://example/path"]
|
|
rm.keysMutex.RUnlock()
|
|
|
|
if exists {
|
|
t.Fatal("expected content key to be removed")
|
|
}
|
|
}
|
|
|
|
func TestGetReplicationStatusReturnsCopy(t *testing.T) {
|
|
rm := newReplicationManagerForTest(t)
|
|
|
|
if err := rm.AddContent("ucxl://example/path", 512, 1); err != nil {
|
|
t.Fatalf("AddContent returned error: %v", err)
|
|
}
|
|
|
|
status, err := rm.GetReplicationStatus("ucxl://example/path")
|
|
if err != nil {
|
|
t.Fatalf("GetReplicationStatus returned error: %v", err)
|
|
}
|
|
|
|
if status.Key != "ucxl://example/path" {
|
|
t.Fatalf("expected status key to match, got %s", status.Key)
|
|
}
|
|
|
|
// Mutating status should not affect internal state
|
|
status.HealthyProviders = 99
|
|
internal, _ := rm.GetReplicationStatus("ucxl://example/path")
|
|
if internal.HealthyProviders == 99 {
|
|
t.Fatal("expected GetReplicationStatus to return a copy")
|
|
}
|
|
}
|
|
|
|
func TestGetMetricsReturnsSnapshot(t *testing.T) {
|
|
rm := newReplicationManagerForTest(t)
|
|
|
|
metrics := rm.GetMetrics()
|
|
if metrics == rm.metrics {
|
|
t.Fatal("expected GetMetrics to return a copy of metrics")
|
|
}
|
|
}
|