-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from gateway-fm/feat/unit-tests
Feat/unit tests
- Loading branch information
Showing
4 changed files
with
154 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |