-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SignalError fixes, ContextHandler (#25)
* Signal errors, context handler * go1.20 * Update GitHub Action
- Loading branch information
1 parent
c709688
commit daf88ed
Showing
5 changed files
with
199 additions
and
48 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
name: test | ||
on: | ||
pull_request: | ||
types: [opened, synchronize] | ||
push: | ||
branches: [main] | ||
schedule: | ||
- cron: "0 12 1 * *" # first day of the month at 12:00 | ||
|
||
jobs: | ||
test: | ||
strategy: | ||
matrix: | ||
platform: [ubuntu-latest, macos-latest, windows-latest] | ||
|
||
runs-on: ${{ matrix.platform }} | ||
|
||
defaults: | ||
run: | ||
shell: bash | ||
|
||
steps: | ||
- name: Install Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.20.x | ||
|
||
- name: Check out repo | ||
uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.event.pull_request.head.sha }} | ||
|
||
- name: Prepare cache | ||
id: cache | ||
run: | | ||
echo "GOCACHE=$(go env GOCACHE)" >> $GITHUB_OUTPUT | ||
echo "GOMODCACHE=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT | ||
echo "GOVERSION=$(go env GOVERSION)" >> $GITHUB_OUTPUT | ||
mkdir -p $(go env GOCACHE) || true | ||
mkdir -p $(go env GOMODCACHE) || true | ||
- name: Cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: | | ||
${{ steps.cache.outputs.GOCACHE }} | ||
${{ steps.cache.outputs.GOMODCACHE }} | ||
key: test.1-${{ runner.os }}-${{ steps.cache.outputs.GOVERSION }}-${{ hashFiles('**/go.mod') }} | ||
restore-keys: | | ||
test.1-${{ runner.os }}-${{ steps.cache.outputs.GOVERSION }}-${{ hashFiles('**/go.mod') }} | ||
test.1-${{ runner.os }}-${{ steps.cache.outputs.GOVERSION }}- | ||
test.1-${{ runner.os }}- | ||
- name: Install tools | ||
run: | | ||
go install honnef.co/go/tools/cmd/staticcheck@latest | ||
go install mvdan.cc/gofumpt@latest | ||
go install github.com/mgechev/revive@latest | ||
- name: Run gofmt | ||
if: matrix.platform != 'windows-latest' # :< | ||
run: diff <(gofmt -d . 2>/dev/null) <(printf '') | ||
|
||
- name: Run go vet | ||
run: go vet ./... | ||
|
||
- name: Run staticcheck | ||
run: staticcheck ./... | ||
|
||
- name: Run gofumpt | ||
run: gofumpt -d -e -l . | ||
|
||
- name: Run go test | ||
run: go test -v -race ./... |
This file was deleted.
Oops, something went wrong.
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 run | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestContextHandler(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
var rg Group | ||
rg.Add(ContextHandler(ctx)) | ||
errc := make(chan error, 1) | ||
go func() { errc <- rg.Run() }() | ||
cancel() | ||
select { | ||
case err := <-errc: | ||
if want, have := context.Canceled, err; !errors.Is(have, want) { | ||
t.Errorf("error: want %v, have %v", want, have) | ||
} | ||
case <-time.After(time.Second): | ||
t.Errorf("timeout waiting for error after cancel") | ||
} | ||
} | ||
|
||
func TestSignalError(t *testing.T) { | ||
testc := make(chan os.Signal, 1) | ||
ctx := putTestSigChan(context.Background(), testc) | ||
|
||
var rg Group | ||
rg.Add(SignalHandler(ctx, os.Interrupt)) | ||
testc <- os.Interrupt | ||
err := rg.Run() | ||
|
||
var sigerr *SignalError | ||
if want, have := true, errors.As(err, &sigerr); want != have { | ||
t.Errorf("errors.As(err, &sigerr): want %v, have %v", want, have) | ||
} | ||
|
||
if sigerr != nil { | ||
if want, have := os.Interrupt, sigerr.Signal; want != have { | ||
t.Errorf("sigerr.Signal: want %v, have %v", want, have) | ||
} | ||
} | ||
|
||
if sigerr := &(SignalError{}); !errors.As(err, &sigerr) { | ||
t.Errorf("errors.As(err, <inline sigerr>): failed") | ||
} | ||
|
||
if want, have := true, errors.As(err, &(SignalError{})); want != have { | ||
t.Errorf("errors.As(err, &(SignalError{})): want %v, have %v", want, have) | ||
} | ||
|
||
if want, have := true, errors.Is(err, ErrSignal); want != have { | ||
t.Errorf("errors.Is(err, ErrSignal): want %v, have %v", want, have) | ||
} | ||
} |
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,3 @@ | ||
module github.com/oklog/run | ||
|
||
go 1.13 | ||
go 1.20 |