Пример #1
0
func updateFriendsData(nFriends int) error {
	var err error
	log.Debugln("Updating friends data")
	err = clearFriendsData()
	if err != nil {
		return err
	}

	peers := getAllPeers()
	log.WithField("peers", len(peers)).Debugln("Number of peers")
	if len(peers) == 0 {
		return ErrNoPeers
	}

	friends, err := chooseRandomFriends(peers, nFriends)
	if err != nil {
		return err
	}
	log.WithField("friends", friends).Debugln("Friends to connect with")

	friendsData, err := getFriendsData(friends)
	if err != nil {
		return err
	}

	clusterData, err := mergeSharedData(friendsData)
	if err != nil {
		return err
	}

	data.SaveSharedCluster(clusterData)

	return nil
}
Пример #2
0
func TestUpdateMetrics(t *testing.T) {
	cfg.SetServices(service.CreateMockServices())

	updateMetrics()
	assert.Equal(t, 0.0, Metrics().Service["service1"].Stats.BaseMetrics[enum.METRIC_CPU_AVG.ToString()])
	assert.Equal(t, 0.0, Metrics().Service["service2"].Analytics.BaseAnalytics[enum.METRIC_CPU_AVG.ToString()])
	assert.Equal(t, "noaction", Metrics().Policy.Name)

	data.SaveMockStats()
	data.SaveMockAnalytics()
	data.SaveSharedCluster(data.CreateMockShared())
	plc := data.CreateMockPolicy("policy", 1.0, []string{"pippo"}, map[string][]enum.Action{})
	data.SavePolicy(plc)
	updateMetrics()
	assert.Equal(t, 0.6, Metrics().Service["service1"].Stats.BaseMetrics[enum.METRIC_CPU_AVG.ToString()])
	assert.Equal(t, 0.1, Metrics().Service["service2"].Analytics.BaseAnalytics[enum.METRIC_CPU_AVG.ToString()])
	assert.Equal(t, 0.6, metrics.Service["service1"].Shared.BaseShared[enum.METRIC_CPU_AVG.ToString()])
	assert.Equal(t, "policy", Metrics().Policy.Name)
}