mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 22:17:14 +00:00
Rename NewGoRoutine to Run in GoRoutineMap
This commit is contained in:
parent
24c46acd16
commit
f7196337c7
@ -30,13 +30,13 @@ import (
|
|||||||
|
|
||||||
// GoRoutineMap defines the supported set of operations.
|
// GoRoutineMap defines the supported set of operations.
|
||||||
type GoRoutineMap interface {
|
type GoRoutineMap interface {
|
||||||
// NewGoRoutine adds operationName to the list of running operations and
|
// Run adds operationName to the list of running operations and spawns a new
|
||||||
// spawns a new go routine to execute the operation. If an operation with
|
// go routine to execute the operation. If an operation with the same name
|
||||||
// the same name already exists, an error is returned. Once the operation
|
// already exists, an error is returned. Once the operation is complete, the
|
||||||
// is complete, the go routine is terminated and the operationName is
|
// go routine is terminated and the operationName is removed from the list
|
||||||
// removed from the list of executing operations allowing a new operation
|
// of executing operations allowing a new operation to be started with the
|
||||||
// to be started with the same name without error.
|
// same name without error.
|
||||||
NewGoRoutine(operationName string, operation func() error) error
|
Run(operationName string, operation func() error) error
|
||||||
|
|
||||||
// Wait blocks until all operations are completed. This is typically
|
// Wait blocks until all operations are completed. This is typically
|
||||||
// necessary during tests - the test should wait until all operations finish
|
// necessary during tests - the test should wait until all operations finish
|
||||||
@ -57,7 +57,7 @@ type goRoutineMap struct {
|
|||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
func (grm *goRoutineMap) NewGoRoutine(operationName string, operation func() error) error {
|
func (grm *goRoutineMap) Run(operationName string, operation func() error) error {
|
||||||
grm.Lock()
|
grm.Lock()
|
||||||
defer grm.Unlock()
|
defer grm.Unlock()
|
||||||
if grm.operations[operationName] {
|
if grm.operations[operationName] {
|
||||||
|
@ -31,7 +31,7 @@ func Test_NewGoRoutineMap_Positive_SingleOp(t *testing.T) {
|
|||||||
operation := func() error { return nil }
|
operation := func() error { return nil }
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
err := grm.NewGoRoutine(operationName, operation)
|
err := grm.Run(operationName, operation)
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -45,7 +45,7 @@ func Test_NewGoRoutineMap_Positive_SecondOpAfterFirstCompletes(t *testing.T) {
|
|||||||
operationName := "operation-name"
|
operationName := "operation-name"
|
||||||
operation1DoneCh := make(chan interface{}, 0 /* bufferSize */)
|
operation1DoneCh := make(chan interface{}, 0 /* bufferSize */)
|
||||||
operation1 := generateCallbackFunc(operation1DoneCh)
|
operation1 := generateCallbackFunc(operation1DoneCh)
|
||||||
err1 := grm.NewGoRoutine(operationName, operation1)
|
err1 := grm.Run(operationName, operation1)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
t.Fatalf("NewGoRoutine failed. Expected: <no error> Actual: <%v>", err1)
|
t.Fatalf("NewGoRoutine failed. Expected: <no error> Actual: <%v>", err1)
|
||||||
}
|
}
|
||||||
@ -56,7 +56,7 @@ func Test_NewGoRoutineMap_Positive_SecondOpAfterFirstCompletes(t *testing.T) {
|
|||||||
err2 := retryWithExponentialBackOff(
|
err2 := retryWithExponentialBackOff(
|
||||||
time.Duration(20*time.Millisecond),
|
time.Duration(20*time.Millisecond),
|
||||||
func() (bool, error) {
|
func() (bool, error) {
|
||||||
err := grm.NewGoRoutine(operationName, operation2)
|
err := grm.Run(operationName, operation2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf("Warning: NewGoRoutine failed. Expected: <no error> Actual: <%v>. Will retry.", err)
|
t.Logf("Warning: NewGoRoutine failed. Expected: <no error> Actual: <%v>. Will retry.", err)
|
||||||
return false, nil
|
return false, nil
|
||||||
@ -76,7 +76,7 @@ func Test_NewGoRoutineMap_Positive_SecondOpAfterFirstPanics(t *testing.T) {
|
|||||||
grm := NewGoRoutineMap()
|
grm := NewGoRoutineMap()
|
||||||
operationName := "operation-name"
|
operationName := "operation-name"
|
||||||
operation1 := generatePanicFunc()
|
operation1 := generatePanicFunc()
|
||||||
err1 := grm.NewGoRoutine(operationName, operation1)
|
err1 := grm.Run(operationName, operation1)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
t.Fatalf("NewGoRoutine failed. Expected: <no error> Actual: <%v>", err1)
|
t.Fatalf("NewGoRoutine failed. Expected: <no error> Actual: <%v>", err1)
|
||||||
}
|
}
|
||||||
@ -86,7 +86,7 @@ func Test_NewGoRoutineMap_Positive_SecondOpAfterFirstPanics(t *testing.T) {
|
|||||||
err2 := retryWithExponentialBackOff(
|
err2 := retryWithExponentialBackOff(
|
||||||
time.Duration(20*time.Millisecond),
|
time.Duration(20*time.Millisecond),
|
||||||
func() (bool, error) {
|
func() (bool, error) {
|
||||||
err := grm.NewGoRoutine(operationName, operation2)
|
err := grm.Run(operationName, operation2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf("Warning: NewGoRoutine failed. Expected: <no error> Actual: <%v>. Will retry.", err)
|
t.Logf("Warning: NewGoRoutine failed. Expected: <no error> Actual: <%v>. Will retry.", err)
|
||||||
return false, nil
|
return false, nil
|
||||||
@ -107,14 +107,14 @@ func Test_NewGoRoutineMap_Negative_SecondOpBeforeFirstCompletes(t *testing.T) {
|
|||||||
operationName := "operation-name"
|
operationName := "operation-name"
|
||||||
operation1DoneCh := make(chan interface{}, 0 /* bufferSize */)
|
operation1DoneCh := make(chan interface{}, 0 /* bufferSize */)
|
||||||
operation1 := generateWaitFunc(operation1DoneCh)
|
operation1 := generateWaitFunc(operation1DoneCh)
|
||||||
err1 := grm.NewGoRoutine(operationName, operation1)
|
err1 := grm.Run(operationName, operation1)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
t.Fatalf("NewGoRoutine failed. Expected: <no error> Actual: <%v>", err1)
|
t.Fatalf("NewGoRoutine failed. Expected: <no error> Actual: <%v>", err1)
|
||||||
}
|
}
|
||||||
operation2 := generateNoopFunc()
|
operation2 := generateNoopFunc()
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
err2 := grm.NewGoRoutine(operationName, operation2)
|
err2 := grm.Run(operationName, operation2)
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
if err2 == nil {
|
if err2 == nil {
|
||||||
@ -128,7 +128,7 @@ func Test_NewGoRoutineMap_Positive_ThirdOpAfterFirstCompletes(t *testing.T) {
|
|||||||
operationName := "operation-name"
|
operationName := "operation-name"
|
||||||
operation1DoneCh := make(chan interface{}, 0 /* bufferSize */)
|
operation1DoneCh := make(chan interface{}, 0 /* bufferSize */)
|
||||||
operation1 := generateWaitFunc(operation1DoneCh)
|
operation1 := generateWaitFunc(operation1DoneCh)
|
||||||
err1 := grm.NewGoRoutine(operationName, operation1)
|
err1 := grm.Run(operationName, operation1)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
t.Fatalf("NewGoRoutine failed. Expected: <no error> Actual: <%v>", err1)
|
t.Fatalf("NewGoRoutine failed. Expected: <no error> Actual: <%v>", err1)
|
||||||
}
|
}
|
||||||
@ -136,7 +136,7 @@ func Test_NewGoRoutineMap_Positive_ThirdOpAfterFirstCompletes(t *testing.T) {
|
|||||||
operation3 := generateNoopFunc()
|
operation3 := generateNoopFunc()
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
err2 := grm.NewGoRoutine(operationName, operation2)
|
err2 := grm.Run(operationName, operation2)
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
if err2 == nil {
|
if err2 == nil {
|
||||||
@ -148,7 +148,7 @@ func Test_NewGoRoutineMap_Positive_ThirdOpAfterFirstCompletes(t *testing.T) {
|
|||||||
err3 := retryWithExponentialBackOff(
|
err3 := retryWithExponentialBackOff(
|
||||||
time.Duration(20*time.Millisecond),
|
time.Duration(20*time.Millisecond),
|
||||||
func() (bool, error) {
|
func() (bool, error) {
|
||||||
err := grm.NewGoRoutine(operationName, operation3)
|
err := grm.Run(operationName, operation3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf("Warning: NewGoRoutine failed. Expected: <no error> Actual: <%v>. Will retry.", err)
|
t.Logf("Warning: NewGoRoutine failed. Expected: <no error> Actual: <%v>. Will retry.", err)
|
||||||
return false, nil
|
return false, nil
|
||||||
@ -224,7 +224,7 @@ func Test_NewGoRoutineMap_Positive_Wait(t *testing.T) {
|
|||||||
operationName := "operation-name"
|
operationName := "operation-name"
|
||||||
operation1DoneCh := make(chan interface{}, 0 /* bufferSize */)
|
operation1DoneCh := make(chan interface{}, 0 /* bufferSize */)
|
||||||
operation1 := generateWaitFunc(operation1DoneCh)
|
operation1 := generateWaitFunc(operation1DoneCh)
|
||||||
err := grm.NewGoRoutine(operationName, operation1)
|
err := grm.Run(operationName, operation1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("NewGoRoutine failed. Expected: <no error> Actual: <%v>", err)
|
t.Fatalf("NewGoRoutine failed. Expected: <no error> Actual: <%v>", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user