func testMigrate(t *testing.T, dhashes []*Node) { for _, d := range dhashes { d.Clear() } var item common.Item for i := 0; i < 1000; i++ { item.Key = []byte(fmt.Sprint(i)) item.Value = []byte(fmt.Sprint(i)) item.Timestamp = 1 dhashes[0].Put(item) } common.AssertWithin(t, func() (string, bool) { sum := 0 status := new(bytes.Buffer) ordered := dhashAry(dhashes) sort.Sort(ordered) lastOwned := ordered[len(ordered)-1].Owned() ok := true for _, d := range ordered { sum += d.Owned() fmt.Fprintf(status, "%v %v %v\n", d.node.GetBroadcastAddr(), common.HexEncode(d.node.GetPosition()), d.Owned()) if float64(lastOwned)/float64(d.Owned()) > migrateHysteresis { ok = false } if d.Owned() == 0 { ok = false } lastOwned = d.Owned() } return string(status.Bytes()), ok && sum == 1000 }, time.Second*100) }
func testSync(t *testing.T, dhashes []*Node) { dhashes[0].tree.Put([]byte{3}, []byte{0}, 1) common.AssertWithin(t, func() (string, bool) { having := countHaving(t, dhashes, []byte{3}, []byte{0}) return fmt.Sprint(having), having == common.Redundancy }, time.Second*10) }
func testClean(t *testing.T, dhashes []*Node) { for _, n := range dhashes { n.tree.Put([]byte{1}, []byte{1}, 1) } common.AssertWithin(t, func() (string, bool) { having := countHaving(t, dhashes, []byte{1}, []byte{1}) return fmt.Sprint(having), having == common.Redundancy }, time.Second*20) }
func testPut(t *testing.T, dhashes []*Node) { for index, n := range dhashes { n.Put(common.Item{Key: []byte{byte(index + 100)}, Timestamp: 1, Value: []byte{byte(index + 100)}}) } common.AssertWithin(t, func() (string, bool) { haves := make(map[int]bool) for index, _ := range dhashes { count := countHaving(t, dhashes, []byte{byte(index + 100)}, []byte{byte(index + 100)}) haves[count] = true } return fmt.Sprint(haves), len(haves) == 1 && haves[common.Redundancy] == true }, time.Second*10) }
func assertMirrored(t *testing.T, dhashes []*Node, c testClient, subTree []byte) { common.AssertWithin(t, func() (string, bool) { for _, n := range dhashes { var conf common.Conf n.SubConfiguration(subTree, &conf) var s int n.SubSize(subTree, &s) if s > 0 { if conf.Data["mirrored"] != "yes" { return fmt.Sprint(n, conf.Data), false } } } return "", true }, time.Second*10) }
func TestSample(t *testing.T) { producer := newTestPeerProducer() peer1 := producer.makePeer() peer2 := producer.makePeer() peer3 := producer.makePeer() peer4 := producer.makePeer() producer.add("1", peer1) producer.add("2", peer2) producer.add("3", peer3) producer.add("4", peer4) peer1.Start() peer2.Start() peer3.Start() peer4.Start() common.AssertWithin(t, func() (string, bool) { d := producer.deviance() return fmt.Sprint(d), d > 0 && d < 1000000 }, time.Second*20) }
func TestStartup(t *testing.T) { firstPort := 9191 var nodes []*Node n := 10 for i := 0; i < n; i++ { nodes = append(nodes, NewNode(fmt.Sprintf("%v:%v", "127.0.0.1", firstPort+i), fmt.Sprintf("%v:%v", "127.0.0.1", firstPort+i))) } for i := 0; i < n; i++ { nodes[i].MustStart() } for i := 1; i < n; i++ { nodes[i].MustJoin(nodes[0].GetBroadcastAddr()) } common.AssertWithin(t, func() (string, bool) { routes := make(map[string]bool) for i := 0; i < n; i++ { routes[nodes[i].Nodes().Describe()] = true } return fmt.Sprint(routes), len(routes) == 1 && nodes[0].ring.Size() > 0 }, time.Second*30) }
func testStartup(t *testing.T, n, port int) (dhashes []*Node) { for i := 0; i < n; i++ { os.RemoveAll(fmt.Sprintf("127.0.0.1:%v", port+i*2)) } dhashes = make([]*Node, n) for i := 0; i < n; i++ { dhashes[i] = NewNode(fmt.Sprintf("127.0.0.1:%v", port+i*2), fmt.Sprintf("127.0.0.1:%v", port+i*2)) dhashes[i].MustStart() } for i := 1; i < n; i++ { dhashes[i].MustJoin(fmt.Sprintf("127.0.0.1:%v", port)) } common.AssertWithin(t, func() (string, bool) { routes := make(map[string]bool) for _, dhash := range dhashes { routes[dhash.node.GetNodes().Describe()] = true } return fmt.Sprint(routes), len(routes) == 1 }, time.Second*10) return }