func allocBytes(f func()) uint64 {
	var stats runtime.MemStats
	runtime.ReadMemStats(&stats)
	t := stats.TotalAlloc
	f()
	runtime.ReadMemStats(&stats)
	return stats.TotalAlloc - t
}
allocBytes returns the number of bytes allocated by invoking f.
package consistent
import (
	"math/rand"
	"runtime"
	"sort"
	"strconv"
	"sync"
	"testing"
	"testing/quick"
	"time"
	"github.com/SQLess/SQLess/crypto/asymmetric"
	"github.com/SQLess/SQLess/crypto/kms"
	"github.com/SQLess/SQLess/pow/cpuminer"
	. "github.com/SQLess/SQLess/proto"
	"github.com/SQLess/SQLess/utils"
	"github.com/SQLess/SQLess/utils/log"
)
const testStorePath = "./test.keystore"
var o sync.Once
func init() {
	o.Do(func() {
		kms.Unittest = true
		utils.RemoveAll(testStorePath + "*")
		x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
		if x == nil {
			log.Fatal("InitConsistent failed")
		}
	})
}
func CheckNum(num, expected int, t *testing.T) {
	if num != expected {
		t.Errorf("got %d, expected %d", num, expected)
	}
}
func NewNodeFromString(id string) Node {
	_, publicKey, _ := asymmetric.GenSecp256k1KeyPair()
	return Node{
		ID:        NodeID(id),
		Addr:      "",
		PublicKey: publicKey,
		Nonce:     cpuminer.Uint256{},
	}
}
func TestNew(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	if x == nil {
		t.Error("expected obj")
	}
}
func TestAdd(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	CheckNum(len(x.circle), x.NumberOfReplicas, t)
	CheckNum(len(x.sortedHashes), x.NumberOfReplicas, t)
	if sort.IsSorted(x.sortedHashes) == false {
		t.Error("expected sorted hashes to be sorted")
	}
	x.Add(NewNodeFromString(("3333333333333333333333333333333333333333333333333333333333333333")))
	CheckNum(len(x.circle), 2*x.NumberOfReplicas, t)
	CheckNum(len(x.sortedHashes), 2*x.NumberOfReplicas, t)
	if sort.IsSorted(x.sortedHashes) == false {
		t.Error("expected sorted hashes to be sorted")
	}
}
func TestRemove(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Remove("0000000000000000000000000000000000000000000000000000000000000000")
	CheckNum(len(x.circle), 0, t)
	CheckNum(len(x.sortedHashes), 0, t)
}
func TestRemoveNonExisting(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Remove("0000000000000000000000000000000000000000000000000000000000000000hijk")
	CheckNum(len(x.circle), x.NumberOfReplicas, t)
}
func TestGetEmpty(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	_, err := x.GetNeighbor("asdfsadfsadf")
	if err == nil {
		t.Error("expected error")
	}
	if err != ErrEmptyCircle {
		t.Error("expected empty circle error")
	}
}
func TestGetSingle(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	f := func(s string) bool {
		y, err := x.GetNeighbor(s)
		if err != nil {
			t.Logf("error: %v", err)
			return false
		}
		return y.ID == "0000000000000000000000000000000000000000000000000000000000000000"
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}
func TestConsistent_GetNode(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	nodeID := "40f26f9c816577adcb271734fec72c7640f26f9c816577adcb271734fec72c76"
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString(nodeID))
	f := func(s string) bool {
		_, err := x.GetNode(s)
		return err == ErrKeyNotFound
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
	node, err := x.GetNode(nodeID)
	if err != nil {
		t.Fatal(err)
	}
	if string(node.ID) != nodeID {
		t.Fatalf("got node: %v", node)
	}
}
type gtest struct {
	in  string
	out string
}
var gmtests = []gtest{
	{"0000", "2222222222222222222222222222222222222222222222222222222222222222"},
	{"2222", "1111111111111111111111111111111111111111111111111111111111111111"},
	{"1111", "1111111111111111111111111111111111111111111111111111111111111111"},
}
func TestGetMultiple(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	x.Add(NewNodeFromString("2222222222222222222222222222222222222222222222222222222222222222"))
	for i, v := range gmtests {
		result, err := x.GetNeighbor(v.in)
		if err != nil {
			t.Fatal(err)
		}
		if string(result.ID) != v.out {
			t.Errorf("%d. got %v, expected %v", i, result, v.out)
		}
	}
}
func TestGetMultipleQuick(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	x.Add(NewNodeFromString("2222222222222222222222222222222222222222222222222222222222222222"))
	f := func(s string) bool {
		y, err := x.GetNeighbor(s)
		if err != nil {
			t.Logf("error: %v", err)
			return false
		}
		return y.ID == "0000000000000000000000000000000000000000000000000000000000000000" || y.ID == "1111111111111111111111111111111111111111111111111111111111111111" || y.ID == "2222222222222222222222222222222222222222222222222222222222222222"
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}
var rtestsBefore = []gtest{
	{"0000", "2222222222222222222222222222222222222222222222222222222222222222"},
	{"2222", "1111111111111111111111111111111111111111111111111111111111111111"},
	{"1111", "1111111111111111111111111111111111111111111111111111111111111111"},
}
var rtestsAfter = []gtest{
	{"0000", "2222222222222222222222222222222222222222222222222222222222222222"},
	{"2222", "0000000000000000000000000000000000000000000000000000000000000000"},
	{"1111", "2222222222222222222222222222222222222222222222222222222222222222"},
}
func TestGetMultipleRemove(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	x.Add(NewNodeFromString("2222222222222222222222222222222222222222222222222222222222222222"))
	for i, v := range rtestsBefore {
		result, err := x.GetNeighbor(v.in)
		if err != nil {
			t.Fatal(err)
		}
		if string(result.ID) != v.out {
			t.Errorf("%d. got %v, expected %v before rm", i, result, v.out)
		}
	}
	x.Remove("1111111111111111111111111111111111111111111111111111111111111111")
	for i, v := range rtestsAfter {
		result, err := x.GetNeighbor(v.in)
		if err != nil {
			t.Fatal(err)
		}
		if string(result.ID) != v.out {
			t.Errorf("%d. got %v, expected %v after rm", i, result, v.out)
		}
	}
}
func TestGetMultipleRemoveQuick(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	x.Add(NewNodeFromString("2222222222222222222222222222222222222222222222222222222222222222"))
	x.Remove(NodeID("2222222222222222222222222222222222222222222222222222222222222222"))
	f := func(s string) bool {
		y, err := x.GetNeighbor(s)
		if err != nil {
			t.Logf("error: %v", err)
			return false
		}
		return y.ID == "0000000000000000000000000000000000000000000000000000000000000000" || y.ID == "1111111111111111111111111111111111111111111111111111111111111111"
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}
func TestGetTwo(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	x.Add(NewNodeFromString("2222222222222222222222222222222222222222222222222222222222222222"))
	a, b, err := x.GetTwoNeighbors("9999999999999999999999999999999999999999999999999999999999999999")
	if err != nil {
		t.Fatal(err)
	}
	if a.ID == b.ID {
		t.Error("a shouldn't equal b")
	}
	if a.ID != "0000000000000000000000000000000000000000000000000000000000000000" {
		t.Errorf("wrong a: %v", a)
	}
	if b.ID != "1111111111111111111111111111111111111111111111111111111111111111" {
		t.Errorf("wrong b: %v", b)
	}
}
func TestGetTwoEmpty(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	_, _, err := x.GetTwoNeighbors("9999999999999999999999999999999999999999999999999999999999999999")
	if err != ErrEmptyCircle {
		t.Fatal(err)
	}
}
func TestGetTwoQuick(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	x.Add(NewNodeFromString("2222222222222222222222222222222222222222222222222222222222222222"))
	f := func(s string) bool {
		a, b, err := x.GetTwoNeighbors(s)
		if err != nil {
			t.Logf("error: %v", err)
			return false
		}
		if a.ID == b.ID {
			t.Log("a.ID == b.ID")
			return false
		}
		if a.ID != "0000000000000000000000000000000000000000000000000000000000000000" && a.ID != "1111111111111111111111111111111111111111111111111111111111111111" && a.ID != "2222222222222222222222222222222222222222222222222222222222222222" {
			t.Logf("invalid a: %v", a)
			return false
		}
		if b.ID != "0000000000000000000000000000000000000000000000000000000000000000" && b.ID != "1111111111111111111111111111111111111111111111111111111111111111" && b.ID != "2222222222222222222222222222222222222222222222222222222222222222" {
			t.Logf("invalid b: %v", b)
			return false
		}
		return true
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}
func TestGetTwoOnlyTwoQuick(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	f := func(s string) bool {
		a, b, err := x.GetTwoNeighbors(s)
		if err != nil {
			t.Logf("error: %v", err)
			return false
		}
		if a.ID == b.ID {
			t.Log("a.ID == b.ID")
			return false
		}
		if a.ID != "0000000000000000000000000000000000000000000000000000000000000000" && a.ID != "1111111111111111111111111111111111111111111111111111111111111111" {
			t.Logf("invalid a: %v", a)
			return false
		}
		if b.ID != "0000000000000000000000000000000000000000000000000000000000000000" && b.ID != "1111111111111111111111111111111111111111111111111111111111111111" {
			t.Logf("invalid b: %v", b)
			return false
		}
		return true
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}
func TestGetTwoOnlyOneInCircle(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	a, b, err := x.GetTwoNeighbors("9999999999999999999999999999999999999999999999999999999999999999")
	if err != nil {
		t.Fatal(err)
	}
	if a.ID == b.ID {
		t.Error("a shouldn't equal b")
	}
	if a.ID != "0000000000000000000000000000000000000000000000000000000000000000" {
		t.Errorf("wrong a: %v", a)
	}
	if b.ID != "" {
		t.Errorf("wrong b: %v", b)
	}
}
func TestGetN(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	x.Add(NewNodeFromString("2222222222222222222222222222222222222222222222222222222222222222"))
	members, err := x.GetNeighbors("9999999", 3)
	if err != nil {
		t.Fatal(err)
	}
	if len(members) != 3 {
		t.Errorf("expected 3 members instead of %d", len(members))
	}
	if members[0].ID != "0000000000000000000000000000000000000000000000000000000000000000" {
		t.Errorf("wrong members[0]: %v", members[0])
	}
	if members[1].ID != "2222222222222222222222222222222222222222222222222222222222222222" {
		t.Errorf("wrong members[1]: %v", members[1])
	}
	if members[2].ID != "1111111111111111111111111111111111111111111111111111111111111111" {
		t.Errorf("wrong members[2]: %v", members[2])
	}
}
func TestGetNFilterRole(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	n := NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000")
	n.Role = Leader
	x.Add(n)
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	x.Add(NewNodeFromString("2222222222222222222222222222222222222222222222222222222222222222"))
	members, err := x.GetNeighborsEx("9999999", 3, ServerRoles{Unknown})
	if err != nil {
		t.Fatal(err)
	}
	if len(members) != 2 {
		t.Errorf("expected 2 members instead of %d", len(members))
	}
	if members[0].ID != "2222222222222222222222222222222222222222222222222222222222222222" {
		t.Errorf("wrong members[0]: %v", members[0])
	}
	if members[1].ID != "1111111111111111111111111111111111111111111111111111111111111111" {
		t.Errorf("wrong members[1]: %v", members[1])
	}
	members, err = x.GetNeighborsEx("9999999", 3, ServerRoles{Leader, Follower})
	if err != nil {
		t.Fatal(err)
	}
	if len(members) != 1 {
		t.Errorf("expected 1 members instead of %d", len(members))
	}
	if members[0].ID != "0000000000000000000000000000000000000000000000000000000000000000" {
		t.Errorf("wrong members[0]: %v", members[0])
	}
}
func TestGetNLess(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	x.Add(NewNodeFromString("2222222222222222222222222222222222222222222222222222222222222222"))
	members, err := x.GetNeighbors("9999999999999999999999999999999999999999999999999999999999999999", 2)
	if err != nil {
		t.Fatal(err)
	}
	if len(members) != 2 {
		t.Errorf("expected 2 members instead of %d", len(members))
	}
	if members[0].ID != "0000000000000000000000000000000000000000000000000000000000000000" {
		t.Errorf("wrong members[0]: %v", members[0])
	}
	if members[1].ID != "1111111111111111111111111111111111111111111111111111111111111111" {
		t.Errorf("wrong members[1]: %v", members[1])
	}
}
func TestGetNMore(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	x.Add(NewNodeFromString("2222222222222222222222222222222222222222222222222222222222222222"))
	members, err := x.GetNeighbors("9999999", 5)
	if err != nil {
		t.Fatal(err)
	}
	if len(members) != 3 {
		t.Errorf("expected 3 members instead of %d", len(members))
	}
	if members[0].ID != "0000000000000000000000000000000000000000000000000000000000000000" {
		t.Errorf("wrong members[0]: %v", members[0])
	}
	if members[1].ID != "2222222222222222222222222222222222222222222222222222222222222222" {
		t.Errorf("wrong members[1]: %v", members[1])
	}
	if members[2].ID != "1111111111111111111111111111111111111111111111111111111111111111" {
		t.Errorf("wrong members[2]: %v", members[2])
	}
}
func TestGetNEmpty(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	members, err := x.GetNeighbors("9999999", 5)
	if err != ErrEmptyCircle {
		t.Fatal(err)
	}
	if len(members) != 0 {
		t.Errorf("expected 3 members instead of %d", len(members))
	}
}
func TestGetNQuick(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	x.Add(NewNodeFromString("2222222222222222222222222222222222222222222222222222222222222222"))
	f := func(s string) bool {
		members, err := x.GetNeighbors(s, 3)
		if err != nil {
			t.Logf("error: %v", err)
			return false
		}
		if len(members) != 3 {
			t.Logf("expected 3 members instead of %d", len(members))
			return false
		}
		set := make(map[NodeID]Node, 4)
		for _, member := range members {
			if set[member.ID].ID != "" {
				t.Log("duplicate error")
				return false
			}
			set[member.ID] = Node{}
			if member.ID != "0000000000000000000000000000000000000000000000000000000000000000" && member.ID != "1111111111111111111111111111111111111111111111111111111111111111" && member.ID != "2222222222222222222222222222222222222222222222222222222222222222" {
				t.Logf("invalid member: %v", member)
				return false
			}
		}
		return true
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}
func TestGetNLessQuick(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	x.Add(NewNodeFromString("2222222222222222222222222222222222222222222222222222222222222222"))
	f := func(s string) bool {
		members, err := x.GetNeighbors(s, 2)
		if err != nil {
			t.Logf("error: %v", err)
			return false
		}
		if len(members) != 2 {
			t.Logf("expected 2 members instead of %d", len(members))
			return false
		}
		set := make(map[NodeID]Node, 4)
		for _, member := range members {
			if set[member.ID].ID != "" {
				t.Log("duplicate error")
				return false
			}
			set[member.ID] = Node{}
			if member.ID != "0000000000000000000000000000000000000000000000000000000000000000" && member.ID != "1111111111111111111111111111111111111111111111111111111111111111" && member.ID != "2222222222222222222222222222222222222222222222222222222222222222" {
				t.Logf("invalid member: %v", member)
				return false
			}
		}
		return true
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}
func TestGetNMoreQuick(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000000000000000000000000000000000000000000000000000000000000000"))
	x.Add(NewNodeFromString("1111111111111111111111111111111111111111111111111111111111111111"))
	x.Add(NewNodeFromString("2222222222222222222222222222222222222222222222222222222222222222"))
	f := func(s string) bool {
		members, err := x.GetNeighbors(s, 5)
		if err != nil {
			t.Logf("error: %v", err)
			return false
		}
		if len(members) != 3 {
			t.Logf("expected 3 members instead of %d", len(members))
			return false
		}
		set := make(map[NodeID]Node, 4)
		for _, member := range members {
			if set[member.ID].ID != "" {
				t.Log("duplicate error")
				return false
			}
			set[member.ID] = Node{}
			if member.ID != "0000000000000000000000000000000000000000000000000000000000000000" && member.ID != "1111111111111111111111111111111111111111111111111111111111111111" && member.ID != "2222222222222222222222222222222222222222222222222222222222222222" {
				t.Logf("invalid member: %v", member)
				return false
			}
		}
		return true
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}
func TestSet(t *testing.T) {
	kms.Unittest = true
	utils.RemoveAll(testStorePath + "*")
	kms.ResetBucket()
	x, _ := InitConsistent(testStorePath, new(KMSStorage), false)
	defer utils.RemoveAll(testStorePath + "*")
	x.Add(NewNodeFromString("0000"))
	x.Add(NewNodeFromString("1111"))
	x.Add(NewNodeFromString("2222"))
	x.Set([]Node{NewNodeFromString("3333"), NewNodeFromString("4444")})
	if len(x.sortedHashes)/x.NumberOfReplicas != 2 {
		t.Errorf("expected 2 elts, got %d", len(x.sortedHashes)/x.NumberOfReplicas)
	}
	a, b, err := x.GetTwoNeighbors("33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333wqer")
	if err != nil {
		t.Fatal(err)
	}
	if a.ID != "3333" && a.ID != "4444" {
		t.Errorf("expected 3333 or 4444, got %v", a)
	}
	if b.ID != "3333" && b.ID != "4444" {
		t.Errorf("expected 3333 or 4444, got %v", b)
	}
	if a.ID == b.ID {
		t.Errorf("expected a != b, they were both %v", a)
	}
	x.Set([]Node{NewNodeFromString("5555"), NewNodeFromString("4444")})
	if len(x.sortedHashes)/x.NumberOfReplicas != 2 {
		t.Errorf("expected 2 elts, got %d", len(x.sortedHashes)/x.NumberOfReplicas)
	}
	a, b, err = x.GetTwoNeighbors("33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333wqer")
	if err != nil {
		t.Fatal(err)
	}
	if a.ID != "5555" && a.ID != "4444" {
		t.Errorf("expected 3333 or 4444, got %v", a)
	}
	if b.ID != "5555" && b.ID != "4444" {
		t.Errorf("expected 3333 or 4444, got %v", b)
	}
	if a.ID == b.ID {
		t.Errorf("expected a != b, they were both %v", a)
	}
	x.Set([]Node{NewNodeFromString("5555"), NewNodeFromString("4444")})
	if len(x.sortedHashes)/x.NumberOfReplicas != 2 {
		t.Errorf("expected 2 elts, got %d", len(x.sortedHashes)/x.NumberOfReplicas)
	}
	a, b, err = x.GetTwoNeighbors("33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333wqer")
	if err != nil {
		t.Fatal(err)
	}
	if a.ID != "5555" && a.ID != "4444" {
		t.Errorf("expected 3333 or 4444, got %v", a)
	}
	if b.ID != "5555" && b.ID != "4444" {
		t.Errorf("expected 3333 or 4444, got %v", b)
	}
	if a.ID == b.ID {
		t.Errorf("expected a != b, they were both %v", a)
	}
}