From 6fb7c79219f10f9d6703b3e52e5342a977fc06b9 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 18:20:48 +0300 Subject: [PATCH] [#302] pkg/netmap: Document default values set in `NewPlacementPolicy` Document field values of instance constructed via `NewPlacementPolicy`. Assert the values in corresponding unit test. Signed-off-by: Pavel Karpy --- pkg/netmap/policy.go | 14 ++++++++++++++ pkg/netmap/policy_test.go | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pkg/netmap/policy.go b/pkg/netmap/policy.go index 27035536..dc2e54ed 100644 --- a/pkg/netmap/policy.go +++ b/pkg/netmap/policy.go @@ -8,6 +8,12 @@ import ( type PlacementPolicy netmap.PlacementPolicy // NewPlacementPolicy creates and returns new PlacementPolicy instance. +// +// Defaults: +// - backupFactor: 0; +// - replicas nil; +// - selectors nil; +// - filters nil. func NewPlacementPolicy() *PlacementPolicy { return NewPlacementPolicyFromV2(new(netmap.PlacementPolicy)) } @@ -31,6 +37,10 @@ func (p *PlacementPolicy) Replicas() []*Replica { rs := (*netmap.PlacementPolicy)(p). GetReplicas() + if rs == nil { + return nil + } + res := make([]*Replica, 0, len(rs)) for i := range rs { @@ -73,6 +83,10 @@ func (p *PlacementPolicy) Selectors() []*Selector { rs := (*netmap.PlacementPolicy)(p). GetSelectors() + if rs == nil { + return nil + } + res := make([]*Selector, 0, len(rs)) for i := range rs { diff --git a/pkg/netmap/policy_test.go b/pkg/netmap/policy_test.go index 173317ab..43b45edd 100644 --- a/pkg/netmap/policy_test.go +++ b/pkg/netmap/policy_test.go @@ -151,6 +151,24 @@ func TestNewPlacementPolicy(t *testing.T) { require.Nil(t, x.ToV2()) }) + + t.Run("default values", func(t *testing.T) { + pp := NewPlacementPolicy() + + // check initial values + require.Nil(t, pp.Replicas()) + require.Nil(t, pp.Filters()) + require.Nil(t, pp.Selectors()) + require.Zero(t, pp.ContainerBackupFactor()) + + // convert to v2 message + ppV2 := pp.ToV2() + + require.Nil(t, ppV2.GetReplicas()) + require.Nil(t, ppV2.GetFilters()) + require.Nil(t, ppV2.GetSelectors()) + require.Zero(t, ppV2.GetContainerBackupFactor()) + }) } func TestNewPlacementPolicyFromV2(t *testing.T) {