Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: use golangci-lint for static checks #28

Merged
merged 3 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@ jobs:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Format
if: matrix.go-version >= '1.17'
run: diff -u <(echo -n) <(gofmt -d .)
- name: Run static checks
uses: golangci/golangci-lint-action@v2
with:
version: v1.42.1
args: --config=.golangci.yml --verbose
skip-go-installation: true
skip-pkg-cache: true
skip-build-cache: true
- name: Build
run: go build
- name: Vet
run: go vet
- name: Install and run staticcheck
if: matrix.go-version >= '1.17'
run: |
go install honnef.co/go/tools/cmd/staticcheck@latest
staticcheck -version
staticcheck -- ./...
- name: Run unit tests
run: go test -v -race -cover
54 changes: 54 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# See https://golangci-lint.run/usage/configuration/ for available options.
# Also https://github.com/cilium/cilium/blob/master/.golangci.yaml as a
# reference.
linters:
disable-all: true
enable:
- asciicheck
- deadcode
- dogsled
- durationcheck
- errcheck
- errname
- errorlint
- exhaustive
- exportloopref
- forcetypeassert
- godot
- goerr113
- gofmt
- goimports
- gosec
- gosimple
- govet
- ifshort
- ineffassign
- misspell
- nestif
- nilerr
- prealloc
- predeclared
- revive
- rowserrcheck
- staticcheck
- structcheck
- thelper
- typecheck
- unconvert
- unparam
- unused
- varcheck

linters-settings:
gosimple:
go: "1.17"
govet:
enable-all: true
disable:
- fieldalignment
staticcheck:
go: "1.17"
stylecheck:
go: "1.17"
unused:
go: "1.17"
23 changes: 15 additions & 8 deletions workerpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package workerpool_test

import (
"context"
"errors"
"fmt"
"runtime"
"sync"
Expand Down Expand Up @@ -181,11 +182,14 @@ func TestWorkerPool(t *testing.T) {
go func() {
id := fmt.Sprintf("task #%2d", numTasks-1)
ready <- struct{}{}
wp.Submit(id, func(_ context.Context) error {
err := wp.Submit(id, func(_ context.Context) error {
defer wg.Done()
done <- struct{}{}
return nil
})
if err != nil {
t.Errorf("failed to submit task '%s': %v", id, err)
}
sc <- struct{}{}
}()

Expand Down Expand Up @@ -282,12 +286,12 @@ func TestConcurrentDrain(t *testing.T) {
<-ready
time.Sleep(10 * time.Millisecond)

if err := wp.Submit("", nil); err != workerpool.ErrDraining {
if err := wp.Submit("", nil); !errors.Is(err, workerpool.ErrDraining) {
t.Errorf("submit: got '%v', want '%v'", err, workerpool.ErrDraining)
}

results, err := wp.Drain()
if err != workerpool.ErrDraining {
if !errors.Is(err, workerpool.ErrDraining) {
t.Errorf("drain: got '%v', want '%v'", err, workerpool.ErrDraining)
}
if results != nil {
Expand Down Expand Up @@ -318,7 +322,7 @@ func TestWorkerPoolDrainAfterClose(t *testing.T) {
wp := workerpool.New(runtime.NumCPU())
wp.Close()
tasks, err := wp.Drain()
if err != workerpool.ErrClosed {
if !errors.Is(err, workerpool.ErrClosed) {
t.Errorf("got %v; want %v", err, workerpool.ErrClosed)
}
if tasks != nil {
Expand All @@ -329,7 +333,7 @@ func TestWorkerPoolDrainAfterClose(t *testing.T) {
func TestWorkerPoolSubmitAfterClose(t *testing.T) {
wp := workerpool.New(runtime.NumCPU())
wp.Close()
if err := wp.Submit("dummy", nil); err != workerpool.ErrClosed {
if err := wp.Submit("dummy", nil); !errors.Is(err, workerpool.ErrClosed) {
t.Fatalf("got %v; want %v", err, workerpool.ErrClosed)
}
}
Expand All @@ -343,10 +347,10 @@ func TestWorkerPoolManyClose(t *testing.T) {
}

// calling Close() more than once should always return an error.
if err := wp.Close(); err != workerpool.ErrClosed {
if err := wp.Close(); !errors.Is(err, workerpool.ErrClosed) {
t.Fatalf("got %v; want %v", err, workerpool.ErrClosed)
}
if err := wp.Close(); err != workerpool.ErrClosed {
if err := wp.Close(); !errors.Is(err, workerpool.ErrClosed) {
t.Fatalf("got %v; want %v", err, workerpool.ErrClosed)
}
}
Expand All @@ -361,12 +365,15 @@ func TestWorkerPoolClose(t *testing.T) {
wg.Add(n)
for i := 0; i < n; i++ {
id := fmt.Sprintf("task #%2d", i)
wp.Submit(id, func(ctx context.Context) error {
err := wp.Submit(id, func(ctx context.Context) error {
working <- struct{}{}
<-ctx.Done()
wg.Done()
return ctx.Err()
})
if err != nil {
t.Errorf("failed to submit task '%s': %v", id, err)
}
}

// ensure n workers are busy
Expand Down