Skip to content

Commit

Permalink
Merge 59f25fe into 60fdced
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode committed Nov 5, 2018
2 parents 60fdced + 59f25fe commit fc15a49
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,9 @@ func doReload() {
// load the specs
count := syncAPISpecs()
// skip re-loading only if dashboard service reported 0 APIs
// and current registry had 0 APIs
if count == 0 && apisByIDLen() == 0 {
// this means if only one is left, it will not be unloaded,
// this is preferred to accidentally loading 0 and downing all services
if count == 0 {
mainLog.Warning("No API Definitions found, not reloading")
return
}
Expand Down
98 changes: 98 additions & 0 deletions rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package main

import (
"fmt"
"github.com/gorilla/mux"
"testing"
"time"

Expand Down Expand Up @@ -79,6 +81,102 @@ func TestSyncAPISpecsRPCFailure(t *testing.T) {
}
}

const apiDefListTest = `[{
"api_id": "1",
"definition": {
"location": "header",
"key": "version"
},
"auth": {"auth_header_name": "authorization"},
"version_data": {
"versions": {
"v1": {"name": "v1"}
}
},
"proxy": {
"listen_path": "/v1",
"target_url": "` + testHttpAny + `"
}
}]`

const apiDefListTest2 = `[{
"api_id": "1",
"definition": {
"location": "header",
"key": "version"
},
"auth": {"auth_header_name": "authorization"},
"version_data": {
"versions": {
"v1": {"name": "v1"}
}
},
"proxy": {
"listen_path": "/v1",
"target_url": "` + testHttpAny + `"
}
},
{
"api_id": "2",
"definition": {
"location": "header",
"key": "version"
},
"auth": {"auth_header_name": "authorization"},
"version_data": {
"versions": {
"v2": {"name": "v2"}
}
},
"proxy": {
"listen_path": "/v2",
"target_url": "` + testHttpAny + `"
}
}]`

func TestSyncAPISpecsRPCFailure_CheckGlobals(t *testing.T) {
// Mock RPC
callCount := 0
dispatcher := gorpc.NewDispatcher()
dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *DefRequest) (string, error) {
if callCount == 0 {
callCount += 1
return apiDefListTest, nil
}

if callCount == 1 {
callCount += 1
return apiDefListTest2, nil
}

return "malformed json", nil
})
dispatcher.AddFunc("Login", func(clientAddr, userKey string) bool {
return true
})

rpc := startRPCMock(dispatcher)
defer stopRPCMock(rpc)

// Three cases: 1 API, 2 APIs and Malformed data
exp := []int{4, 6, 6}

for _, e := range exp {
doReload()

rtCnt := 0
mainRouter.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
rtCnt += 1
fmt.Println(route.GetPathTemplate())
return nil
})

if rtCnt != e {
t.Errorf("There should be %v routes, got %v", e, rtCnt)
}
}
}

func TestSyncAPISpecsRPCSuccess(t *testing.T) {
// Mock RPC
dispatcher := gorpc.NewDispatcher()
Expand Down

0 comments on commit fc15a49

Please sign in to comment.