-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
100 additions
and
24 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,38 @@ | ||
package relax | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Routine is a handle to a goroutine's error response | ||
type Routine struct { | ||
errChan chan error | ||
} | ||
|
||
// Wait blocks until the goroutine corresponding to the Routine instance returns an error | ||
func (r *Routine) Wait() error { | ||
return <-r.errChan | ||
} | ||
|
||
// Go launches a goroutine that will return an error if the provided func panics | ||
func Go(f func() error) *Routine { | ||
routine := &Routine{ | ||
errChan: make(chan error, 1), | ||
} | ||
go func() { | ||
// Define a recover func that converts a panic to an error | ||
recoverFunc := func() { | ||
if r := recover(); r != nil { | ||
// Assign the panic content to returned error | ||
routine.errChan <- fmt.Errorf("%w: %v", PanicError, r) | ||
} | ||
} | ||
// Always close | ||
defer close(routine.errChan) | ||
// Handle panics | ||
defer recoverFunc() | ||
// Call the provided func | ||
routine.errChan <- f() | ||
}() | ||
return routine | ||
} |
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,27 @@ | ||
package relax | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRoutine_NilError(t *testing.T) { | ||
r := Go(func() error { | ||
return nil | ||
}) | ||
assert.NoError(t, r.Wait()) | ||
} | ||
|
||
func TestRoutine_Panic_NotNilError(t *testing.T) { | ||
panicMsg := "test panic" | ||
r := Go(func() error { | ||
panic("test panic") | ||
}) | ||
err := r.Wait() | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), panicMsg) | ||
assert.True(t, errors.Is(err, PanicError)) | ||
} |