mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-02-15 13:53:24 +02:00
Update global handler_test.go
On slower VMs (like the CI VMs), this test was timing out in 2ms and failing. Additionally, in the process of failing, the suite tear-down function would reset the globalHandler and cause a race with the spawned goroutine that was abandoned. This increases the pause from 2ms to 10ms, unifies and simplifies the wait logic, and stops the child goroutine on failure.
This commit is contained in:
parent
a98bb979df
commit
388dbc785f
@ -17,6 +17,7 @@ package global
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -71,12 +72,29 @@ func (s *HandlerTestSuite) TestGlobalHandler() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *HandlerTestSuite) TestNoDropsOnDelegate() {
|
func (s *HandlerTestSuite) TestNoDropsOnDelegate() {
|
||||||
|
// max time to wait for goroutine to Handle an error.
|
||||||
|
pause := 10 * time.Millisecond
|
||||||
|
|
||||||
var sent int
|
var sent int
|
||||||
err := errors.New("")
|
err := errors.New("")
|
||||||
stop := make(chan struct{})
|
stop := make(chan struct{})
|
||||||
beat := make(chan struct{})
|
beat := make(chan struct{})
|
||||||
done := 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:
|
||||||
|
timer.Stop()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -96,11 +114,7 @@ func (s *HandlerTestSuite) TestNoDropsOnDelegate() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Wait for the spice to flow
|
// Wait for the spice to flow
|
||||||
select {
|
s.Require().NoError(wait(pause), "starting error stream")
|
||||||
case <-time.Tick(2 * time.Millisecond):
|
|
||||||
s.T().Fatal("no errors were sent in 2ms")
|
|
||||||
case <-beat:
|
|
||||||
}
|
|
||||||
|
|
||||||
// Change to another Handler. We are testing this is loss-less.
|
// Change to another Handler. We are testing this is loss-less.
|
||||||
newErrLogger := new(errLogger)
|
newErrLogger := new(errLogger)
|
||||||
@ -108,21 +122,12 @@ func (s *HandlerTestSuite) TestNoDropsOnDelegate() {
|
|||||||
l: log.New(newErrLogger, "", 0),
|
l: log.New(newErrLogger, "", 0),
|
||||||
}
|
}
|
||||||
SetHandler(secondary)
|
SetHandler(secondary)
|
||||||
|
// Flush so we can ensure new errors are sent to new Handler.
|
||||||
select {
|
s.Require().NoError(wait(pause), "flushing error stream")
|
||||||
case <-time.Tick(2 * time.Millisecond):
|
|
||||||
s.T().Fatal("no errors were sent within 2ms after SetHandler")
|
|
||||||
case <-beat:
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now beat is clear, wait for a fresh send.
|
// Now beat is clear, wait for a fresh send.
|
||||||
select {
|
s.Require().NoError(wait(pause), "getting fresh errors to new Handler")
|
||||||
case <-time.Tick(2 * time.Millisecond):
|
|
||||||
s.T().Fatal("no fresh errors were sent within 2ms after SetHandler")
|
|
||||||
case <-beat:
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop sending errors.
|
// Testing done, stop sending errors.
|
||||||
stop <- struct{}{}
|
stop <- struct{}{}
|
||||||
// Ensure we do not lose any straglers.
|
// Ensure we do not lose any straglers.
|
||||||
<-done
|
<-done
|
||||||
|
Loading…
x
Reference in New Issue
Block a user