Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.
Open
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: 11 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ name: CI

on:
push:
branches: [master]
branches: [ master ]
pull_request:
branches: [master]
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.17', '1.18', '1.19' ]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v2
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: ${{ matrix.go }}

- name: CI
- name: Run CI
env:
COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
run: make ci
run: make ci
3 changes: 2 additions & 1 deletion .golangci.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ linters-settings:
misspell:
locale: US
gosimple:
go: "1.16"
go: "1.17"
checks: ["all"]
prealloc:
simple: true
Expand All @@ -50,3 +50,4 @@ linters:
- gosimple
- prealloc
fast: false

39 changes: 18 additions & 21 deletions Makefile
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
TOOLS_BIN=$(shell pwd)/.tools
COVERALLS_TOKEN?=dev

.PHONY: install
install:
go mod download
rm -rf $(TOOLS_BIN)
mkdir -p $(TOOLS_BIN)
GO111MODULE=off GOBIN=$(TOOLS_BIN) go get github.com/mattn/goveralls
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(TOOLS_BIN) v1.38.0

lint:
$(TOOLS_BIN)/golangci-lint -v run ./...
go install github.com/dewep-online/devtool@latest

.PHONY: setup
setup:
devtool setup-lib

generate:
go generate -v ./...
.PHONY: lint
lint:
devtool lint

.PHONY: build
build:
go build -race -v ./...
devtool build

.PHONY: tests
tests:
@if [ "$(COVERALLS_TOKEN)" = "dev" ]; then \
go test -race -v ./... ;\
else \
go test -race -v -covermode=atomic -coverprofile=coverage.out ./... ;\
$(TOOLS_BIN)/goveralls -coverprofile=coverage.out -repotoken $(COVERALLS_TOKEN) ;\
fi
devtool test

.PHONY: pre-commite
pre-commite: setup lint build tests

pre-commite: generate lint tests
.PHONY: ci
ci: install setup lint build tests

ci: install build lint tests
27 changes: 27 additions & 0 deletions contexts/contexts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package contexts

import (
"context"
"reflect"
)

func Combine(multi ...context.Context) context.Context {
ctx, cancel := context.WithCancel(context.Background())

go func() {
cases := make([]reflect.SelectCase, 0, len(multi))
for _, vv := range multi {
cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(vv.Done()),
})
}
chosen, _, _ := reflect.Select(cases)
switch chosen {
default:
cancel()
}
}()

return ctx
}
37 changes: 37 additions & 0 deletions contexts/contexts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package contexts_test

import (
"context"
"fmt"
"testing"
"time"

"github.com/deweppro/go-utils/contexts"
)

func TestUnit_Combine(t *testing.T) {
cc, cancel := context.WithCancel(context.Background())
c := contexts.Combine(context.Background(), context.Background(), cc)
if c == nil {
t.Fatalf("contexts.Combine returned nil")
}

select {
case <-c.Done():
t.Fatalf("<-c.Done() == it should block")
default:
}

cancel()
<-time.After(time.Second)

select {
case <-c.Done():
default:
t.Fatalf("<-c.Done() it shouldn't block")
}

if got, want := fmt.Sprint(c), "context.Background.WithCancel"; got != want {
t.Fatalf("contexts.Combine() = %q want %q", got, want)
}
}
4 changes: 2 additions & 2 deletions routine/routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func Retry(count int, ttl time.Duration, call func() error) error {
}

func Parallel(calls ...func()) {
wg := sync.WaitGroup{}
wg.Add(len(calls))
var wg sync.WaitGroup
for _, call := range calls {
call := call
wg.Add(1)
go func() {
call()
wg.Done()
Expand Down
Loading