コード例 #1
0
ファイル: dhash_test.go プロジェクト: rrudduck/golang-stuff
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)
}