Skip to content

Commit

Permalink
Merge pull request #23 from gateway-fm/feat/unit-tests
Browse files Browse the repository at this point in the history
Feat/unit tests
  • Loading branch information
dmitriyselivanov authored Jun 15, 2023
2 parents 86e0d64 + 98e52b0 commit 2006403
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 13 deletions.
26 changes: 22 additions & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,29 @@ jobs:
skip-go-installation: true
args: --timeout=10m

- name: Test
run: go test ./...
- name: run tests and generate coverage
run: go test ./... -coverprofile=./cover.out

- name: Clean workspace
uses: AutoModality/action-clean@v1.1.0
- name: check test coverage
id: coverage ## this step must have id
uses: vladopajic/go-test-coverage@v2
with:
profile: cover.out
local-prefix: github.com/org/project
threshold-file: 30
threshold-package: 30
threshold-total: 30

- name: make coverage badge
uses: action-badges/core@0.2.2
if: contains(github.ref, 'main')
with:
label: coverage
message: ${{ steps.coverage.outputs.badge-text }}
message-color: ${{ steps.coverage.outputs.badge-color }}
file-name: coverage.svg
badge-branch: badges ## orphan branch where badge will be committed
github-token: "${{ secrets.GITHUB_TOKEN }}"

release:
name: Make Release
Expand Down
9 changes: 0 additions & 9 deletions services_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ import (
"github.com/gateway-fm/service-pool/service"
)

func newHealthyService(addr string) service.IService {
srv := service.NewService(addr, "", nil)

baseSrv := srv.(*service.BaseService)
baseSrv.SetStatus(service.StatusHealthy)

return baseSrv
}

func TestServicesListShuffle(t *testing.T) {
numServices := 20
numTries := 50000
Expand Down
67 changes: 67 additions & 0 deletions services_pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package pool

import (
"github.com/gateway-fm/service-pool/service"
"testing"
"time"
)

// TestServicesPoolStart tests whether discovery loop is spawned on pool.Start()
func TestServicesPoolStart(t *testing.T) {
pool := newServicesPool(1*time.Second, 1*time.Second, dummyMutationFunc)
pool.Start(true)

time.Sleep(200 * time.Millisecond) // wait until discovery and healthchecks are finished

count := pool.Count()
if count != 1 {
t.Errorf("num services in pool want 1, got: %d", count)
}

// ok, the service was discovered

nextSrv := pool.NextService()
if nextSrv != nil {
t.Errorf("next service is not nil, got id: %s, status: %s", nextSrv.ID(), nextSrv.Status())
}

// ok, the service is in healthy slice, but has status UnHealthy (coz healthcheck func returns nil)
}

// TestServicesPoolHealthCheckLoop tests whether hc loop is spawned on pool.Start()
// and how many times hc is called if checksInterval=1s
func TestServicesPoolHealthCheckLoop(t *testing.T) {
pool := newServicesPool(100*time.Hour, 1*time.Second, healthySrvMutationFunc)
pool.Start(true)

time.Sleep(200 * time.Millisecond) // wait until discovery and healthchecks are finished

pool.List().SetOnSrvAddCallback(func(srv service.IService) error { // to make srv being always added as unhealthy
s, _ := srv.(*healthyService)
s.SetStatus(service.StatusUnHealthy)
return nil
})

for i := 0; i < 5; i++ {
pool.List().RemoveFromHealthyByIndex(0)
pool.List().Add(&healthyService{0, &service.BaseService{}}) // add service (callback makes it unhealthy)

if pool.NextService() != nil {
t.Errorf("unexpected healthy service was found")
}

time.Sleep(1 * time.Second) // wait until healthcheck is finished

if pool.NextService() == nil {
t.Errorf("unexpected no healthy services occured")
}
}

time.Sleep(3 * time.Second) // wait until 3 healthchecks are done

healthchecksDoneCount := pool.List().Next().(*healthyService).HCheckCounter

if healthchecksDoneCount > 5 { // 5 is because 1 (healthcheck during add) + 1 (seconds passed) + 3 (seconds passed)
t.Errorf("Too much healthchecks are done")
}
}
65 changes: 65 additions & 0 deletions testutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package pool

import (
"fmt"
"time"

"github.com/gateway-fm/service-pool/discovery"
"github.com/gateway-fm/service-pool/service"
)

type healthyService struct {
HCheckCounter int
*service.BaseService
}

func (s *healthyService) HealthCheck() error {
if s != nil {
s.HCheckCounter = s.HCheckCounter + 1
s.SetStatus(service.StatusHealthy)
}
return nil
}

func healthySrvMutationFunc(srv service.IService) (service.IService, error) {
baseSrv, ok := srv.(*service.BaseService)
if !ok {
return nil, fmt.Errorf("service is not BaseService")
}

return &healthyService{
0,
baseSrv,
}, nil
}

func dummyMutationFunc(srv service.IService) (service.IService, error) {
return srv, nil
}

func newHealthyService(addr string) service.IService {
srv := service.NewService(addr, "", nil)

baseSrv := srv.(*service.BaseService)
baseSrv.SetStatus(service.StatusHealthy)

return baseSrv
}

func newServicesPool(discoveryInterval time.Duration, hcInterval time.Duration, mutationFunc func(srv service.IService) (service.IService, error)) IServicesPool {
manualDisc, _ := discovery.NewManualDiscovery(discovery.TransportHttp, "localhost")

opts := &ServicesPoolsOpts{
Name: "TestServicePool",
Discovery: manualDisc,
DiscoveryInterval: discoveryInterval,
ListOpts: &ServicesListOpts{
TryUpTries: 5,
TryUpInterval: 1 * time.Second,
ChecksInterval: hcInterval,
},
MutationFnc: mutationFunc,
}

return NewServicesPool(opts)
}

0 comments on commit 2006403

Please sign in to comment.