@@ -35,8 +35,8 @@ import (
35
35
var (
36
36
// errStorage records all errors and associated RowData objects reported by exporter.
37
37
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
40
40
// projRds saves all RowData objects passed to addToBundler call by project ID. Since a
41
41
// value of a map is not addressable, we save the pointer to the slice.
42
42
projRds map [string ]* []* RowData
@@ -63,7 +63,7 @@ func init() {
63
63
// testDataInit() initializes all data needed for each test. This function must be called at the
64
64
// beginning of each test.
65
65
func testDataInit () {
66
- bndlerProjMap = map [ * bundler. Bundler ] string {}
66
+ projDataMap = nil
67
67
projRds = map [string ]* []* RowData {}
68
68
timeSeriesReqs = nil
69
69
timeSeriesResults = nil
@@ -88,15 +88,29 @@ func mockCreateTimeSeries(_ *monitoring.MetricClient, _ context.Context, req *mp
88
88
return err
89
89
}
90
90
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 {}
96
97
}
97
98
98
99
func 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
+
100
114
rds , ok := projRds [projID ]
101
115
if ! ok {
102
116
// 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 {
108
122
return nil
109
123
}
110
124
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
+
111
160
// checkMetricClient checks all recorded requests to the metric client. We only compare int64
112
161
// values of the time series. To make this work, we assigned different int64 values for all valid
113
162
// rows in the test.
@@ -137,19 +186,6 @@ func checkMetricClient(t *testing.T, wantReqsValues [][]int64) {
137
186
}
138
187
}
139
188
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
-
153
189
// errRowDataCheck contains data for checking content of error storage.
154
190
type errRowDataCheck struct {
155
191
errPrefix , errSuffix string
@@ -208,17 +244,6 @@ func checkRowData(rd, wantRd *RowData) error {
208
244
return nil
209
245
}
210
246
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
-
222
247
// checkProjData checks all data passed to the bundler by bundler.Add().
223
248
func checkProjData (t * testing.T , wantProjData map [string ][]* RowData ) {
224
249
wantProj := map [string ]bool {}
@@ -243,12 +268,6 @@ func checkProjData(t *testing.T, wantProjData map[string][]*RowData) {
243
268
}
244
269
}
245
270
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
-
252
271
// checkLabels checks data in labels.
253
272
func checkLabels (t * testing.T , prefix string , labels , wantLabels map [string ]string ) {
254
273
for labelName , value := range labels {
0 commit comments