Skip to content

Commit

Permalink
Fixed flakiness.
Browse files Browse the repository at this point in the history
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
  • Loading branch information
bwplotka committed Jan 22, 2021
1 parent c03359b commit e0ea420
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions interceptors/logging/payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"sort"
"testing"
"time"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -154,7 +156,32 @@ func (s *loggingPayloadSuite) TestPingStream_LogsAllRequestsAndResponses() {
}
require.NoError(s.T(), stream.CloseSend(), "no error on send stream")

lines := s.logger.o.Lines()
require.Len(s.T(), lines, 4*messagesExpected)
s.assertPayloadLogLinesForMessage(lines, "PingStream", interceptors.BidiStream)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
require.NoError(s.T(), waitUntil(200*time.Millisecond, ctx.Done(), func() error {
got := len(s.logger.o.Lines())
if got >= 4*messagesExpected {
return nil
}
return errors.Errorf("not enough log lines, waiting; got: %v", got)
}))
s.assertPayloadLogLinesForMessage(s.logger.o.Lines(), "PingStream", interceptors.BidiStream)
}

// waitUntil executes f every interval seconds until timeout or no error is returned from f.
func waitUntil(interval time.Duration, stopc <-chan struct{}, f func() error) error {
tick := time.NewTicker(interval)
defer tick.Stop()

var err error
for {
if err = f(); err == nil {
return nil
}
select {
case <-stopc:
return err
case <-tick.C:
}
}
}

0 comments on commit e0ea420

Please sign in to comment.