Skip to content

Commit

Permalink
GH-217: Added test to replicate the deadlock bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Oct 21, 2021
1 parent 3d16fb4 commit 5711fd7
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions test/50bug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"
"testing"

"github.com/resgateio/resgate/server"
)

// Test to replicate the bug about possible client resource inconsistency.
Expand Down Expand Up @@ -50,3 +52,68 @@ func TestBug_PossibleClientResourceInconsistency(t *testing.T) {
})
}
}

// Test to replicate the bug: Deadlock on throttled access requests to same resource
//
// See: https://github.com/resgateio/resgate/issues/217
func TestBug_DeadlockOnThrottledAccessRequestsToSameResource(t *testing.T) {
const connectionCount = 4
const resetThrottle = 3
rid := "test.model"
model := resources[rid].data
runTest(t, func(s *Session) {
// Create a set of connections subscribing to the same resource
conns := make([]*Conn, 0, connectionCount)
for i := 0; i < connectionCount; i++ {
c := s.Connect()

creq := c.Request("subscribe."+rid, nil)
reqCount := 1
if i == 0 {
reqCount = 2
}
// Handle access request (and model request for the first connection)
mreqs := s.GetParallelRequests(t, reqCount)
// Handle access
mreqs.GetRequest(t, "access."+rid).
RespondSuccess(json.RawMessage(`{"get":true}`))
if i == 0 {
// Handle get
mreqs.GetRequest(t, "get."+rid).
RespondSuccess(json.RawMessage(fmt.Sprintf(`{"model":%s}`, model)))
}
creq.GetResponse(t)

conns = append(conns, c)
}

// Send system reset
s.SystemEvent("reset", json.RawMessage(`{"resources":null,"access":["test.>"]}`))
// Get throttled number of requests
mreqs := s.GetParallelRequests(t, resetThrottle)
requestCount := resetThrottle

// Respond to requests one by one
for len(mreqs) > 0 {
r := mreqs[0]
mreqs = mreqs[1:]
r.RespondSuccess(json.RawMessage(`{"get":true}`))

// If we still have remaining get or access requests not yet received
if requestCount < connectionCount {
// For each response, a new request should be sent.
req := s.GetRequest(t)
mreqs = append(mreqs, req)
requestCount++
}
}

// Assert no other requests are sent
for _, c := range conns {
c.AssertNoNATSRequest(t, rid)
}

}, func(c *server.Config) {
c.ResetThrottle = resetThrottle
})
}

0 comments on commit 5711fd7

Please sign in to comment.