Skip to content

Commit

Permalink
[v1.1.1] - hotfix solving issues with gorotines (#4)
Browse files Browse the repository at this point in the history
Now the package use `go test -race` comand to trace issues with `go
routines`

## What does this PR do?

<!-- Describe what this PR is doing -->

## Is this PR a?

- [x] Bugfix
- [ ] New Feature
- [ ] BREAKING CHANGES

## This PR will bump?

- [ ] Major
- [ ] Minor
- [x] Patch

## Other pieces of information

<!-- Place more information if you need -->
Now the package use test flag `-race` to track issues with gorotines.
A small changes in `healthchecker.Readiness` funcion was made to fix
the current issues of concurrence threads handling `result` variable
  • Loading branch information
gritzkoo authored Nov 6, 2024
1 parent 0d23cb4 commit a0d3699
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 33 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ jobs:
with:
go-version: ${{ matrix.go }}
- name: test application
run: |
ls -lRa
make test
run: make test
- name: Send coverage to coverall.io
uses: shogo82148/actions-goveralls@v1
with:
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# golang-health-checker-lw

## `[1.1.1] - 2024-10-06`

**_Changes_**

- `healthchecker.Readiness` **_bug fix_** race conditions: the package test was not covering issues with open threads and race conditions, now the package is thread safe.


---

## `[1.1.0] - 2024-09-16`

**_Changes_**
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
test:
go test -coverprofile=profile.cov ./...
go test -race -coverprofile=profile.cov ./...
coverage: test
go tool cover -html=profile.cov
build:
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o healthchecker pkg/**/*.go
go build -race -a -installsuffix cgo -o healthchecker pkg/**/*.go
view-docs:
godoc -http=:8331
run:
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/gritzkoo/golang-health-checker-lw

go 1.23

require gopkg.in/go-playground/assert.v1 v1.2.1
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
16 changes: 10 additions & 6 deletions pkg/healthchecker/healthchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,30 @@ func (h *HealthCheck) Readiness() Readiness {
)
wg.Add(len(h.config.Integrations))
for _, v := range h.config.Integrations {
go step(v, &result, &wg, checklist, semaphore)
go step(v, &wg, checklist, semaphore)
}
go func() {
wg.Wait()
close(checklist)
result.Duration = time.Since(start).Seconds()
}()
result.Duration = time.Since(start).Seconds()
for chk := range checklist {
if !chk.Status {
result.Status = false
}
result.Integrations = append(result.Integrations, chk)
}

return result
}

// internal function to only execute the Check.Handle function async
func step(c Check, result *Readiness, wg *sync.WaitGroup, checklist chan Integration, semaphore chan struct{}) {
defer (*wg).Done()
func step(c Check, wg *sync.WaitGroup, checklist chan Integration, semaphore chan struct{}) {
semaphore <- struct{}{} // reserve a spot on semaphore
defer func() {
wg.Done()
<-semaphore // release semaphore spot
}()
st := time.Now()
validation := c.Handle()
check := Integration{
Expand All @@ -88,9 +94,7 @@ func step(c Check, result *Readiness, wg *sync.WaitGroup, checklist chan Integra
Status: validation.Error == nil,
}
if !check.Status {
result.Status = false
check.Error = validation.Error.Error()
}
checklist <- check
<-semaphore
}
21 changes: 3 additions & 18 deletions pkg/healthchecker/healthchecker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"net/http"
"reflect"
"testing"

"gopkg.in/go-playground/assert.v1"
)

type CustomError struct {
Expand All @@ -32,21 +30,6 @@ var (
},
},
}
configSetup4 = Config{
Name: "test 1",
Version: "v1",
Integrations: []Check{
{
Name: "func 1",
Handle: func() CheckResponse {
return CheckResponse{
Error: errors.New("Test faild"),
URL: "https://someone-has-failed.com/status",
}
},
},
},
}
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -223,7 +206,9 @@ func TestHealthCheck_Readiness(t *testing.T) {
config: tt.fields.config,
}
got := h.Readiness()
assert.Equal(t, got.Status, tt.want.Status)
if got.Status != tt.want.Status {
t.Errorf("Test Readiness() fail want: %v got: %v", tt.want.Status, got.Status)
}
})
}
}

0 comments on commit a0d3699

Please sign in to comment.