-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat): Add test coverage for layer.ExponentialBackoffLayerExecutor()
- Loading branch information
Showing
8 changed files
with
223 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
/ebs-bootstrap* | ||
coverage.html | ||
coverage.out | ||
coverage.* |
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,118 @@ | ||
package layer | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/reecetech/ebs-bootstrap/internal/action" | ||
"github.com/reecetech/ebs-bootstrap/internal/config" | ||
"github.com/reecetech/ebs-bootstrap/internal/utils" | ||
) | ||
|
||
const ( | ||
MaxUint32 = ^uint32(0) | ||
) | ||
|
||
type MockLayer struct { | ||
from *utils.MockIncrementError | ||
modify *utils.MockIncrementError | ||
validate *utils.MockIncrementError | ||
} | ||
|
||
func (ml *MockLayer) From(c *config.Config) error { | ||
return ml.from.Trigger() | ||
} | ||
|
||
func (ml *MockLayer) Modify(c *config.Config) ([]action.Action, error) { | ||
err := ml.modify.Trigger() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return []action.Action{}, nil | ||
} | ||
|
||
func (ml *MockLayer) Validate(c *config.Config) error { | ||
return ml.validate.Trigger() | ||
} | ||
|
||
func (ml *MockLayer) Warning() string { | ||
return DisabledWarning | ||
} | ||
|
||
func TestExponentialBackoffLayerExecutor(t *testing.T) { | ||
mae := action.NewDefaultActionExecutor() | ||
// Lets generate ExponentialBackoffParameters with a custom | ||
// InitialInterval. We do not want to slow down the test suite | ||
// with an excessively long initial interval | ||
debp := DefaultExponentialBackoffParameters() | ||
ebp := &ExponentialBackoffParameters{ | ||
InitialInterval: 1 * time.Millisecond, | ||
Multiplier: debp.Multiplier, | ||
MaxRetries: debp.MaxRetries, | ||
} | ||
|
||
subtests := []struct { | ||
Name string | ||
From *utils.MockIncrementError | ||
Modify *utils.MockIncrementError | ||
Validate *utils.MockIncrementError | ||
ExpectedError error | ||
}{ | ||
{ | ||
Name: "Success", | ||
From: utils.NewMockIncrementError("From()", utils.SuccessUntilTrigger, MaxUint32), | ||
Modify: utils.NewMockIncrementError("Modify()", utils.SuccessUntilTrigger, MaxUint32), | ||
Validate: utils.NewMockIncrementError("Validate()", utils.SuccessUntilTrigger, MaxUint32), | ||
ExpectedError: nil, | ||
}, | ||
{ | ||
Name: "From() - Failure on First Call", | ||
From: utils.NewMockIncrementError("From()", utils.SuccessUntilTrigger, 1), | ||
Modify: utils.NewMockIncrementError("Modify()", utils.SuccessUntilTrigger, MaxUint32), | ||
Validate: utils.NewMockIncrementError("Validate()", utils.SuccessUntilTrigger, MaxUint32), | ||
ExpectedError: fmt.Errorf("🔴 From(): Type=SuccessUntilTrigger, Increment=1, Trigger=1"), | ||
}, | ||
{ | ||
Name: "From() - Trigger Permant Backoff Failure", | ||
From: utils.NewMockIncrementError("From()", utils.SuccessUntilTrigger, 2), | ||
Modify: utils.NewMockIncrementError("Modify()", utils.SuccessUntilTrigger, MaxUint32), | ||
Validate: utils.NewMockIncrementError("Validate()", utils.SuccessUntilTrigger, MaxUint32), | ||
ExpectedError: fmt.Errorf("🔴 From(): Type=SuccessUntilTrigger, Increment=2, Trigger=2"), | ||
}, | ||
{ | ||
Name: "Modify() - Failure on First Call", | ||
From: utils.NewMockIncrementError("From()", utils.SuccessUntilTrigger, MaxUint32), | ||
Modify: utils.NewMockIncrementError("Modify()", utils.SuccessUntilTrigger, 1), | ||
Validate: utils.NewMockIncrementError("Validate()", utils.SuccessUntilTrigger, MaxUint32), | ||
ExpectedError: fmt.Errorf("🔴 Modify(): Type=SuccessUntilTrigger, Increment=1, Trigger=1"), | ||
}, | ||
// The number of times Validate() would be the initial call (1) plus the number of allowed retries (ebp.MaxRetries) | ||
{ | ||
Name: "Validate() - Success Just Before Maximum Retries Reached", | ||
From: utils.NewMockIncrementError("From()", utils.SuccessUntilTrigger, MaxUint32), | ||
Modify: utils.NewMockIncrementError("Modify()", utils.SuccessUntilTrigger, MaxUint32), | ||
Validate: utils.NewMockIncrementError("Validate()", utils.ErrorUntilTrigger, 1+ebp.MaxRetries), | ||
ExpectedError: nil, | ||
}, | ||
{ | ||
Name: "Validate() - Trigger Maximum Retries", | ||
From: utils.NewMockIncrementError("From()", utils.SuccessUntilTrigger, MaxUint32), | ||
Modify: utils.NewMockIncrementError("Modify()", utils.SuccessUntilTrigger, MaxUint32), | ||
Validate: utils.NewMockIncrementError("Validate()", utils.ErrorUntilTrigger, 2+ebp.MaxRetries), | ||
ExpectedError: fmt.Errorf("🔴 Validate(): Type=ErrorUntilTrigger, Increment=%d, Trigger=%d", 1+ebp.MaxRetries, 2+ebp.MaxRetries), | ||
}, | ||
} | ||
for _, subtest := range subtests { | ||
t.Run(subtest.Name, func(t *testing.T) { | ||
ml := &MockLayer{ | ||
from: subtest.From, | ||
modify: subtest.Modify, | ||
validate: subtest.Validate, | ||
} | ||
eb := NewExponentialBackoffLayerExecutor(nil, mae, ebp) | ||
err := eb.Execute([]Layer{ml}) | ||
utils.CheckError("eb.Execute()", t, subtest.ExpectedError, err) | ||
}) | ||
} | ||
} |
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