-
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
0 parents
commit a2a41f9
Showing
14 changed files
with
539 additions
and
0 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,33 @@ | ||
name: Coverage | ||
on: | ||
push: | ||
tags: | ||
- 'v*' | ||
permissions: | ||
contents: read | ||
jobs: | ||
coverage: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.21' | ||
- run: | | ||
go test -coverprofile=coverage.out -covermode=atomic ./... | ||
for dir in v[0-9]*; | ||
do | ||
test -d "${dir}" || continue | ||
cd "${dir}" || continue | ||
go test -coverprofile="../coverage-${dir}.out" -covermode=atomic ./... | ||
cd .. | ||
done | ||
- uses: codecov/codecov-action@v3 | ||
with: | ||
files: coverage.out, coverage-* | ||
env: | ||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} |
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,37 @@ | ||
name: "CodeQL" | ||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
branches: | ||
- master | ||
schedule: | ||
- cron: '0 0 */7 * *' | ||
jobs: | ||
analyze: | ||
name: Analyze | ||
runs-on: ubuntu-latest | ||
permissions: | ||
actions: read | ||
contents: read | ||
security-events: write | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
language: | ||
- go | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
- name: Initialize CodeQL | ||
uses: github/codeql-action/init@v2 | ||
with: | ||
languages: ${{ matrix.language }} | ||
queries: security-extended,security-and-quality | ||
- name: Autobuild | ||
uses: github/codeql-action/autobuild@v2 | ||
- name: Perform CodeQL Analysis | ||
uses: github/codeql-action/analyze@v2 | ||
with: | ||
category: "/language:${{matrix.language}}" |
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,45 @@ | ||
name: Lint and test | ||
on: | ||
push: | ||
tags: | ||
- 'v*' | ||
branches: | ||
- master | ||
pull_request: | ||
permissions: | ||
contents: read | ||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.21' | ||
- uses: actions/checkout@v4 | ||
- run: | | ||
go test -v -race -bench=^BenchmarkRace ./... | ||
for dir in v[0-9]*; | ||
do | ||
test -d "${dir}" || continue | ||
cd "${dir}" || continue | ||
go test -v -race -bench=^BenchmarkRace ./... | ||
cd .. | ||
done | ||
lint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.21' | ||
- uses: actions/checkout@v4 | ||
- uses: golangci/golangci-lint-action@v3 | ||
with: | ||
version: v1.55.2 | ||
- uses: golangci/golangci-lint-action@v3 | ||
with: | ||
version: v1.55.2 | ||
working-directory: v2 |
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,51 @@ | ||
linters: | ||
enable: | ||
- bidichk | ||
- bodyclose | ||
- decorder | ||
- errcheck | ||
- errname | ||
- errorlint | ||
- exportloopref | ||
- forbidigo | ||
- forcetypeassert | ||
- gocheckcompilerdirectives | ||
- gochecknoglobals | ||
- gochecknoinits | ||
- gochecksumtype | ||
- goconst | ||
- gocritic | ||
- gocyclo | ||
- godot | ||
- godox | ||
- gofmt | ||
- goheader | ||
- gomnd | ||
- gosec | ||
- gosimple | ||
- govet | ||
- ineffassign | ||
- lll | ||
- misspell | ||
- nestif | ||
- nilerr | ||
- nilnil | ||
- nolintlint | ||
- perfsprint | ||
- prealloc | ||
- predeclared | ||
- reassign | ||
- revive | ||
- sqlclosecheck | ||
- staticcheck | ||
- stylecheck | ||
- tenv | ||
- testableexamples | ||
- testifylint | ||
- typecheck | ||
- unconvert | ||
- unused | ||
- usestdlibvars | ||
- wastedassign | ||
- wsl | ||
- zerologlint |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Aleksandr Kramarenko akramarenkov@yandex.ru | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,40 @@ | ||
# Breaker | ||
|
||
[![Go Reference](https://pkg.go.dev/badge/github.com/akramarenkov/breaker.svg)](https://pkg.go.dev/github.com/akramarenkov/breaker) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/akramarenkov/breaker)](https://goreportcard.com/report/github.com/akramarenkov/breaker) | ||
[![codecov](https://codecov.io/gh/akramarenkov/breaker/branch/master/graph/badge.svg?token=)](https://codecov.io/gh/akramarenkov/breaker) | ||
|
||
## Purpose | ||
|
||
Library that allows you to break goroutine and wait it completion | ||
|
||
## Usage | ||
|
||
Example: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/akramarenkov/breaker/breaker" | ||
) | ||
|
||
func main() { | ||
breaker := breaker.New() | ||
|
||
go func() { | ||
defer breaker.Complete() | ||
|
||
_, opened := <-breaker.IsBreaked() | ||
|
||
fmt.Println(opened) | ||
}() | ||
|
||
breaker.Break() | ||
|
||
// Output: | ||
// false | ||
} | ||
``` |
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,42 @@ | ||
// Contains a Breaker implementation that is used to break a goroutine and | ||
// wait for it to complete. | ||
package breaker | ||
|
||
import "github.com/akramarenkov/breaker/closing" | ||
|
||
// Used to break goroutine and wait it completion. | ||
type Breaker struct { | ||
completer *closing.Closing | ||
interrupter *closing.Closing | ||
} | ||
|
||
// Creates Breaker instance. | ||
func New() *Breaker { | ||
brk := &Breaker{ | ||
completer: closing.New(), | ||
interrupter: closing.New(), | ||
} | ||
|
||
return brk | ||
} | ||
|
||
// Closes channel returned by IsBreaked() method and wait call of Complete() method. | ||
// | ||
// Method is thread-safe, it can be called many times. | ||
// | ||
// Also you can specify additional functions that will be called once, | ||
// before waiting for completion. | ||
func (brk *Breaker) Break(adds ...func()) { | ||
brk.interrupter.Close(adds...) | ||
<-brk.completer.IsClosed() | ||
} | ||
|
||
// Returns channel that will be closed by Break() method. | ||
func (brk *Breaker) IsBreaked() <-chan struct{} { | ||
return brk.interrupter.IsClosed() | ||
} | ||
|
||
// Marks a goroutine as completed. | ||
func (brk *Breaker) Complete() { | ||
brk.completer.Close(nil) | ||
} |
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 breaker_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/akramarenkov/breaker/breaker" | ||
) | ||
|
||
func ExampleBreaker() { | ||
breaker := breaker.New() | ||
|
||
go func() { | ||
defer breaker.Complete() | ||
|
||
_, opened := <-breaker.IsBreaked() | ||
|
||
fmt.Println(opened) | ||
}() | ||
|
||
breaker.Break() | ||
|
||
// Output: | ||
// false | ||
} |
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,52 @@ | ||
package breaker | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBreaker(t *testing.T) { | ||
testBreaker(t) | ||
|
||
counter := 0 | ||
|
||
add := func() { | ||
counter++ | ||
} | ||
|
||
testBreaker(t, add, nil) | ||
require.Equal(t, 1, counter) | ||
} | ||
|
||
func testBreaker(t *testing.T, adds ...func()) { | ||
breaker := New() | ||
|
||
select { | ||
case <-breaker.IsBreaked(): | ||
require.FailNow(t, "must not be breaked") | ||
default: | ||
} | ||
|
||
go func() { | ||
defer breaker.Complete() | ||
|
||
<-breaker.IsBreaked() | ||
}() | ||
|
||
breaker.Break(adds...) | ||
|
||
select { | ||
case <-breaker.IsBreaked(): | ||
default: | ||
require.FailNow(t, "must be breaked") | ||
} | ||
|
||
breaker.Break(adds...) | ||
|
||
select { | ||
case <-breaker.IsBreaked(): | ||
default: | ||
require.FailNow(t, "must be breaked") | ||
} | ||
} |
Oops, something went wrong.