Пример #1
0
func TestClearFriendsData(t *testing.T) {
	var err error
	storage.New("internal")
	data := []byte{}
	storage.StoreLocalData(data, enum.SHARED)
	storage.StoreClusterData(data, enum.SHARED)
	storage.StoreData("node1", data, enum.SHARED)
	storage.StoreData("node2", data, enum.SHARED)
	storage.StoreData("node3", data, enum.SHARED)

	err = clearFriendsData()
	assert.NoError(t, err)
	stored, _ := storage.GetAllData(enum.SHARED)
	assert.Equal(t, 2, len(stored))
}
Пример #2
0
func getFriendsData(friends map[string]string) ([]data.Shared, error) {
	var err error
	friendsData := make([]data.Shared, 0, len(friends))

	for friend, address := range friends {
		friendRoute := address + c_ROUTE_SHARED
		friendData, err := network.DoRequest("GET", friendRoute, nil)
		if err != nil {
			log.WithField("address", address).Debugln("Error retrieving friend stats")
		}
		err = storage.StoreData(friend, friendData, enum.SHARED)
		if err != nil {
			log.WithField("err", err).Debugln("Error storing friends data")
		}

		sharedData, err := data.ByteToShared(friendData)
		if err != nil {
			log.WithField("address", address).Debugln("Friend data not stored")
		} else {
			friendsData = append(friendsData, sharedData)
		}

	}

	return friendsData, err
}
Пример #3
0
func saveData(data interface{}, dataType enum.Datatype, dataOwner enum.DataOwner) error {
	var encoded []byte
	var err error
	switch dataType {
	case enum.STATS:
		stats := data.(GruStats)
		encoded, err = json.Marshal(stats)
		if err != nil {
			return err
		}
	case enum.ANALYTICS:
		analytics := data.(GruAnalytics)
		encoded, err = json.Marshal(analytics)
		if err != nil {
			return err
		}
	case enum.POLICIES:
		policy := data.(Policy)
		encoded, err = json.Marshal(policy)
		if err != nil {
			return err
		}
	case enum.SHARED:
		info := data.(Shared)
		encoded, err = json.Marshal(info)
		if err != nil {
			return err
		}
	default:
		return errors.New("Cannot save data: unknown data type")
	}

	storage.StoreData(dataOwner.ToString(), encoded, dataType)

	return nil
}