Skip to content

Commit

Permalink
use a timeout based read timeout constraint instead 42 attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Feb 28, 2023
1 parent e557510 commit d2be80f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 0 additions & 2 deletions random/Make_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package random_test

import (
"github.com/adamluzsi/testcase/pp"
"math/rand"
"testing"
"time"
Expand Down Expand Up @@ -290,7 +289,6 @@ func TestSlice_smoke(t *testing.T) {
rnd := random.New(random.CryptoSeed{})
length := rnd.IntB(1, 5)
slice1 := random.Slice[int](length, rnd.Int)
pp.PP(slice1)
it.Must.Equal(length, len(slice1))
it.Must.NotEmpty(slice1)
it.Must.AnyOf(func(a *assert.AnyOf) {
Expand Down
15 changes: 10 additions & 5 deletions random/Random.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,19 @@ func (r *Random) Read(p []byte) (n int, err error) {
}

func (r *Random) mustRead(b []byte) {
var err error
for i := 0; i < 42; i++ {
_, err = r.Read(b)
if err == nil {
deadline := time.Now().Add(5 * time.Minute)
for {
n, err := r.Read(b)
if err != nil {
if time.Now().After(deadline) {
panic(err)
}
continue
}
if n == len(b) {
return
}
}
panic(err)
}

func (r *Random) UUID() string {
Expand Down

0 comments on commit d2be80f

Please sign in to comment.