@@ -362,9 +362,45 @@ func TestWorkerStarttime(t *testing.T) {
362
362
e .Unlock ()
363
363
}
364
364
365
+ func TestUnregisterReportsUsage (t * testing.T ) {
366
+ restart ()
367
+ ctx := context .Background ()
368
+
369
+ m1 := stats .Int64 ("measure" , "desc" , "unit" )
370
+ view1 := & View {Name : "count" , Measure : m1 , Aggregation : Count ()}
371
+ m2 := stats .Int64 ("measure2" , "desc" , "unit" )
372
+ view2 := & View {Name : "count2" , Measure : m2 , Aggregation : Count ()}
373
+
374
+ SetReportingPeriod (time .Hour )
375
+
376
+ if err := Register (view1 , view2 ); err != nil {
377
+ t .Fatalf ("cannot register: %v" , err )
378
+ }
379
+
380
+ e := & countExporter {}
381
+ RegisterExporter (e )
382
+
383
+ stats .Record (ctx , m1 .M (1 ))
384
+ stats .Record (ctx , m2 .M (1 ))
385
+ stats .Record (ctx , m2 .M (1 ))
386
+
387
+ Unregister (view2 )
388
+
389
+ // Unregister should only flush view2, so expect the count of 2.
390
+ want := int64 (2 )
391
+
392
+ e .Lock ()
393
+ got := e .totalCount
394
+ e .Unlock ()
395
+ if got != want {
396
+ t .Errorf ("got count data = %v; want %v" , got , want )
397
+ }
398
+ }
399
+
365
400
type countExporter struct {
366
401
sync.Mutex
367
- count int64
402
+ count int64
403
+ totalCount int64
368
404
}
369
405
370
406
func (e * countExporter ) ExportView (vd * Data ) {
@@ -376,6 +412,7 @@ func (e *countExporter) ExportView(vd *Data) {
376
412
e .Lock ()
377
413
defer e .Unlock ()
378
414
e .count = d .Value
415
+ e .totalCount += d .Value
379
416
}
380
417
381
418
type vdExporter struct {
0 commit comments