Skip to content

Commit

Permalink
sdk/metric: Add unit tests for Shutdown WithTimeout (#4379)
Browse files Browse the repository at this point in the history
* sdk/metric: Improve WithTimeout doc

* Add tests
  • Loading branch information
pellared authored Aug 2, 2023
1 parent 378e51e commit c4d2d7c
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions sdk/metric/periodic_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,52 @@ func TestPeriodicReaderFlushesPending(t *testing.T) {
_ = r.Shutdown(context.Background())
})

t.Run("ForceFlush timeout on producer", func(t *testing.T) {
exp, called := expFunc(t)
timeout := time.Millisecond
r := NewPeriodicReader(exp, WithTimeout(timeout))
r.register(testSDKProducer{
produceFunc: func(ctx context.Context, rm *metricdata.ResourceMetrics) error {
select {
case <-time.After(timeout + time.Second):
*rm = testResourceMetricsA
case <-ctx.Done():
// we timed out before we could collect metrics
return ctx.Err()
}
return nil
}})
r.RegisterProducer(testExternalProducer{})
assert.Equal(t, context.DeadlineExceeded, r.ForceFlush(context.Background()), "timeout error not returned")
assert.False(t, *called, "exporter Export method called when it should have failed before export")

// Ensure Reader is allowed clean up attempt.
_ = r.Shutdown(context.Background())
})

t.Run("ForceFlush timeout on external producer", func(t *testing.T) {
exp, called := expFunc(t)
timeout := time.Millisecond
r := NewPeriodicReader(exp, WithTimeout(timeout))
r.register(testSDKProducer{})
r.RegisterProducer(testExternalProducer{
produceFunc: func(ctx context.Context) ([]metricdata.ScopeMetrics, error) {
select {
case <-time.After(timeout + time.Second):
case <-ctx.Done():
// we timed out before we could collect metrics
return nil, ctx.Err()
}
return []metricdata.ScopeMetrics{testScopeMetricsA}, nil
},
})
assert.Equal(t, context.DeadlineExceeded, r.ForceFlush(context.Background()), "timeout error not returned")
assert.False(t, *called, "exporter Export method called when it should have failed before export")

// Ensure Reader is allowed clean up attempt.
_ = r.Shutdown(context.Background())
})

t.Run("Shutdown", func(t *testing.T) {
exp, called := expFunc(t)
r := NewPeriodicReader(exp)
Expand Down

0 comments on commit c4d2d7c

Please sign in to comment.