Skip to content

Commit

Permalink
Stable version v1.0.0 (#5)
Browse files Browse the repository at this point in the history
* Add semaphore like in errgroup and return wrapped errors

* Add new tests for stable version

* Update go version and remove all deps

* Add Makefile to run fmt, linter and tests

* Add Gitub CI

* Add comments for new functions

* Rename module to an idiomatic name

* Update README

* Update README and LICENSE
  • Loading branch information
Kurt212 authored Nov 7, 2024
1 parent 01e38eb commit 6d34b9f
Show file tree
Hide file tree
Showing 12 changed files with 895 additions and 161 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22' # Задайте нужную версию Go

- name: Run make command
run: make ci
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.idea
bin

coverage.*
2 changes: 1 addition & 1 deletion LICENSE.md → LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2021, Alexander Trapeznikov
Copyright (c) 2021 Alexander Trapeznikov
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
39 changes: 39 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
BIN_DIR = $(PWD)/bin

GOIMPORTS_BIN = $(BIN_DIR)/goimports
GOLANGCI_BIN = $(BIN_DIR)/golangci-lint

.PHONY: all
all: tidy fmt lint test

.PHONY: tidy
tidy:
go mod tidy

.PHONY: test
test:
go test -v -race -cover -coverprofile=coverage.out -timeout 10s -cpu 1,2,4,8 ./...
go tool cover -html=coverage.out -o coverage.html

.PHONY: vet
vet:
go vet ./...

.PHONY: fmt
fmt:
go fmt ./...
$(GOIMPORTS_BIN) -w .

.PHONY: lint
lint: vet
$(GOLANGCI_BIN) run -c golangci.yaml ./...

.PHONY: ci
ci: deps lint test

.PHONY: deps
deps:
go mod download
mkdir -p $(BIN_DIR)
GOBIN=$(BIN_DIR) go install golang.org/x/tools/cmd/goimports@v0.26.0
GOBIN=$(BIN_DIR) go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.61.0
64 changes: 63 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
### Syncgroup

[![Go Report Card](https://goreportcard.com/badge/github.com/kurt212/syncgroup)](https://goreportcard.com/report/github.com/kurt212/syncgroup)
![Build Status](https://github.com/kurt212/syncgroup/actions/workflows/ci.yml/badge.svg)
![GitHub issues](https://img.shields.io/github/issues/kurt212/syncgroup)
![GitHub pull requests](https://img.shields.io/github/issues-pr/kurt212/syncgroup)

[![Go Reference](https://pkg.go.dev/badge/github.com/kurt212/syncgroup.svg)](https://pkg.go.dev/github.com/kurt212/syncgroup)
![Go Version](https://img.shields.io/github/go-mod/go-version/kurt212/syncgroup)

This is a package that contains an implementation of an abstract
synchronisation mechanism - synchronisation group.
The main idea is to have an ability to run independent tasks in separate goroutines which way return errors.
Expand All @@ -9,4 +17,58 @@ The design is similar to errgroup (https://godoc.org/golang.org/x/sync/errgroup)
but it does not cancel the context of the goroutines if any of them returns an error.

## Documentation
See more on [godoc site](https://godoc.org/github.com/Kurt212/syncgroup)
See more on [godoc site](https://godoc.org/github.com/kurt212/syncgroup)

## Usage

### Installation

```shell
go get github.com/kurt212/syncgroup
```

### Example

```go
package main

import (
"fmt"
"time"

"github.com/kurt212/syncgroup"
)

func main() {
sg := syncgroup.New()

for i := range 10 {
sg.Go(func() error {
time.Sleep(1 * time.Second)

fmt.Printf("Hello from %d\n", i)

return nil
})
}

sg.Wait()
}
```

## Contributing

Feel free to contribute to this project. You can report bugs, suggest features or submit pull requests.

Before submitting a bug report or a feature request, check if there is an existing one and provide as much information as possible.

### Submitting a pull request

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Run checks (`make all`)
4. Commit your changes (`git commit -am 'Add some feature'`)
5. Push to the branch (`git push origin my-new-feature`)
6. Create a new Pull Request
7. Wait for CI to pass
8. Profit! 🎉
9 changes: 3 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
module github.com/Kurt212/syncgroup
module github.com/kurt212/syncgroup

go 1.13
go 1.22.1

require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1
)
toolchain go1.22.5
15 changes: 0 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +0,0 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7 changes: 7 additions & 0 deletions golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
linters:
enable-all: true
disable:
- depguard
- exportloopref
- gomnd
- execinquery
69 changes: 69 additions & 0 deletions internal/testutil/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package testutil

import (
"reflect"
"testing"
)

// Equal is a helper function to check equality of two values.
func Equal(t *testing.T, expected, actual any, optionalMessage ...string) {
t.Helper()

var message string
if len(optionalMessage) > 0 {
message = optionalMessage[0]
}

if !reflect.DeepEqual(expected, actual) {
t.Fatalf("expected %v, got %v %q", expected, actual, message)
}
}

// True is Helper function to check if a value is true.
func True(t *testing.T, actual bool, optionalMessage ...string) {
t.Helper()

var message string
if len(optionalMessage) > 0 {
message = optionalMessage[0]
}

if !actual {
t.Fatal("expected true, got false", message)
}
}

// EqualSlices is a helper function to check equality of two slices.
func EqualSlices[T1, T2 any](t *testing.T, expected []T1, actual []T2) {
t.Helper()

if len(expected) != len(actual) {
t.Fatalf("expected slice length %d, got %d", len(expected), len(actual))

return
}

for i := range expected {
if !reflect.DeepEqual(expected[i], actual[i]) {
t.Fatalf("at index %d: expected %v, got %v", i, expected[i], actual[i])
}
}
}

// Panics is a helper function to check if a function panics.
func Panics(t *testing.T, fnc func(), optionalMessage ...string) {
t.Helper()

var message string
if len(optionalMessage) > 0 {
message = optionalMessage[0]
}

defer func() {
if recover() == nil {
t.Fatalf("expected panic, got nil %q", message)
}
}()

fnc()
}
Loading

0 comments on commit 6d34b9f

Please sign in to comment.