Skip to content

Commit

Permalink
fmting hostcheckermanager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tbuchaillot committed Sep 23, 2020
1 parent 3708fe0 commit b982a9a
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions gateway/host_checker_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"testing"
"text/template"
)
func TestHostCheckerManagerInit(t *testing.T){

func TestHostCheckerManagerInit(t *testing.T) {
ts := StartTest()
defer ts.Close()

hc := HostCheckerManager{}
redisStorage:= &storage.RedisCluster{KeyPrefix: "host-checker:"}
redisStorage := &storage.RedisCluster{KeyPrefix: "host-checker:"}
hc.Init(redisStorage)

if hc.Id == ""{
if hc.Id == "" {
t.Error("HostCheckerManager should create an Id on Init")
}
if hc.unhealthyHostList == nil {
Expand All @@ -28,7 +29,7 @@ func TestHostCheckerManagerInit(t *testing.T){
}
}

func TestAmIPolling(t *testing.T){
func TestAmIPolling(t *testing.T) {
hc := HostCheckerManager{}

pooling := hc.AmIPolling()
Expand All @@ -44,12 +45,11 @@ func TestAmIPolling(t *testing.T){
ts := StartTest()
defer ts.Close()

redisStorage:= &storage.RedisCluster{KeyPrefix: "host-checker:"}
redisStorage := &storage.RedisCluster{KeyPrefix: "host-checker:"}
hc.Init(redisStorage)
hc2 := HostCheckerManager{}
hc2.Init(redisStorage)


pooling = hc.AmIPolling()
poolingHc2 := hc2.AmIPolling()
if !pooling && poolingHc2 {
Expand All @@ -59,7 +59,7 @@ func TestAmIPolling(t *testing.T){
//Testing if the PollerCacheKey contains the poller_group
activeInstance, err := hc.store.GetKey("PollerActiveInstanceID.TEST")
if err != nil {
t.Error("PollerActiveInstanceID.TEST should exist in redis.",activeInstance)
t.Error("PollerActiveInstanceID.TEST should exist in redis.", activeInstance)
}
if activeInstance != hc.Id {
t.Error("PollerActiveInstanceID.TEST value should be hc.Id")
Expand All @@ -76,36 +76,36 @@ func TestAmIPolling(t *testing.T){

activeInstance, err = hc.store.GetKey("PollerActiveInstanceID")
if err != nil {
t.Error("PollerActiveInstanceID should exist in redis.",activeInstance)
t.Error("PollerActiveInstanceID should exist in redis.", activeInstance)
}
if activeInstance != hc.Id {
t.Error("PollerActiveInstanceID value should be hc.Id")
}
}

func TestGenerateCheckerId(t *testing.T){
func TestGenerateCheckerId(t *testing.T) {
hc := HostCheckerManager{}
hc.GenerateCheckerId()
if hc.Id == ""{
if hc.Id == "" {
t.Error("HostCheckerManager should generate an Id on GenerateCheckerId")
}

uuid, _ := uuid2.FromString(hc.Id)
uuid, _ := uuid2.FromString(hc.Id)
if uuid.Version() != 4 {
t.Error("HostCheckerManager should generate an uuid.v4 id")
}
}

func TestCheckActivePollerLoop(t *testing.T){
func TestCheckActivePollerLoop(t *testing.T) {
ts := StartTest()
defer ts.Close()
emptyRedis()

hc := &HostCheckerManager{}
redisStorage:= &storage.RedisCluster{KeyPrefix: "host-checker:"}
redisStorage := &storage.RedisCluster{KeyPrefix: "host-checker:"}
hc.Init(redisStorage)
//defering the stop of the CheckActivePollerLoop
defer func (hc *HostCheckerManager){
defer func(hc *HostCheckerManager) {
hc.stopLoop = true
}(hc)

Expand All @@ -114,21 +114,21 @@ func TestCheckActivePollerLoop(t *testing.T){
found := false

//Giving 5 retries to find the poller active key
for i:=0;i<5;i++{
for i := 0; i < 5; i++ {
activeInstance, err := hc.store.GetKey("PollerActiveInstanceID")
if activeInstance == hc.Id && err ==nil{
if activeInstance == hc.Id && err == nil {
found = true
break
}
}

if !found{
if !found {
t.Error("activeInstance should be hc.Id when the CheckActivePollerLoop is running")
}

}

func TestStartPoller(t *testing.T){
func TestStartPoller(t *testing.T) {
hc := HostCheckerManager{}
hc.StartPoller()

Expand All @@ -137,13 +137,13 @@ func TestStartPoller(t *testing.T){
}
}

func TestRecordUptimeAnalytics(t *testing.T){
func TestRecordUptimeAnalytics(t *testing.T) {
ts := StartTest()
defer ts.Close()
emptyRedis()

hc := &HostCheckerManager{}
redisStorage:= &storage.RedisCluster{KeyPrefix: "host-checker:"}
redisStorage := &storage.RedisCluster{KeyPrefix: "host-checker:"}
hc.Init(redisStorage)

specTmpl := template.Must(template.New("spec").Parse(sampleUptimeTestAPI))
Expand All @@ -169,30 +169,27 @@ func TestRecordUptimeAnalytics(t *testing.T){
apisMu.Unlock()
}()



hostData := HostData{
CheckURL: "/test",
Method: http.MethodGet,
Method: http.MethodGet,
}
report := HostHealthReport{
HostData:hostData,
HostData: hostData,
ResponseCode: http.StatusOK,
Latency: 10.00,
IsTCPError: false,
Latency: 10.00,
IsTCPError: false,
}
report.MetaData = make(map[string]string)
report.MetaData[UnHealthyHostMetaDataAPIKey] = spec.APIID


err := hc.RecordUptimeAnalytics(report)
if err != nil{
if err != nil {
t.Error("RecordUptimeAnalytics shouldn't fail")
}

set , err := hc.store.Exists(UptimeAnalytics_KEYNAME)
if err != nil || !set{
t.Error("tyk-uptime-analytics should exist in redis.",err)
set, err := hc.store.Exists(UptimeAnalytics_KEYNAME)
if err != nil || !set {
t.Error("tyk-uptime-analytics should exist in redis.", err)
}

}
}

0 comments on commit b982a9a

Please sign in to comment.