From a571e110b1c5e6055e0fbf7ee0b874bfef6a5561 Mon Sep 17 00:00:00 2001 From: Dmitry Selivanov Date: Thu, 15 Jun 2023 15:38:26 +0200 Subject: [PATCH 1/2] feat: improve unit tests for srv list and pool --- Makefile | 5 +- services_list_test.go | 23 ++++++++++ services_pool_test.go | 103 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 844aef2..88d5522 100644 --- a/Makefile +++ b/Makefile @@ -5,4 +5,7 @@ update: go get -u ./... test: - go test ./... \ No newline at end of file + go test ./... + +test-cover: + go test ./... -coverprofile=coverage.out && go tool cover -html=coverage.out \ No newline at end of file diff --git a/services_list_test.go b/services_list_test.go index 2738a38..3b48973 100644 --- a/services_list_test.go +++ b/services_list_test.go @@ -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") + } +} diff --git a/services_pool_test.go b/services_pool_test.go index bf97178..bcc99a8 100644 --- a/services_pool_test.go +++ b/services_pool_test.go @@ -1,6 +1,7 @@ package pool import ( + "github.com/gateway-fm/service-pool/discovery" "github.com/gateway-fm/service-pool/service" "testing" "time" @@ -65,3 +66,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") + } +} From 48c6ba5e1025a2881bacc351483ed49d64f657e5 Mon Sep 17 00:00:00 2001 From: Dmitry Selivanov Date: Thu, 15 Jun 2023 15:44:49 +0200 Subject: [PATCH 2/2] fix: sort imports --- services_pool_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services_pool_test.go b/services_pool_test.go index bcc99a8..294aa2e 100644 --- a/services_pool_test.go +++ b/services_pool_test.go @@ -1,10 +1,11 @@ package pool import ( - "github.com/gateway-fm/service-pool/discovery" - "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()