ipfs-cluster/consensus/raft/config_test.go
Hector Sanjuan 848023e381 Fix #139: Update cluster to Raft 1.0.0
The main differences is that the new version of Raft is more strict
about starting raft peers which already contain configurations.

For a start, cluster will fail to start if the configured cluster
peers are different from the Raft peers. The user will have to
manually cleanup Raft (TODO: an ipfs-cluster-service command for it).

Additionally, this commit adds extra options to the consensus/raft
configuration section, adds tests and improves existing ones and
improves certain code sections.

License: MIT
Signed-off-by: Hector Sanjuan <hector@protocol.ai>
2017-11-01 12:17:33 +01:00

98 lines
1.8 KiB
Go

package raft
import (
"encoding/json"
"testing"
hraft "github.com/hashicorp/raft"
)
var cfgJSON = []byte(`
{
"heartbeat_timeout": "1s",
"commit_retries": 1,
"wait_for_leader_timeout": "15s",
"election_timeout": "1s",
"commit_timeout": "50ms",
"max_append_entries": 64,
"trailing_logs": 10240,
"snapshot_interval": "2m0s",
"snapshot_threshold": 8192,
"leader_lease_timeout": "500ms"
}
`)
func TestLoadJSON(t *testing.T) {
cfg := &Config{}
err := cfg.LoadJSON(cfgJSON)
if err != nil {
t.Fatal(err)
}
j := &jsonConfig{}
json.Unmarshal(cfgJSON, j)
j.HeartbeatTimeout = "1us"
tst, _ := json.Marshal(j)
err = cfg.LoadJSON(tst)
if err == nil {
t.Error("expected error decoding heartbeat_timeout")
}
json.Unmarshal(cfgJSON, j)
j.LeaderLeaseTimeout = "abc"
tst, _ = json.Marshal(j)
err = cfg.LoadJSON(tst)
if err != nil {
t.Fatal(err)
}
def := hraft.DefaultConfig()
if cfg.RaftConfig.LeaderLeaseTimeout != def.LeaderLeaseTimeout {
t.Error("expected default leader lease")
}
}
func TestToJSON(t *testing.T) {
cfg := &Config{}
cfg.LoadJSON(cfgJSON)
newjson, err := cfg.ToJSON()
if err != nil {
t.Fatal(err)
}
cfg = &Config{}
err = cfg.LoadJSON(newjson)
if err != nil {
t.Fatal(err)
}
}
func TestDefault(t *testing.T) {
cfg := &Config{}
cfg.Default()
if cfg.Validate() != nil {
t.Fatal("error validating")
}
cfg.RaftConfig.HeartbeatTimeout = 0
if cfg.Validate() == nil {
t.Fatal("expected error validating")
}
cfg.Default()
cfg.RaftConfig = nil
if cfg.Validate() == nil {
t.Fatal("expected error validating")
}
cfg.Default()
cfg.CommitRetries = -1
if cfg.Validate() == nil {
t.Fatal("expected error validating")
}
cfg.Default()
cfg.WaitForLeaderTimeout = 0
if cfg.Validate() == nil {
t.Fatal("expected error validating")
}
}