@@ -35,8 +35,8 @@ import (
3535var (
3636 // errStorage records all errors and associated RowData objects reported by exporter.
3737 errStorage []errRowData
38- // bndlerProjMap records project ID of given bundler .
39- bndlerProjMap map [* bundler. Bundler ] string
38+ // projDataMap is a copy of projDataMap used by each tests .
39+ projDataMap map [string ] * projectData
4040 // projRds saves all RowData objects passed to addToBundler call by project ID. Since a
4141 // value of a map is not addressable, we save the pointer to the slice.
4242 projRds map [string ]* []* RowData
@@ -63,7 +63,7 @@ func init() {
6363// testDataInit() initializes all data needed for each test. This function must be called at the
6464// beginning of each test.
6565func testDataInit () {
66- bndlerProjMap = map [ * bundler. Bundler ] string {}
66+ projDataMap = nil
6767 projRds = map [string ]* []* RowData {}
6868 timeSeriesReqs = nil
6969 timeSeriesResults = nil
@@ -88,15 +88,29 @@ func mockCreateTimeSeries(_ *monitoring.MetricClient, _ context.Context, req *mp
8888 return err
8989}
9090
91- func mockNewBundler (projectID string , _ interface {}, _ func (interface {})) * bundler.Bundler {
92- bndler := & bundler.Bundler {}
93- // Record the bundler's project ID.
94- bndlerProjMap [bndler ] = projectID
95- return bndler
91+ func mockNewBundler (_ interface {}, _ func (interface {})) * bundler.Bundler {
92+ // We do not return nil but create an empty Bundler object because
93+ // 1. Exporter.newProjectData() is setting fields of Bundler.
94+ // 2. mockAddToBundler needs to get the project ID of the bundler. To do that we need
95+ // different address for each bundler.
96+ return & bundler.Bundler {}
9697}
9798
9899func mockAddToBundler (bndler * bundler.Bundler , item interface {}, _ int ) error {
99- projID := bndlerProjMap [bndler ]
100+ // Get the project ID of the bndler by inspecting projDataMap.
101+ var projID string
102+ projIDfound := false
103+ for tempProjID , pd := range projDataMap {
104+ if pd .bndler == bndler {
105+ projID = tempProjID
106+ projIDfound = true
107+ break
108+ }
109+ }
110+ if ! projIDfound {
111+ return unrecognizedDataError
112+ }
113+
100114 rds , ok := projRds [projID ]
101115 if ! ok {
102116 // For new project ID, create the actual slice and save its pointer.
@@ -108,6 +122,41 @@ func mockAddToBundler(bndler *bundler.Bundler, item interface{}, _ int) error {
108122 return nil
109123}
110124
125+ // newTest*() functions create exporters and project data used for testing. Each test should call
126+ // One of these functions once and only once, and never call NewExporter() directly.
127+
128+ // newTestExp creates an exporter which saves error to errStorage. Caller should not set
129+ // opts.OnError.
130+ func newTestExp (t * testing.T , opts * Options ) * Exporter {
131+ opts .OnError = testOnError
132+ exp , err := NewExporter (ctx , opts )
133+ if err != nil {
134+ t .Fatalf ("creating exporter failed: %v" , err )
135+ }
136+ // Expose projDataMap so that mockAddToBundler() can use it.
137+ projDataMap = exp .projDataMap
138+ return exp
139+ }
140+
141+ // newTestProjData creates a projectData object to test behavior of projectData.uploadRowData. Other
142+ // uses are not recommended. As newTestExp, all errors are saved to errStorage.
143+ func newTestProjData (t * testing.T , opts * Options ) * projectData {
144+ return newTestExp (t , opts ).newProjectData (project1 )
145+ }
146+
147+ // We define a storage for all errors happened in export operation.
148+
149+ type errRowData struct {
150+ err error
151+ rds []* RowData
152+ }
153+
154+ // testOnError records any incoming error and accompanying RowData array. This function is passed to
155+ // the exporter to record errors.
156+ func testOnError (err error , rds ... * RowData ) {
157+ errStorage = append (errStorage , errRowData {err , rds })
158+ }
159+
111160// checkMetricClient checks all recorded requests to the metric client. We only compare int64
112161// values of the time series. To make this work, we assigned different int64 values for all valid
113162// rows in the test.
@@ -137,19 +186,6 @@ func checkMetricClient(t *testing.T, wantReqsValues [][]int64) {
137186 }
138187}
139188
140- // We define a storage for all errors happened in export operation.
141-
142- type errRowData struct {
143- err error
144- rds []* RowData
145- }
146-
147- // testOnError records any incoming error and accompanying RowData array. This function is passed to
148- // the exporter to record errors.
149- func testOnError (err error , rds ... * RowData ) {
150- errStorage = append (errStorage , errRowData {err , rds })
151- }
152-
153189// errRowDataCheck contains data for checking content of error storage.
154190type errRowDataCheck struct {
155191 errPrefix , errSuffix string
@@ -208,17 +244,6 @@ func checkRowData(rd, wantRd *RowData) error {
208244 return nil
209245}
210246
211- // newTestExp creates an exporter which saves error to errStorage. Caller should not set
212- // opts.OnError.
213- func newTestExp (t * testing.T , opts * Options ) * Exporter {
214- opts .OnError = testOnError
215- exp , err := NewExporter (ctx , opts )
216- if err != nil {
217- t .Fatalf ("creating exporter failed: %v" , err )
218- }
219- return exp
220- }
221-
222247// checkProjData checks all data passed to the bundler by bundler.Add().
223248func checkProjData (t * testing.T , wantProjData map [string ][]* RowData ) {
224249 wantProj := map [string ]bool {}
@@ -243,12 +268,6 @@ func checkProjData(t *testing.T, wantProjData map[string][]*RowData) {
243268 }
244269}
245270
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.
248- func newTestProjData (t * testing.T , opts * Options ) * projectData {
249- return newTestExp (t , opts ).newProjectData (project1 )
250- }
251-
252271// checkLabels checks data in labels.
253272func checkLabels (t * testing.T , prefix string , labels , wantLabels map [string ]string ) {
254273 for labelName , value := range labels {
0 commit comments