@@ -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.
3335var (
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
5052func 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+
6975func 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
8591func 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.
106114func 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.
141149func 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.
152160func 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.
205213func 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 .
240248func newTestProjData (t * testing.T , opts * Options ) * projectData {
241249 return newTestExp (t , opts ).newProjectData (project1 )
242250}
0 commit comments