Skip to content

Commit

Permalink
reduce wait time for network test
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Feb 3, 2023
1 parent b97c90c commit 69d03ad
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 56 deletions.
102 changes: 53 additions & 49 deletions fsadapter/zipfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,60 +145,64 @@ func TestNewZIP(t *testing.T) {
}

func TestCreateConcurrency(t *testing.T) {
// test for GH issue#90 - race condition in ZIP.Create
const (
numRoutines = 16
testContentsSz = 1 * (1 << 20)
)

var buf bytes.Buffer
var wg sync.WaitGroup

zw := zip.NewWriter(&buf)
defer zw.Close()

fsa := NewZIP(zw)
defer fsa.Close()

// prepare workers
readySteadyGo := make(chan struct{})
panicAttacks := make(chan any, numRoutines)

for i := 0; i < numRoutines; i++ {
wg.Add(1)
go func(n int) {
defer func() {
if r := recover(); r != nil {
panicAttacks <- fmt.Sprintf("ZIP.Create race condition in gr %d: %v", n, r)
t.Parallel()
t.Run("issue#90", func(t *testing.T) {
t.Parallel()
// test for GH issue#90 - race condition in ZIP.Create
const (
numRoutines = 16
testContentsSz = 1 * (1 << 20)
)

var buf bytes.Buffer
var wg sync.WaitGroup

zw := zip.NewWriter(&buf)
defer zw.Close()

fsa := NewZIP(zw)
defer fsa.Close()

// prepare workers
readySteadyGo := make(chan struct{})
panicAttacks := make(chan any, numRoutines)

for i := 0; i < numRoutines; i++ {
wg.Add(1)
go func(n int) {
defer func() {
if r := recover(); r != nil {
panicAttacks <- fmt.Sprintf("ZIP.Create race condition in gr %d: %v", n, r)
}
}()

defer wg.Done()
var contents bytes.Buffer
if _, err := io.CopyN(&contents, rand.Reader, testContentsSz); err != nil {
panic(err)
}
}()

defer wg.Done()
var contents bytes.Buffer
if _, err := io.CopyN(&contents, rand.Reader, testContentsSz); err != nil {
panic(err)
}

<-readySteadyGo
fw, err := fsa.Create(fmt.Sprintf("file%d", n))
if err != nil {
panic(err)
}
defer fw.Close()
<-readySteadyGo
fw, err := fsa.Create(fmt.Sprintf("file%d", n))
if err != nil {
panic(err)
}
defer fw.Close()

if _, err := io.Copy(fw, &contents); err != nil {
panic(err)
if _, err := io.Copy(fw, &contents); err != nil {
panic(err)
}
}(i)
}
close(readySteadyGo)
wg.Wait()
close(panicAttacks)
for r := range panicAttacks {
if r != nil {
t.Error(r)
}
}(i)
}
close(readySteadyGo)
wg.Wait()
close(panicAttacks)
for r := range panicAttacks {
if r != nil {
t.Error(r)
}
}
})
}

func TestZIP_normalizePath(t *testing.T) {
Expand Down
9 changes: 6 additions & 3 deletions internal/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,13 @@ func WithRetry(ctx context.Context, l *rate.Limiter, maxAttempts int, fn func()
}

// waitTime returns the amount of time to wait before retrying depending on
// the current attempt. The wait time is calculated as (x+2)^3 seconds, where
// x is the current attempt number. The maximum wait time is capped at 5
// the current attempt. This variable exists to reduce the test time.
var waitTime = cubicWait

// cubicWait is the wait time function. Time is calculated as (x+2)^3 seconds,
// where x is the current attempt number. The maximum wait time is capped at 5
// minutes.
func waitTime(attempt int) time.Duration {
func cubicWait(attempt int) time.Duration {
x := attempt + 2 // this is to ensure that we sleep at least 8 seconds.
delay := time.Duration(x*x*x) * time.Second
if delay > MaxAllowedWaitTime {
Expand Down
10 changes: 6 additions & 4 deletions internal/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,16 @@ func Test_withRetry(t *testing.T) {
}

func Test500ErrorHandling(t *testing.T) {
t.Parallel()
waitTime = func(attempt int) time.Duration { return 50 * time.Millisecond }
defer func() {
waitTime = cubicWait
}()

var codes = []int{500, 502, 503, 504, 598}
for _, code := range codes {
var thisCode = code
// This test is to ensure that we handle 500 errors correctly.
t.Run(fmt.Sprintf("%d error", code), func(t *testing.T) {
t.Parallel()

const (
testRetryCount = 1
Expand Down Expand Up @@ -227,7 +229,7 @@ func Test500ErrorHandling(t *testing.T) {
})
}

func Test_waitTime(t *testing.T) {
func Test_cubicWait(t *testing.T) {
type args struct {
attempt int
}
Expand All @@ -245,7 +247,7 @@ func Test_waitTime(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := waitTime(tt.args.attempt); !reflect.DeepEqual(got, tt.want) {
if got := cubicWait(tt.args.attempt); !reflect.DeepEqual(got, tt.want) {
t.Errorf("waitTime() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 69d03ad

Please sign in to comment.