Skip to content

Commit e228f9e

Browse files
committed
cleanup
1 parent b59222d commit e228f9e

File tree

3 files changed

+45
-33
lines changed

3 files changed

+45
-33
lines changed

monitoring/exporter/stackdriver/data_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ var (
8989
Aggregation: view.Sum(),
9090
}
9191

92-
// To make verification easy, we require all valid rows should int64 values and all of them
93-
// must be distinct.
92+
// To make verification easy, we require all valid rows should have int64 values and all of
93+
// them must be distinct.
9494
view1row1 = &view.Row{
9595
Tags: nil,
9696
Data: &view.SumData{Value: 1},

monitoring/exporter/stackdriver/mock_check_test.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,23 @@ import (
3030
// This file defines various mocks for testing, and checking functions for mocked data. We mock
3131
// metric client and bundler because their actions involves RPC calls or non-deterministic behavior.
3232

33+
// Following data are used to store various data generated by exporters' activity. They are used by
34+
// each test to verify intended behavior. Each test should call testDataInit() to clear these data.
3335
var (
34-
// bundlerRec records project ID of given bundler.
36+
// errStorage records all errors and associated RowData objects reported by exporter.
37+
errStorage []errRowData
38+
// bndlerProjMap records project ID of given bundler.
3539
bndlerProjMap map[*bundler.Bundler]string
36-
// projRds saves all RowData objects passed to addToBundler call by project ID.
40+
// projRds saves all RowData objects passed to addToBundler call by project ID. Since a
41+
// value of a map is not addressable, we save the pointer to the slice.
3742
projRds map[string]*[]*RowData
38-
3943
// timeSeriesReqs saves all incoming requests for creating time series.
4044
timeSeriesReqs []*mpb.CreateTimeSeriesRequest
4145
// timeSeriesResults holds predefined error values to be returned by mockCreateTimeSerie()
4246
// calls. Each errors in timeSeriesResults are returned per each mockCreateTimeSeries()
4347
// call. If all errors in timeSeriesResults are used, all other mockCreateTimeSeries calls
4448
// will return nil.
4549
timeSeriesResults []error
46-
47-
errRds []errRowData
4850
)
4951

5052
func init() {
@@ -58,14 +60,18 @@ func init() {
5860
addToBundler = mockAddToBundler
5961
}
6062

61-
func testInit() {
63+
// testDataInit() initializes all data needed for each test. This function must be called at the
64+
// beginning of each test.
65+
func testDataInit() {
6266
bndlerProjMap = map[*bundler.Bundler]string{}
6367
projRds = map[string]*[]*RowData{}
6468
timeSeriesReqs = nil
6569
timeSeriesResults = nil
66-
errRds = nil
70+
errStorage = nil
6771
}
6872

73+
// Mocked functions.
74+
6975
func mockNewMetricClient(_ context.Context, _ ...option.ClientOption) (*monitoring.MetricClient, error) {
7076
return nil, nil
7177
}
@@ -84,6 +90,7 @@ func mockCreateTimeSeries(_ *monitoring.MetricClient, _ context.Context, req *mp
8490

8591
func mockNewBundler(projectID string, _ interface{}, _ func(interface{})) *bundler.Bundler {
8692
bndler := &bundler.Bundler{}
93+
// Record the bundler's project ID.
8794
bndlerProjMap[bndler] = projectID
8895
return bndler
8996
}
@@ -92,15 +99,16 @@ func mockAddToBundler(bndler *bundler.Bundler, item interface{}, _ int) error {
9299
projID := bndlerProjMap[bndler]
93100
rds, ok := projRds[projID]
94101
if !ok {
95-
var rdsOrig []*RowData
96-
rds = &rdsOrig
102+
// For new project ID, create the actual slice and save its pointer.
103+
var rdsSlice []*RowData
104+
rds = &rdsSlice
97105
projRds[projID] = rds
98106
}
99107
*rds = append(*rds, item.(*RowData))
100108
return nil
101109
}
102110

103-
// checkMetricClient checks all recorded requests in mock metric client. We only compare int64
111+
// checkMetricClient checks all recorded requests to the metric client. We only compare int64
104112
// values of the time series. To make this work, we assigned different int64 values for all valid
105113
// rows in the test.
106114
func checkMetricClient(t *testing.T, wantReqsValues [][]int64) {
@@ -136,10 +144,10 @@ type errRowData struct {
136144
rds []*RowData
137145
}
138146

139-
// onError records any incoming error and accompanying RowData array. This method is passed to the
140-
// exporter to record errors.
147+
// testOnError records any incoming error and accompanying RowData array. This function is passed to
148+
// the exporter to record errors.
141149
func testOnError(err error, rds ...*RowData) {
142-
errRds = append(errRds, errRowData{err, rds})
150+
errStorage = append(errStorage, errRowData{err, rds})
143151
}
144152

145153
// errRowDataCheck contains data for checking content of error storage.
@@ -150,14 +158,14 @@ type errRowDataCheck struct {
150158

151159
// checkErrStorage checks content of error storage. For returned errors, we check prefix and suffix.
152160
func checkErrStorage(t *testing.T, wantErrRdCheck []errRowDataCheck) {
153-
gotLen, wantLen := len(errRds), len(wantErrRdCheck)
161+
gotLen, wantLen := len(errStorage), len(wantErrRdCheck)
154162
if gotLen != wantLen {
155163
t.Errorf("number of reported errors: %d, want: %d", gotLen, wantLen)
156164
return
157165
}
158166
for i := 0; i < gotLen; i++ {
159167
prefix := fmt.Sprintf("%d-th reported error mismatch", i+1)
160-
errRd, wantErrRd := errRds[i], wantErrRdCheck[i]
168+
errRd, wantErrRd := errStorage[i], wantErrRdCheck[i]
161169
errStr := errRd.err.Error()
162170
if errPrefix := wantErrRd.errPrefix; !strings.HasPrefix(errStr, errPrefix) {
163171
t.Errorf("%s: error got: %q, want: prefixed by %q", prefix, errStr, errPrefix)
@@ -200,7 +208,7 @@ func checkRowData(rd, wantRd *RowData) error {
200208
return nil
201209
}
202210

203-
// newMockExp creates mock expoter and error storage storing all errors. Caller need not set
211+
// newTestExp creates an exporter which saves error to errStorage. Caller should not set
204212
// opts.OnError.
205213
func newTestExp(t *testing.T, opts *Options) *Exporter {
206214
opts.OnError = testOnError
@@ -211,8 +219,8 @@ func newTestExp(t *testing.T, opts *Options) *Exporter {
211219
return exp
212220
}
213221

214-
// checkExpProjData checks all data passed to the bundler by bundler.Add().
215-
func checkExpProjData(t *testing.T, wantProjData map[string][]*RowData) {
222+
// checkProjData checks all data passed to the bundler by bundler.Add().
223+
func checkProjData(t *testing.T, wantProjData map[string][]*RowData) {
216224
wantProj := map[string]bool{}
217225
for proj := range wantProjData {
218226
wantProj[proj] = true
@@ -235,8 +243,8 @@ func checkExpProjData(t *testing.T, wantProjData map[string][]*RowData) {
235243
}
236244
}
237245

238-
// newMockUploader creates objects to test behavior of projectData.uploadRowData. Other uses are not
239-
// recommended.
246+
// newTestProjData creates a projectData object to test behavior of projectData.uploadRowData. Other
247+
// uses are not recommended. As newTestExp, all errors are saved to errStorage.
240248
func newTestProjData(t *testing.T, opts *Options) *projectData {
241249
return newTestExp(t, opts).newProjectData(project1)
242250
}

monitoring/exporter/stackdriver/stackdriver_test.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
// This file contains actual tests.
2626

27+
// TestAll runs all tests defined in this file.
2728
func TestAll(t *testing.T) {
2829
testData := []struct {
2930
name string
@@ -41,14 +42,14 @@ func TestAll(t *testing.T) {
4142

4243
for _, data := range testData {
4344
run := func(t *testing.T) {
44-
testInit()
45+
testDataInit()
4546
data.test(t)
4647
}
4748
t.Run(data.name, run)
4849
}
4950
}
5051

51-
// TestProjectClassifyNoError tests that exporter can recognize and distribute incoming data by
52+
// testProjectClassifyNoError tests that exporter can recognize and distribute incoming data by
5253
// its project.
5354
func testProjectClassifyNoError(t *testing.T) {
5455
viewData1 := &view.Data{
@@ -89,10 +90,10 @@ func testProjectClassifyNoError(t *testing.T) {
8990
},
9091
}
9192
checkErrStorage(t, nil)
92-
checkExpProjData(t, wantRowData)
93+
checkProjData(t, wantRowData)
9394
}
9495

95-
// TestProjectClassifyError tests that exporter can properly handle errors while classifying
96+
// testProjectClassifyError tests that exporter can properly handle errors while classifying
9697
// incoming data by its project.
9798
func testProjectClassifyError(t *testing.T) {
9899
viewData1 := &view.Data{
@@ -139,9 +140,11 @@ func testProjectClassifyError(t *testing.T) {
139140
},
140141
}
141142
checkErrStorage(t, wantErrRdCheck)
142-
checkExpProjData(t, wantRowData)
143+
checkProjData(t, wantRowData)
143144
}
144145

146+
// testDefaultProjectClassify tests that defaultGetProjectID classifies RowData by tag with key name
147+
// "project_id".
145148
func testDefaultProjectClassify(t *testing.T) {
146149
viewData1 := &view.Data{
147150
View: view1,
@@ -164,10 +167,11 @@ func testDefaultProjectClassify(t *testing.T) {
164167
project1: []*RowData{{view3, startTime2, endTime2, view3row1}},
165168
project2: []*RowData{{view3, startTime2, endTime2, view3row2}},
166169
}
167-
checkExpProjData(t, wantRowData)
170+
checkProjData(t, wantRowData)
168171
}
169172

170-
//
173+
// testUploadNoError tests that all RowData objects passed to uploadRowData() are grouped by
174+
// slice of length MaxTimeSeriesPerUpload, and passed to createTimeSeries().
171175
func testUploadNoError(t *testing.T) {
172176
pd := newTestProjData(t, &Options{})
173177
rd := []*RowData{
@@ -187,7 +191,7 @@ func testUploadNoError(t *testing.T) {
187191
checkMetricClient(t, wantClData)
188192
}
189193

190-
// TestUploadTimeSeriesMakeError tests that errors while creating time series are properly handled.
194+
// testUploadTimeSeriesMakeError tests that errors while creating time series are properly handled.
191195
func testUploadTimeSeriesMakeError(t *testing.T) {
192196
makeResource := func(rd *RowData) (*mrpb.MonitoredResource, error) {
193197
if rd.Row == view1row2 {
@@ -227,7 +231,7 @@ func testUploadTimeSeriesMakeError(t *testing.T) {
227231
checkMetricClient(t, wantClData)
228232
}
229233

230-
// TestUploadTimeSeriesMakeError tests that exporter can handle error on metric client's time
234+
// testUploadTimeSeriesMakeError tests that exporter can handle error on metric client's time
231235
// series create RPC call.
232236
func testUploadWithMetricClientError(t *testing.T) {
233237
pd := newTestProjData(t, &Options{})
@@ -261,7 +265,7 @@ func testUploadWithMetricClientError(t *testing.T) {
261265
checkMetricClient(t, wantClData)
262266
}
263267

264-
// TestMakeResource tests that exporter can create monitored resource dynamically.
268+
// testMakeResource tests that exporter can create monitored resource dynamically.
265269
func testMakeResource(t *testing.T) {
266270
makeResource := func(rd *RowData) (*mrpb.MonitoredResource, error) {
267271
switch rd.Row {
@@ -290,7 +294,7 @@ func testMakeResource(t *testing.T) {
290294
}
291295
}
292296

293-
// TestMakeLabel tests that exporter can correctly handle label manipulation process, including
297+
// testMakeLabel tests that exporter can correctly handle label manipulation process, including
294298
// merging default label with tags, and removing unexported labels.
295299
func testMakeLabel(t *testing.T) {
296300
opts := &Options{

0 commit comments

Comments
 (0)