Skip to content

Commit

Permalink
Merge pull request #25 from gateway-fm/feat/imporve-unit-tests-srvpoo…
Browse files Browse the repository at this point in the history
…l-srvlist

feat: improve unit tests for srv list and pool
  • Loading branch information
dmitriyselivanov authored Jun 15, 2023
2 parents 052ea66 + 48c6ba5 commit 2a8a4e8
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ update:
go get -u ./...

test:
go test ./...
go test ./...

test-cover:
go test ./... -coverprofile=coverage.out && go tool cover -html=coverage.out
23 changes: 23 additions & 0 deletions services_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,26 @@ func TestServicesListShuffle(t *testing.T) {
}
}
}

func TestServicesListTryUp(t *testing.T) {
list := NewServicesList("testServicesList", &ServicesListOpts{
TryUpTries: 5,
TryUpInterval: 1 * time.Second,
ChecksInterval: 1 * time.Second,
})

srv := &healthyService{0, &service.BaseService{}}

list.Add(srv)
list.FromHealthyToJail(srv.ID())

if list.CountAll() != 1 {
t.Errorf("unexpected num of services in list")
}

list.TryUpService(srv, 0)

if list.Next() == nil {
t.Errorf("unexpected no healthy services")
}
}
106 changes: 105 additions & 1 deletion services_pool_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package pool

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

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

// TestServicesPoolStart tests whether discovery loop is spawned on pool.Start()
Expand Down Expand Up @@ -65,3 +67,105 @@ func TestServicesPoolHealthCheckLoop(t *testing.T) {
t.Errorf("Too much healthchecks are done")
}
}

func TestServicesPoolCustomList(t *testing.T) {
manualDisc, _ := discovery.NewManualDiscovery(discovery.TransportHttp, "localhost")

customList := NewServicesList("testServiceList", &ServicesListOpts{
TryUpTries: 100,
TryUpInterval: 100 * time.Second,
ChecksInterval: 100 * time.Second,
})
customList.Add(&healthyService{0, &service.BaseService{}})

opts := &ServicesPoolsOpts{
Name: "TestServicePool",
Discovery: manualDisc,
DiscoveryInterval: 1 * time.Second,
ListOpts: &ServicesListOpts{
TryUpTries: 5,
TryUpInterval: 1 * time.Second,
ChecksInterval: 1 * time.Second,
},
MutationFnc: func(srv service.IService) (service.IService, error) {
return srv, nil
},
CustomList: customList,
}
pool := NewServicesPool(opts)

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

if pool.List().Unhealthy() != nil {
t.Errorf("unexpected unhealthy services")
}
}

func TestServicesPoolDiscoveryError(t *testing.T) {
consulDisc, _ := discovery.NewConsulDiscovery(discovery.TransportHttp, "localhost")
opts := &ServicesPoolsOpts{
Name: "TestServicePool",
Discovery: consulDisc,
DiscoveryInterval: 1 * time.Second,
ListOpts: &ServicesListOpts{
TryUpTries: 5,
TryUpInterval: 1 * time.Second,
ChecksInterval: 1 * time.Second,
},
MutationFnc: func(srv service.IService) (service.IService, error) {
return srv, nil
},
}

pool := NewServicesPool(opts)
if err := pool.DiscoverServices(); err == nil {
t.Errorf("unexpected nil error during consul discovery")
}
}

func TestServicesPoolClose(t *testing.T) {
pool := newServicesPool(1*time.Second, 1*time.Second, healthySrvMutationFunc)
pool.Start(true)

time.Sleep(200 * time.Millisecond)

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

pool.List().RemoveFromHealthyByIndex(0)
pool.Close()
time.Sleep(1 * time.Second)

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

func TestServicesPoolCallbacks(t *testing.T) {
pool := newServicesPool(1*time.Second, 1*time.Second, healthySrvMutationFunc)

onNewDiscIsExecuted := false
pool.SetOnNewDiscCallback(func(srv service.IService) error {
onNewDiscIsExecuted = true
return nil
})

onDiscCompletedIsExecuted := false
pool.SetOnDiscCompletedCallback(func() {
onDiscCompletedIsExecuted = true
})

pool.Start(true)
time.Sleep(200 * time.Millisecond)

if !onNewDiscIsExecuted {
t.Errorf("onNewDiscCallback was not executed")
}

if !onDiscCompletedIsExecuted {
t.Errorf("onDiscCompletedCallback was not executed")
}
}

0 comments on commit 2a8a4e8

Please sign in to comment.