Skip to content

Commit 7fe6a48

Browse files
jhjangjjhwan-h
authored andcommitted
refactor: retryLoop with atomic counter and drained channel
1 parent ae4b2e9 commit 7fe6a48

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

out.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

runner/options.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,6 @@ func (options *Options) ValidateOptions() error {
765765
return errors.New(fmt.Sprintf("invalid retry-delay: must be >0 when retry-rounds=%d (got %d)", options.RetryRounds, options.RetryDelay))
766766
}
767767

768-
if options.RetryRounds > 0 && options.RetryDelay <= 0 {
769-
return errors.New(fmt.Sprintf("invalid retry-delay: must be >0 when retry-rounds=%d (got %d)", options.RetryRounds, options.RetryDelay))
770-
}
771-
772768
return nil
773769
}
774770

runner/runner_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ func TestCreateNetworkpolicyInstance_AllowDenyFlags(t *testing.T) {
321321

322322
func TestRunner_Process_And_RetryLoop(t *testing.T) {
323323
var hits1, hits2 int32
324+
325+
// srv1: returns 429 for the first 3 requests, and 200 on the 4th request
324326
srv1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
325327
if atomic.AddInt32(&hits1, 1) != 4 {
326328
w.WriteHeader(http.StatusTooManyRequests)
@@ -330,6 +332,7 @@ func TestRunner_Process_And_RetryLoop(t *testing.T) {
330332
}))
331333
defer srv1.Close()
332334

335+
// srv2: returns 429 for the first 2 requests, and 200 on the 3rd request
333336
srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
334337
if atomic.AddInt32(&hits2, 1) != 3 {
335338
w.WriteHeader(http.StatusTooManyRequests)
@@ -366,7 +369,7 @@ func TestRunner_Process_And_RetryLoop(t *testing.T) {
366369
var drainWG sync.WaitGroup
367370
drainWG.Add(1)
368371
var s1n429, s1n200, s2n429, s2n200 int
369-
go func(output chan Result) {
372+
go func() {
370373
defer drainWG.Done()
371374
for res := range output {
372375
switch res.StatusCode {
@@ -384,7 +387,7 @@ func TestRunner_Process_And_RetryLoop(t *testing.T) {
384387
}
385388
}
386389
}
387-
}(output)
390+
}()
388391

389392
for _, url := range seed {
390393
r.process(url, wg, r.hp, httpx.HTTP, so, output, retryCh)
@@ -395,8 +398,12 @@ func TestRunner_Process_And_RetryLoop(t *testing.T) {
395398
close(output)
396399
drainWG.Wait()
397400

401+
// Verify expected results
402+
// srv1: should have 3x 429 responses and no 200 (never succeeds within retries)
398403
require.Equal(t, 3, s1n429)
399404
require.Equal(t, 0, s1n200)
405+
406+
// srv2: should have 2x 429 responses and 1x 200 (succeeds on 3rd attempt)
400407
require.Equal(t, 2, s2n429)
401408
require.Equal(t, 1, s2n200)
402409
}

0 commit comments

Comments
 (0)