Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-86: Fixed crash bug on load error on disposed resources. #87

Merged
merged 1 commit into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions server/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,32 @@ func (s *Subscription) IsReady() bool {
// when loading the resource, resourceSub will be nil, and err will be the error.
func (s *Subscription) Loaded(resourceSub *rescache.ResourceSubscription, err error) {
if !s.c.Enqueue(func() {
if err != nil {
s.err = err
s.doneLoading()
return
}

if s.state == stateDisposed {
resourceSub.Unsubscribe(s)
return
}

if err != nil {
s.err = err
} else {
s.resourceSub = resourceSub
s.typ = resourceSub.GetResourceType()
s.state = stateLoaded

s.setResource()
}
s.resourceSub = resourceSub
s.typ = resourceSub.GetResourceType()
s.state = stateLoaded

s.setResource()
if s.err != nil {
s.doneLoading()
} else {
rcbs := s.readyCallbacks
s.readyCallbacks = nil
// Collect references for any waiting ready callbacks
for _, rcb := range rcbs {
s.collectRefs(rcb)
}
return
}

rcbs := s.readyCallbacks
s.readyCallbacks = nil
// Collect references for any waiting ready callbacks
for _, rcb := range rcbs {
s.collectRefs(rcb)
}
}) {
if err == nil {
Expand Down
121 changes: 121 additions & 0 deletions test/14http_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"encoding/json"
"net/http"
"strings"
"testing"

"github.com/jirenius/resgate/server/reserr"
Expand Down Expand Up @@ -293,3 +294,123 @@ func TestHTTPGetInvalidURLs(t *testing.T) {
})
}
}

// Test handle HTTP get requests with cyclic data
func TestCyclicHTTPGetTest(t *testing.T) {
// The following cyclic groups exist
// a -> a

// b -> c
// c -> f

// d -> e, f
// e -> d
// f -> d

// Other entry points
// g -> e, f
// h -> e

resources := map[string]string{
"example.a": `{"a":{"rid":"example.a"}}`,

"example.b": `{"c":{"rid":"example.c"}}`,
"example.c": `{"b":{"rid":"example.b"}}`,

"example.d": `{"e":{"rid":"example.e"},"f":{"rid":"example.f"}}`,
"example.e": `{"d":{"rid":"example.d"}}`,
"example.f": `{"d":{"rid":"example.d"}}`,

"example.g": `{"e":{"rid":"example.e"},"f":{"rid":"example.f"}}`,
"example.h": `{"e":{"rid":"example.e"}}`,
}

responses := map[string]string{
"example.a": `{"a":{"href":"/api/example/a"}}`,
"example.b": `{"c":{"href":"/api/example/c","model":{"b":{"href":"/api/example/b"}}}}`,
"example.d": `{"e":{"href":"/api/example/e","model":{"d":{"href":"/api/example/d"}}},"f":{"href":"/api/example/f","model":{"d":{"href":"/api/example/d"}}}}`,
"example.g": `{"e":{"href":"/api/example/e","model":{"d":{"href":"/api/example/d","model":{"e":{"href":"/api/example/e"},"f":{"href":"/api/example/f","model":{"d":{"href":"/api/example/d"}}}}}}},"f":{"href":"/api/example/f","model":{"d":{"href":"/api/example/d","model":{"e":{"href":"/api/example/e","model":{"d":{"href":"/api/example/d"}}},"f":{"href":"/api/example/f"}}}}}}`,
"example.h": `{"e":{"href":"/api/example/e","model":{"d":{"href":"/api/example/d","model":{"e":{"href":"/api/example/e"},"f":{"href":"/api/example/f","model":{"d":{"href":"/api/example/d"}}}}}}}}`,
}

tbl := [][]struct {
Event string
RID string
}{
{
{"subscribe", "example.a"},
{"access", "example.a"},
{"get", "example.a"},
{"response", "example.a"},
},
{
{"subscribe", "example.b"},
{"access", "example.b"},
{"get", "example.b"},
{"get", "example.c"},
{"response", "example.b"},
},
{
{"subscribe", "example.d"},
{"access", "example.d"},
{"get", "example.d"},
{"get", "example.e"},
{"get", "example.f"},
{"response", "example.d"},
},
{
{"subscribe", "example.g"},
{"access", "example.g"},
{"get", "example.g"},
{"get", "example.e"},
{"get", "example.f"},
{"get", "example.d"},
{"response", "example.g"},
},
{
{"subscribe", "example.d"},
{"access", "example.d"},
{"get", "example.d"},
{"subscribe", "example.h"},
{"access", "example.h"},
{"get", "example.e"},
{"get", "example.h"},
{"get", "example.f"},
{"response", "example.d"},
{"response", "example.h"},
},
}

for _, l := range tbl {
runTest(t, func(s *Session) {
var hreq *HTTPRequest
var req *Request

hreqs := make(map[string]*HTTPRequest)
reqs := make(map[string]*Request)

for _, ev := range l {
switch ev.Event {
case "subscribe":
url := "/api/" + strings.Replace(ev.RID, ".", "/", -1)
hreqs[ev.RID] = s.HTTPRequest("GET", url, nil)
case "access":
for req = reqs["access."+ev.RID]; req == nil; req = reqs["access."+ev.RID] {
treq := s.GetRequest(t)
reqs[treq.Subject] = treq
}
req.RespondSuccess(json.RawMessage(`{"get":true}`))
case "get":
for req = reqs["get."+ev.RID]; req == nil; req = reqs["get."+ev.RID] {
req = s.GetRequest(t)
reqs[req.Subject] = req
}
req.RespondSuccess(json.RawMessage(`{"model":` + resources[ev.RID] + `}`))
case "response":
hreq = hreqs[ev.RID]
hreq.GetResponse(t).Equals(t, http.StatusOK, json.RawMessage(responses[ev.RID]))
}
}
})
}
}