func TestDupSupress(t *testing.T) {

	f := clustercache.GetFunc(func(key string) ([]byte, error) {
		return []byte(<-dataRepo), nil
	})
	cc := clustercache.NewClusterCache("TestDupSupress", cluster.DefaultCluster{}, uint64(1<<20), uint64(10000), f)

	callNum := 2
	for i := 0; i < callNum; i++ {
		go func() {
			if value, err := cc.Get("testKey"); err != nil {
				result <- err.Error()
			} else {
				result <- string(value)
			}
		}()
	}

	dataRepo <- "Hello!"

	for i := 0; i < callNum; i++ {
		select {
		case data := <-result:
			if data != "Hello!" {
				t.Errorf("The value of #%dshould be 'Hello!', but found %s", i, data)
			}

		case <-time.After(1 * time.Second):
			t.Errorf("Timeout while waiting #%d", i)
		}
	}
}
func TestCluster(t *testing.T) {

	c := testCluster(make([]cluster.Peer, 5))
	for i := 0; i < 5; i++ {
		c[i] = &testPeer{}
	}
	getLocally := 0
	getter := clustercache.GetFunc(func(key string) ([]byte, error) {
		getLocally++
		return []byte(key), nil
	})
	cc := clustercache.NewClusterCache("TestCluster", c, (1 << 20), 10000, getter)

	execute := func(n int) string {
		for i := 0; i < n; i++ {
			cc.Get(strconv.Itoa(i))
		}

		res := strconv.Itoa(getLocally)
		for _, peer := range c {
			switch v := peer.(type) {
			case *testPeer:
				res += "-" + strconv.Itoa(int(v.hitCnt))
			default:
				res += "-" + "0"
			}
		}
		return res
	}

	res := execute(100)
	ans := "0-20-20-20-20-20"
	if res != ans {
		t.Errorf("requests distribution should be %s, but found %s", ans, res)
	}

	for _, peer := range c {
		switch v := peer.(type) {
		case *testPeer:
			v.hitCnt = 0
		}
	}
	c[1] = nil
	c[3] = nil

	res = execute(100)
	ans = "40-20-0-20-0-20"
	if res != ans {
		t.Errorf("requests distribution should be %s, but found %s", ans, res)
	}
}
Esempio n. 3
0
func TestNetPool(t *testing.T) {

	//for i := 0; i < len(stopSignal); i++ {
	//	stopSignal[i] = make(chan bool)
	//}

	cacheList := make([]*cc.ClusterCache, len(hostList))
	getters := make([]cc.GetFunc, 3)

	getters[0] = cc.GetFunc(func(key string) ([]byte, error) {
		return []byte(key + hostList[0]), nil
	})

	getters[1] = cc.GetFunc(func(key string) ([]byte, error) {
		return []byte(key + hostList[1]), nil
	})

	getters[2] = cc.GetFunc(func(key string) ([]byte, error) {
		return []byte(key + hostList[2]), nil
	})

	for i, host := range hostList {
		np := netp.NewNetPool(host, hostList...)

		ccache := cc.NewClusterCache(host, np, 1<<20, 1000, getters[i])
		cacheList[i] = ccache
	}

	time.Sleep(500 * time.Millisecond)

	answers := make([][]string, len(hostList))

	for i, ccache := range cacheList {
		for _, key := range keys {

			value, err := ccache.Get(key)
			if err != nil {
				t.Errorf(err.Error())
			} else {
				answers[i] = append(answers[i], string(value))
			}
		}
	}

	for i := 0; i < len(keys); i++ {

		for j := 1; j < len(hostList); j++ {

			if answers[j][i] != answers[j-1][i] {
				t.Errorf("Not corresponding: %s-%s %s-%s",
					hostList[j-1], answers[j-1][i],
					hostList[j], answers[j][i])
			}
		}
	}

	fmt.Println(answers)

	//for _, c := range stopSignal {
	//	c <- true
	//}
}