Skip to content

Commit

Permalink
fix(depthmonitor): unit tests (#3473)
Browse files Browse the repository at this point in the history
  • Loading branch information
istae committed Oct 29, 2022
1 parent 946fbc5 commit dfe8d7f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
30 changes: 10 additions & 20 deletions pkg/topology/depthmonitor/depthmonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/ethersphere/bee/pkg/topology/depthmonitor"
)

const depthMonitorWakeUpInterval = 10 * time.Millisecond

func newTestSvc(
t depthmonitor.Topology,
s depthmonitor.SyncReporter,
Expand Down Expand Up @@ -84,7 +86,7 @@ func TestDepthMonitorService(t *testing.T) {
t.Parallel()

bs := mockbatchstore.New(mockbatchstore.WithReserveState(&postage.ReserveState{Radius: 3}))
svc := newTestSvc(nil, nil, nil, nil, bs, 100*time.Millisecond, depthmonitor.DefaultWakeupInterval)
svc := newTestSvc(nil, nil, nil, nil, bs, 0, depthmonitor.DefaultWakeupInterval)
waitForDepth(t, svc, 3)
err := svc.Close()
if err != nil {
Expand All @@ -95,25 +97,19 @@ func TestDepthMonitorService(t *testing.T) {
t.Run("depth decrease due to under utilization", func(t *testing.T) {
t.Parallel()

const depthMonitorWakeUpInterval = 200 * time.Millisecond

topo := &mockTopology{peers: 1}
// >50% utilized reserve
reserve := &mockReserveReporter{size: 26000, capacity: 50000}

bs := mockbatchstore.New(mockbatchstore.WithReserveState(&postage.ReserveState{Radius: 3}))

svc := newTestSvc(topo, nil, reserve, nil, bs, 100*time.Millisecond, depthMonitorWakeUpInterval)
svc.SetMinimumRadius(0)
svc := newTestSvc(topo, nil, reserve, nil, bs, 0, depthMonitorWakeUpInterval)

waitForDepth(t, svc, 3)
// simulate huge eviction to trigger manage worker
reserve.setSize(1000)

waitForDepth(t, svc, 1)
if topo.getStorageDepth() != 1 {
t.Fatal("topology depth not updated")
}
waitForDepth(t, svc, 0)
err := svc.Close()
if err != nil {
t.Fatal(err)
Expand All @@ -123,16 +119,14 @@ func TestDepthMonitorService(t *testing.T) {
t.Run("depth doesnt change due to non-zero pull rate", func(t *testing.T) {
t.Parallel()

const depthMonitorWakeUpInterval = 200 * time.Millisecond

// under utilized reserve
reserve := &mockReserveReporter{size: 10000, capacity: 50000}
bs := mockbatchstore.New(mockbatchstore.WithReserveState(&postage.ReserveState{Radius: 3}))
syncer := &mockSyncReporter{rate: 10}

svc := newTestSvc(nil, syncer, reserve, nil, bs, 100*time.Millisecond, depthMonitorWakeUpInterval)
svc := newTestSvc(nil, syncer, reserve, nil, bs, 0, depthMonitorWakeUpInterval)

time.Sleep(2 * time.Second)
time.Sleep(time.Second)
// ensure that after few cycles of the adaptation period, the depth hasn't changed
if svc.StorageDepth() != 3 {
t.Fatal("found drop in depth")
Expand All @@ -146,15 +140,13 @@ func TestDepthMonitorService(t *testing.T) {
t.Run("depth doesnt change for utilized reserve", func(t *testing.T) {
t.Parallel()

const depthMonitorWakeUpInterval = 200 * time.Millisecond

// >50% utilized reserve
reserve := &mockReserveReporter{size: 25001, capacity: 50000}
bs := mockbatchstore.New(mockbatchstore.WithReserveState(&postage.ReserveState{Radius: 3}))

svc := newTestSvc(nil, nil, reserve, nil, bs, 100*time.Millisecond, depthMonitorWakeUpInterval)
svc := newTestSvc(nil, nil, reserve, nil, bs, 0, depthMonitorWakeUpInterval)

time.Sleep(2 * time.Second)
time.Sleep(time.Second)
// ensure the depth hasnt changed
if svc.StorageDepth() != 3 {
t.Fatal("found drop in depth")
Expand All @@ -168,14 +160,12 @@ func TestDepthMonitorService(t *testing.T) {
t.Run("radius setter handler", func(t *testing.T) {
t.Parallel()

const depthMonitorWakeUpInterval = 200 * time.Millisecond

topo := &mockTopology{connDepth: 3}
bs := mockbatchstore.New(mockbatchstore.WithReserveState(&postage.ReserveState{Radius: 3}))
// >50% utilized reserve
reserve := &mockReserveReporter{size: 25001, capacity: 50000}

svc := newTestSvc(topo, nil, reserve, nil, bs, 100*time.Millisecond, depthMonitorWakeUpInterval)
svc := newTestSvc(topo, nil, reserve, nil, bs, 0, depthMonitorWakeUpInterval)

waitForDepth(t, svc, 3)

Expand Down
4 changes: 0 additions & 4 deletions pkg/topology/depthmonitor/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ func (s *Service) SetStorageRadius(r uint8) {
return r
})
}

func (s *Service) SetMinimumRadius(radius uint8) {
s.minimumRadius = radius
}

0 comments on commit dfe8d7f

Please sign in to comment.