forked from AliyunContainerService/terway
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: optimize the backoff for wait eni
Signed-off-by: l1b0k <[email protected]>
- Loading branch information
Showing
9 changed files
with
174 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package backoff | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBackoffManagerNextWithEmptySequence(t *testing.T) { | ||
b := NewBackoffManager([]time.Duration{}, nil, nil) | ||
_, err := b.Next(context.Background()) | ||
if err == nil || err.Error() != "timeout" { | ||
t.Errorf("expected timeout error, got %v", err) | ||
} | ||
} | ||
|
||
func TestBackoffManagerNextWithCancelledContext(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
b := NewBackoffManager([]time.Duration{time.Second}, nil, nil) | ||
_, err := b.Next(ctx) | ||
if err == nil || !errors.Is(err, context.Canceled) { | ||
t.Errorf("expected context canceled error, got %v", err) | ||
} | ||
} | ||
|
||
func TestBackoffManagerNextWithConditionFuncError(t *testing.T) { | ||
b := NewBackoffManager([]time.Duration{time.Second}, func(ctx context.Context) (bool, bool, error) { | ||
return false, false, fmt.Errorf("condition error") | ||
}, nil) | ||
_, err := b.Next(context.Background()) | ||
if err == nil || err.Error() != "condition error" { | ||
t.Errorf("expected condition error, got %v", err) | ||
} | ||
} | ||
|
||
func TestBackoffManagerNextWithConditionFuncDone(t *testing.T) { | ||
b := NewBackoffManager([]time.Duration{time.Second}, func(ctx context.Context) (bool, bool, error) { | ||
return true, false, nil | ||
}, nil) | ||
_, err := b.Next(context.Background()) | ||
if err != nil { | ||
t.Errorf("expected no error, got %v", err) | ||
} | ||
} | ||
|
||
func TestBackoffManagerNextWithConditionFuncThrottle(t *testing.T) { | ||
b := NewBackoffManager([]time.Duration{time.Second, 2 * time.Second}, func(ctx context.Context) (bool, bool, error) { | ||
return false, true, nil | ||
}, func(now time.Duration) time.Duration { | ||
return 3 * time.Second | ||
}) | ||
b.Next(context.Background()) | ||
if b.sequence[0] != 3*time.Second { | ||
t.Errorf("expected first sequence to be 3s, got %v", b.sequence[0]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.