@@ -47,6 +47,21 @@ func TestInitResourcesForBatch(t *testing.T) {
4747 assert .Error (t , err , "failed to retrieve resources: invalid resource query" )
4848 m .AssertExpectations (t )
4949 })
50+ t .Run ("does not panic when all resource configs return an empty list" , func (t * testing.T ) {
51+ client := NewMockBatchClient (logger )
52+ client .Config = resourceQueryConfig
53+ m := & MockService {}
54+ // empty list + nil error: the "no resources of this type exist" path
55+ m .On ("GetResourceDefinitions" , mock .Anything , mock .Anything , mock .Anything , mock .Anything ).Return ([]* armresources.GenericResourceExpanded {}, nil )
56+ client .AzureMonitorService = m
57+
58+ err := client .InitResources (mockConcurrentMapResourceMetrics )
59+ require .NoError (t , err )
60+
61+ // The closer goroutine launched by InitResources runs asynchronously and
62+ // will panic on close(nil) if the channels were never initialized.
63+ time .Sleep (100 * time .Millisecond )
64+ })
5065}
5166
5267func TestGetMetricsInBatch (t * testing.T ) {
@@ -99,7 +114,7 @@ func TestGetMetricsInBatch(t *testing.T) {
99114 mr := MockReporterV2 {}
100115 mr .On ("Error" , mock .Anything ).Return (true )
101116 results := client .GetMetricsInBatch (groupedMetrics , referenceTime , & mr )
102- assert .Equal (t , len ( results ), 0 )
117+ assert .Empty (t , results , "expected no metric values when QueryResources returns an error" )
103118 m .AssertExpectations (t )
104119 })
105120
@@ -169,12 +184,12 @@ func TestGetMetricsInBatch(t *testing.T) {
169184 mr := MockReporterV2 {}
170185
171186 metricValues := client .GetMetricsInBatch (groupedMetrics , referenceTime , & mr )
172- require .Equal (t , len ( metricValues ) , 1 )
173- require .Equal (t , len ( metricValues [0 ].Values ) , 1 )
187+ require .Len (t , metricValues , 1 , "expected exactly one metric value group" )
188+ require .Len (t , metricValues [0 ].Values , 1 , "expected exactly one value in the metric group" )
174189
175- assert .Equal (t , * metricValues [0 ].Values [0 ].avg , 1.0 )
176- assert .Equal (t , * metricValues [0 ].Values [0 ].max , 2.0 )
177- assert .Equal (t , * metricValues [0 ].Values [0 ].min , 3.0 )
190+ assert .InDelta (t , 1.0 , * metricValues [0 ].Values [0 ].avg , 0.0001 )
191+ assert .InDelta (t , 2.0 , * metricValues [0 ].Values [0 ].max , 0.0001 )
192+ assert .InDelta (t , 3.0 , * metricValues [0 ].Values [0 ].min , 0.0001 )
178193
179194 m .AssertExpectations (t )
180195 })
0 commit comments