Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky global ErrorHandler delegation test #1829

Merged
merged 4 commits into from
Apr 21, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 9 additions & 55 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ package otel
import (
"bytes"
"errors"
"fmt"
"log"
"testing"
"time"

"github.com/stretchr/testify/suite"
)
Expand Down Expand Up @@ -72,70 +70,26 @@ func (s *HandlerTestSuite) TestGlobalHandler() {
}

func (s *HandlerTestSuite) TestNoDropsOnDelegate() {
// max time to wait for goroutine to Handle an error.
pause := 10 * time.Millisecond

var sent int
err := errors.New("")
stop := make(chan struct{})
beat := make(chan struct{})
done := make(chan struct{})

// Wait for a error to be submitted from the following goroutine.
wait := func(d time.Duration) error {
timer := time.NewTimer(d)
select {
case <-timer.C:
// We are about to fail, stop the spawned goroutine.
stop <- struct{}{}
return fmt.Errorf("no errors sent in %v", d)
case <-beat:
// Allow the timer to be reclaimed by GC.
timer.Stop()
return nil
}
}

go func() {
// Slow down to speed up: do not overload the processor.
ticker := time.NewTicker(100 * time.Microsecond)
for {
select {
case <-stop:
ticker.Stop()
done <- struct{}{}
return
case <-ticker.C:
sent++
Handle(err)
}

select {
case beat <- struct{}{}:
default:
}
causeErr := func() func() {
err := errors.New("")
return func() {
Handle(err)
}
}()

// Wait for the spice to flow
s.Require().NoError(wait(pause), "starting error stream")
causeErr()
s.Require().Len(s.errLogger.Got(), 1)

// Change to another Handler. We are testing this is loss-less.
newErrLogger := new(errLogger)
secondary := &loggingErrorHandler{
l: log.New(newErrLogger, "", 0),
}
SetErrorHandler(secondary)
s.Require().NoError(wait(pause), "switched to new Handler")

// Testing done, stop sending errors.
stop <- struct{}{}
// Ensure we do not lose any straglers.
<-done

got := append(s.errLogger.Got(), newErrLogger.Got()...)
s.Assert().Greater(len(got), 1, "at least 2 errors should have been sent")
s.Assert().Len(got, sent)
causeErr()
s.Assert().Len(s.errLogger.Got(), 1, "original Handler used after delegation")
s.Assert().Len(newErrLogger.Got(), 1, "new Handler not used after delegation")
}

func TestHandlerTestSuite(t *testing.T) {
Expand Down