func TestStoreStatsEmptyDataNotReported(t *testing.T) {
	var (
		testServices = []string{"s0", "s1"}
		testLocality = "test-locality"
	)
	store := NewStore()
	for _, s := range testServices {
		store.PerCluster("c0", s).CallStarted(testLocality)
		store.PerCluster("c0", s).CallFinished(testLocality, nil)
	}
	for _, s := range testServices {
		store.PerCluster("c1", s).CallStarted(testLocality)
	}
	want0 := []*Data{
		{
			Cluster: "c0", Service: "s0",
			LocalityStats: map[string]LocalityData{
				"test-locality": {RequestStats: RequestData{Succeeded: 1}},
			},
		},
		{
			Cluster: "c0", Service: "s1",
			LocalityStats: map[string]LocalityData{
				"test-locality": {RequestStats: RequestData{Succeeded: 1}},
			},
		},
		{
			Cluster: "c1", Service: "s0",
			LocalityStats: map[string]LocalityData{
				"test-locality": {RequestStats: RequestData{InProgress: 1}},
			},
		},
		{
			Cluster: "c1", Service: "s1",
			LocalityStats: map[string]LocalityData{
				"test-locality": {RequestStats: RequestData{InProgress: 1}},
			},
		},
	}
	got0 := store.Stats(nil)
	if diff := cmp.Diff(want0, got0, cmpopts.EquateEmpty(), cmpopts.IgnoreFields(Data{}, "ReportInterval"), sortDataSlice); diff != "" {
		t.Errorf("store.stats() returned unexpected diff (-want +got):\n%s", diff)
	}
	want1 := []*Data{
		{
			Cluster: "c1", Service: "s0",
			LocalityStats: map[string]LocalityData{
				"test-locality": {RequestStats: RequestData{InProgress: 1}},
			},
		},
		{
			Cluster: "c1", Service: "s1",
			LocalityStats: map[string]LocalityData{
				"test-locality": {RequestStats: RequestData{InProgress: 1}},
			},
		},
	}
	got1 := store.Stats(nil)
	if diff := cmp.Diff(want1, got1, cmpopts.EquateEmpty(), cmpopts.IgnoreFields(Data{}, "ReportInterval"), sortDataSlice); diff != "" {
		t.Errorf("store.stats() returned unexpected diff (-want +got):\n%s", diff)
	}
}
Test the cases that if a cluster doesn't have load to report, its data is not
appended to the slice returned by Stats().
package load
import (
	"fmt"
	"sort"
	"sync"
	"testing"
	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)
