Repository: grpc/grpc-go
File: xds/internal/clients/lrsclient/load_store_test.go (Go)

Code before:
// calculated between consecutive calls to stats().
func TestStoreReportInterval(t *testing.T) {
	originalClockNow := clockNow
	t.Cleanup(func() { clockNow = originalClockNow })

	// Initial time for reporter creation
	currentTime := time.Now()
	clockNow = func() time.Time {
		return currentTime
	}

	store := newLoadStore()
	reporter := store.ReporterForCluster("test-cluster", "test-service")
	// To ensure stats() returns non-nil data, report a dummy drop.
	reporter.CallDropped("dummy-category")

	// First call to stats() calculates the report interval from reporter
	// creation time.
	currentTime = currentTime.Add(5 * time.Second)
	stats1 := reporter.stats()

	if stats1 == nil {
		t.Fatalf("stats1 is nil after reporting a drop, want non-nil")
	}
	wantInterval := 5 * time.Second
	if stats1.reportInterval != wantInterval {
		t.Errorf("First call stats() = %v, want %v", stats1.reportInterval, wantInterval)
	}

	// Second call to stats() calculates the report interval from last stats()
	// call time.
	currentTime = currentTime.Add(10 * time.Second)
	// Report another dummy drop to ensure stats2 is not nil.
	reporter.CallDropped("dummy-category-2")
	stats2 := reporter.stats()

	if stats2 == nil {
		t.Fatalf("stats2 is nil after reporting a drop, want non-nil")
	}
	wantInterval = 10 * time.Second
	if stats2.reportInterval != wantInterval {
		t.Errorf("Second call stats() = %v, want %v", stats2.reportInterval, wantInterval)
	}
}


Review comment (suggestion):
Optional:

I think the best way I've been recommended of writing this kind of test comparison is:

```go
if got, want := stats1.reportInterval, 5 * time.Second; got != want {
	t.Fatalf("blah blah = %v; want %v", got, want)
}
```

Code after:
// TestStoreReportInterval verify that the load report interval gets
// calculated at every stats() call and is the duration between start of last
// load reporting to next stats() call.
func TestStoreReportInterval(t *testing.T) {
	originalTimeNow := lrsclientinternal.TimeNow
	t.Cleanup(func() { lrsclientinternal.TimeNow = originalTimeNow })

	// Initial time for reporter creation
	currentTime := time.Now()
	lrsclientinternal.TimeNow = func() time.Time {
		return currentTime
	}

	store := newLoadStore()
	reporter := store.ReporterForCluster("test-cluster", "test-service")
	// Report dummy drop to ensure stats1 is not nil.
	reporter.CallDropped("dummy-category")

	// Update currentTime to simulate the passage of time between the reporter
	// creation and first stats() call.
	currentTime = currentTime.Add(5 * time.Second)
	stats1 := reporter.stats()

	if stats1 == nil {
		t.Fatalf("stats1 is nil after reporting a drop, want non-nil")
	}
	// Verify stats() call calculate the report interval from the time of
	// reporter creation.
	if got, want := stats1.reportInterval, 5*time.Second; got != want {
		t.Errorf("stats1.reportInterval = %v, want %v", stats1.reportInterval, want)
	}

	// Update currentTime to simulate the passage of time between the first
	// and second stats() call.
	currentTime = currentTime.Add(10 * time.Second)
	// Report another dummy drop to ensure stats2 is not nil.
	reporter.CallDropped("dummy-category-2")
	stats2 := reporter.stats()

	if stats2 == nil {
		t.Fatalf("stats2 is nil after reporting a drop, want non-nil")
	}
	// Verify stats() call calculate the report interval from the time of first
	// stats() call.
	if got, want := stats2.reportInterval, 10*time.Second; got != want {
		t.Errorf("stats2.reportInterval = %v, want %v", stats2.reportInterval, want)
	}
}