Browse Source

add collection settings test case

pull/279/head
tnextday 10 years ago
parent
commit
3bcbb9a1e8
  1. 15
      go/storage/collection_settings.go
  2. 32
      go/storage/collection_settings_test.go
  3. 2
      go/storage/volume_super_block_test.go

15
go/storage/collection_settings.go

@ -43,11 +43,11 @@ func (cs *CollectionSettings) ToPbMessage() []*weedpb.CollectionSetting {
setting := &weedpb.CollectionSetting{
Collection: collection,
}
if v, ok := m[keyReplicatePlacement]; ok {
if v, ok := m[keyReplicatePlacement]; ok && v != nil {
setting.ReplicaPlacement = v.(*ReplicaPlacement).String()
}
if v, ok := m[keyGarbageThreshold]; ok {
setting.ReplicaPlacement = v.(string)
if v, ok := m[keyGarbageThreshold]; ok && v != nil {
setting.VacuumGarbageThreshold = v.(string)
}
msg = append(msg, setting)
}
@ -87,7 +87,12 @@ func (cs *CollectionSettings) GetGarbageThreshold(collection string) string {
}
func (cs *CollectionSettings) SetGarbageThreshold(collection string, gt string) {
if gt == "" {
cs.set(collection, keyGarbageThreshold, nil)
} else {
cs.set(collection, keyGarbageThreshold, gt)
}
}
func (cs *CollectionSettings) GetReplicaPlacement(collection string) *ReplicaPlacement {
@ -100,6 +105,10 @@ func (cs *CollectionSettings) GetReplicaPlacement(collection string) *ReplicaPla
}
func (cs *CollectionSettings) SetReplicaPlacement(collection, t string) error {
if t == "" {
cs.set(collection, keyReplicatePlacement, nil)
return nil
}
rp, e := NewReplicaPlacementFromString(t)
if e == nil {
cs.set(collection, keyReplicatePlacement, rp)

32
go/storage/collection_settings_test.go

@ -0,0 +1,32 @@
package storage
import (
"testing"
"reflect"
"encoding/json"
)
func TestCollectionSettings(t *testing.T) {
cs1 := NewCollectionSettings("000", "0.3")
cs1.SetReplicaPlacement("col1", "001")
cs1.SetGarbageThreshold("col2", "0.5")
if cs1.GetGarbageThreshold("col1") != "0.3" ||
cs1.GetGarbageThreshold("col2") != "0.5" ||
cs1.GetGarbageThreshold("") != "0.3" ||
cs1.GetReplicaPlacement("").String() != "000" ||
cs1.GetReplicaPlacement("col1").String() != "001" ||
cs1.GetReplicaPlacement("col2").String() != "000" {
t.Fatal("Value incorrect.")
}
pb := cs1.ToPbMessage()
if buf, e := json.MarshalIndent(pb, "", "\t");e == nil {
t.Log(string(buf))
}else{
t.Fatal(e)
}
cs2 := NewCollectionSettingsFromPbMessage(pb)
if !reflect.DeepEqual(cs1, cs2) {
t.Fatal("PbMessage convert incorrect.")
}
}

2
go/storage/volume_super_block_test.go

@ -5,11 +5,9 @@ import (
)
func TestSuperBlockReadWrite(t *testing.T) {
rp, _ := NewReplicaPlacementFromByte(byte(001))
ttl, _ := ReadTTL("15d")
s := &SuperBlock{
version: CurrentVersion,
ReplicaPlacement: rp,
Ttl: ttl,
}

Loading…
Cancel
Save