var (
	dropCategories = []string{"drop_for_real", "drop_for_fun"}
	localities     = []string{"locality-A", "locality-B"}
	errTest        = fmt.Errorf("test error")
)
type rpcData struct {
	start, success, failure int
	serverData              map[string]float64  
}
func TestDrops(t *testing.T) {
	var (
		drops = map[string]int{
			dropCategories[0]: 30,
			dropCategories[1]: 40,
			"":                10,
		}
		wantStoreData = &Data{
			TotalDrops: 80,
			Drops: map[string]uint64{
				dropCategories[0]: 30,
				dropCategories[1]: 40,
			},
		}
	)
	ls := perClusterStore{}
	var wg sync.WaitGroup
	for category, count := range drops {
		for i := 0; i < count; i++ {
			wg.Add(1)
			go func(c string) {
				ls.CallDropped(c)
				wg.Done()
			}(category)
		}
	}
	wg.Wait()
	gotStoreData := ls.stats()
	if diff := cmp.Diff(wantStoreData, gotStoreData, cmpopts.EquateEmpty(), cmpopts.IgnoreFields(Data{}, "ReportInterval")); diff != "" {
		t.Errorf("store.stats() returned unexpected diff (-want +got):\n%s", diff)
	}
}
func TestLocalityStats(t *testing.T) {
	var (
		localityData = map[string]rpcData{
			localities[0]: {
				start:      40,
				success:    20,
				failure:    10,
				serverData: map[string]float64{"net": 1, "disk": 2, "cpu": 3, "mem": 4},
			},
			localities[1]: {
				start:      80,
				success:    40,
				failure:    20,
				serverData: map[string]float64{"net": 1, "disk": 2, "cpu": 3, "mem": 4},
			},
		}
		wantStoreData = &Data{
			LocalityStats: map[string]LocalityData{
				localities[0]: {
					RequestStats: RequestData{Succeeded: 20, Errored: 10, InProgress: 10},
					LoadStats: map[string]ServerLoadData{
						"net":  {Count: 20, Sum: 20},
						"disk": {Count: 20, Sum: 40},
						"cpu":  {Count: 20, Sum: 60},
						"mem":  {Count: 20, Sum: 80},
					},
				},
				localities[1]: {
					RequestStats: RequestData{Succeeded: 40, Errored: 20, InProgress: 20},
					LoadStats: map[string]ServerLoadData{
						"net":  {Count: 40, Sum: 40},
						"disk": {Count: 40, Sum: 80},
						"cpu":  {Count: 40, Sum: 120},
						"mem":  {Count: 40, Sum: 160},
					},
				},
			},
		}
	)
	ls := perClusterStore{}
	var wg sync.WaitGroup
	for locality, data := range localityData {
		wg.Add(data.start)
		for i := 0; i < data.start; i++ {
			go func(l string) {
				ls.CallStarted(l)
				wg.Done()
			}(locality)
		}
		wg.Wait()
		wg.Add(data.success)
		for i := 0; i < data.success; i++ {
			go func(l string, serverData map[string]float64) {
				ls.CallFinished(l, nil)
				for n, d := range serverData {
					ls.CallServerLoad(l, n, d)
				}
				wg.Done()
			}(locality, data.serverData)
		}
		wg.Add(data.failure)
		for i := 0; i < data.failure; i++ {
			go func(l string) {
				ls.CallFinished(l, errTest)
				wg.Done()
			}(locality)
		}
		wg.Wait()
	}
	gotStoreData := ls.stats()
	if diff := cmp.Diff(wantStoreData, gotStoreData, cmpopts.EquateEmpty(), cmpopts.IgnoreFields(Data{}, "ReportInterval")); diff != "" {
		t.Errorf("store.stats() returned unexpected diff (-want +got):\n%s", diff)
	}
}
func TestResetAfterStats(t *testing.T) {
	var (
		drops = map[string]int{
			dropCategories[0]: 30,
			dropCategories[1]: 40,
		}
		localityData = map[string]rpcData{
			localities[0]: {
				start:      40,
				success:    20,
				failure:    10,
				serverData: map[string]float64{"net": 1, "disk": 2, "cpu": 3, "mem": 4},
			},
			localities[1]: {
				start:      80,
				success:    40,
				failure:    20,
				serverData: map[string]float64{"net": 1, "disk": 2, "cpu": 3, "mem": 4},
			},
		}
		wantStoreData = &Data{
			TotalDrops: 70,
			Drops: map[string]uint64{
				dropCategories[0]: 30,
				dropCategories[1]: 40,
			},
			LocalityStats: map[string]LocalityData{
				localities[0]: {
					RequestStats: RequestData{Succeeded: 20, Errored: 10, InProgress: 10},
					LoadStats: map[string]ServerLoadData{
						"net":  {Count: 20, Sum: 20},
						"disk": {Count: 20, Sum: 40},
						"cpu":  {Count: 20, Sum: 60},
						"mem":  {Count: 20, Sum: 80},
					},
				},
				localities[1]: {
					RequestStats: RequestData{Succeeded: 40, Errored: 20, InProgress: 20},
					LoadStats: map[string]ServerLoadData{
						"net":  {Count: 40, Sum: 40},
						"disk": {Count: 40, Sum: 80},
						"cpu":  {Count: 40, Sum: 120},
						"mem":  {Count: 40, Sum: 160},
					},
				},
			},
		}
	)
	reportLoad := func(ls *perClusterStore) {
		for category, count := range drops {
			for i := 0; i < count; i++ {
				ls.CallDropped(category)
			}
		}
		for locality, data := range localityData {
			for i := 0; i < data.start; i++ {
				ls.CallStarted(locality)
			}
			for i := 0; i < data.success; i++ {
				ls.CallFinished(locality, nil)
				for n, d := range data.serverData {
					ls.CallServerLoad(locality, n, d)
				}
			}
			for i := 0; i < data.failure; i++ {
				ls.CallFinished(locality, errTest)
			}
		}
	}
	ls := perClusterStore{}
	reportLoad(&ls)
	gotStoreData := ls.stats()
	if diff := cmp.Diff(wantStoreData, gotStoreData, cmpopts.EquateEmpty(), cmpopts.IgnoreFields(Data{}, "ReportInterval")); diff != "" {
		t.Errorf("store.stats() returned unexpected diff (-want +got):\n%s", diff)
	}
	for _, l := range localities {
		ls := wantStoreData.LocalityStats[l]
		ls.RequestStats.InProgress *= 2
		wantStoreData.LocalityStats[l] = ls
	}
	reportLoad(&ls)
	gotStoreData = ls.stats()
	if diff := cmp.Diff(wantStoreData, gotStoreData, cmpopts.EquateEmpty(), cmpopts.IgnoreFields(Data{}, "ReportInterval")); diff != "" {
		t.Errorf("store.stats() returned unexpected diff (-want +got):\n%s", diff)
	}
}
var sortDataSlice = cmp.Transformer("SortDataSlice", func(in []*Data) []*Data {
	out := append([]*Data(nil), in...)  
	sort.Slice(out,
		func(i, j int) bool {
			if out[i].Cluster < out[j].Cluster {
				return true
			}
			if out[i].Cluster == out[j].Cluster {
				return out[i].Service < out[j].Service
			}
			return false
		},
	)
	return out
})
func TestStoreStats(t *testing.T) {
	var (
		testClusters = []string{"c0", "c1", "c2"}
		testServices = []string{"s0", "s1"}
		testLocality = "test-locality"
	)
	store := NewStore()
	for _, c := range testClusters {
		for _, s := range testServices {
			store.PerCluster(c, s).CallStarted(testLocality)
			store.PerCluster(c, s).CallServerLoad(testLocality, "abc", 123)
			store.PerCluster(c, s).CallDropped("dropped")
			store.PerCluster(c, s).CallFinished(testLocality, nil)
		}
	}
	wantC0 := []*Data{
		{
			Cluster: "c0", Service: "s0",
			TotalDrops: 1, Drops: map[string]uint64{"dropped": 1},
			LocalityStats: map[string]LocalityData{
				"test-locality": {
					RequestStats: RequestData{Succeeded: 1},
					LoadStats:    map[string]ServerLoadData{"abc": {Count: 1, Sum: 123}},
				},
			},
		},
		{
			Cluster: "c0", Service: "s1",
			TotalDrops: 1, Drops: map[string]uint64{"dropped": 1},
			LocalityStats: map[string]LocalityData{
				"test-locality": {
					RequestStats: RequestData{Succeeded: 1},
					LoadStats:    map[string]ServerLoadData{"abc": {Count: 1, Sum: 123}},
				},
			},
		},
	}
	gotC0 := store.Stats([]string{"c0"})
	if diff := cmp.Diff(wantC0, gotC0, cmpopts.EquateEmpty(), cmpopts.IgnoreFields(Data{}, "ReportInterval"), sortDataSlice); diff != "" {
		t.Errorf("store.stats() returned unexpected diff (-want +got):\n%s", diff)
	}
	wantOther := []*Data{
		{
			Cluster: "c1", Service: "s0",
			TotalDrops: 1, Drops: map[string]uint64{"dropped": 1},
			LocalityStats: map[string]LocalityData{
				"test-locality": {
					RequestStats: RequestData{Succeeded: 1},
					LoadStats:    map[string]ServerLoadData{"abc": {Count: 1, Sum: 123}},
				},
			},
		},
		{
			Cluster: "c1", Service: "s1",
			TotalDrops: 1, Drops: map[string]uint64{"dropped": 1},
			LocalityStats: map[string]LocalityData{
				"test-locality": {
					RequestStats: RequestData{Succeeded: 1},
					LoadStats:    map[string]ServerLoadData{"abc": {Count: 1, Sum: 123}},
				},
			},
		},
		{
			Cluster: "c2", Service: "s0",
			TotalDrops: 1, Drops: map[string]uint64{"dropped": 1},
			LocalityStats: map[string]LocalityData{
				"test-locality": {
					RequestStats: RequestData{Succeeded: 1},
					LoadStats:    map[string]ServerLoadData{"abc": {Count: 1, Sum: 123}},
				},
			},
		},
		{
			Cluster: "c2", Service: "s1",
			TotalDrops: 1, Drops: map[string]uint64{"dropped": 1},
			LocalityStats: map[string]LocalityData{
				"test-locality": {
					RequestStats: RequestData{Succeeded: 1},
					LoadStats:    map[string]ServerLoadData{"abc": {Count: 1, Sum: 123}},
				},
			},
		},
	}
	gotOther := store.Stats(nil)
	if diff := cmp.Diff(wantOther, gotOther, cmpopts.EquateEmpty(), cmpopts.IgnoreFields(Data{}, "ReportInterval"), sortDataSlice); diff != "" {
		t.Errorf("store.stats() returned unexpected diff (-want +got):\n%s", diff)
	}
}