-
Notifications
You must be signed in to change notification settings - Fork 3
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
7 changed files
with
167 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package fn | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
func Name(v interface{}) (string, error) { | ||
value := reflect.ValueOf(v) | ||
if value.Kind() != reflect.Func { | ||
return "", fmt.Errorf("%T is not func type", v) | ||
} | ||
|
||
name := runtime.FuncForPC(value.Pointer()).Name() | ||
if i := strings.LastIndex(name, "."); i != -1 { | ||
return strings.TrimSuffix(name[i+1:], "-fm"), nil | ||
} | ||
|
||
return "", fmt.Errorf("not found func name") | ||
} |
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,46 @@ | ||
package fn | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type s struct{} | ||
|
||
func f1(int) {} | ||
func f2(...int) {} | ||
func F3(int) {} | ||
func F4(...int) {} | ||
func (s) f1(int) {} | ||
func (s) f2(...int) {} | ||
func (s) F3(int) {} | ||
func (s) F4(...int) {} | ||
|
||
func TestName(t *testing.T) { | ||
type check struct { | ||
input interface{} | ||
expected string | ||
err error | ||
} | ||
|
||
checks := []check{ | ||
{input: f1, expected: "f1", err: nil}, | ||
{input: f2, expected: "f2", err: nil}, | ||
{input: F3, expected: "F3", err: nil}, | ||
{input: F4, expected: "F4", err: nil}, | ||
{input: s{}.f1, expected: "f1", err: nil}, | ||
{input: s{}.f2, expected: "f2", err: nil}, | ||
{input: s{}.F3, expected: "F3", err: nil}, | ||
{input: s{}.F4, expected: "F4", err: nil}, | ||
{input: 1, expected: "", err: fmt.Errorf("int is not func type")}, | ||
{input: true, expected: "", err: fmt.Errorf("bool is not func type")}, | ||
} | ||
|
||
for _, c := range checks { | ||
output, err := Name(c.input) | ||
require.Equal(t, c.expected, output) | ||
require.Equal(t, c.err, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package fn | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
func Timeout(ctx context.Context, fn func() error, timeout time.Duration) error { | ||
errChan := make(chan error) | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
go func() { | ||
errChan <- fn() | ||
close(errChan) | ||
}() | ||
|
||
select { | ||
case err := <-errChan: | ||
return err | ||
case <-ctx.Done(): | ||
return ctx.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package fn | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type stringErr string | ||
|
||
func (e stringErr) Error() string { | ||
return string(e) | ||
} | ||
|
||
func TestTimeout(t *testing.T) { | ||
genFunc := func(d time.Duration, err error) func() error { | ||
return func() error { | ||
time.Sleep(d) | ||
return err | ||
} | ||
} | ||
|
||
type check struct { | ||
input func() error | ||
timeout time.Duration | ||
expected error | ||
} | ||
|
||
checks := []check{ | ||
{ | ||
input: genFunc(time.Millisecond, nil), | ||
timeout: time.Millisecond * 2, | ||
expected: nil, | ||
}, | ||
{ | ||
input: genFunc(time.Millisecond, stringErr("func err")), | ||
timeout: time.Millisecond * 2, | ||
expected: stringErr("func err"), | ||
}, | ||
{ | ||
input: genFunc(time.Millisecond*2, stringErr("func err")), | ||
timeout: time.Millisecond, | ||
expected: context.DeadlineExceeded, | ||
}, | ||
} | ||
|
||
for i, c := range checks { | ||
err := Timeout(context.Background(), c.input, c.timeout) | ||
require.Equal(t, c.expected, err, "#%d check", i) | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
go func() { | ||
time.Sleep(time.Millisecond * 50) | ||
cancel() | ||
}() | ||
require.Equal(t, context.Canceled, Timeout(ctx, genFunc(time.Minute, nil), time.Hour)) | ||
} |
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