Skip to content

Commit

Permalink
GH-187: Added test for reset throttling.
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Aug 25, 2021
1 parent 9599dee commit ed3652f
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions test/11system_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package test
import (
"encoding/json"
"fmt"
"strings"
"testing"

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

Expand Down Expand Up @@ -335,3 +337,49 @@ func TestSystemReset_MismatchingResourceTypeResponseOnCollection_LogsError(t *te
s.AssertErrorsLogged(t, 1)
})
}

func TestSystemReset_WithThrottle_ThrottlesRequests(t *testing.T) {
const subscriptionCount = 5
const resetThrottle = 3
runTest(t, func(s *Session) {
c := s.Connect()
// Get subscriptions
for i := 1; i <= subscriptionCount; i++ {
subscribeToCustomResource(t, s, c, fmt.Sprintf("test.model.%d", i), resource{
typ: typeModel,
data: fmt.Sprintf(`{"id":%d}`, i),
})
}
// Send system reset
s.SystemEvent("reset", json.RawMessage(`{"resources":["test.>"]}`))
// Get throttled number of requests
mreqs := s.GetParallelRequests(t, resetThrottle)
requestCount := resetThrottle
// Assert no other requests are sent
for i := 1; i <= subscriptionCount; i++ {
c.AssertNoNATSRequest(t, fmt.Sprintf("test.model.%d", i))
}
// Respond to requests one by one
for len(mreqs) > 0 {
r := mreqs[0]
mreqs = mreqs[1:]
id := r.Subject[strings.LastIndexByte(r.Subject, '.')+1:]
r.RespondSuccess(json.RawMessage(`{"model":` + fmt.Sprintf(`{"id":%s}`, id) + `}`))
// If we still have remaining subscriptions not yet received
if requestCount < subscriptionCount {
// 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 i := 1; i <= subscriptionCount; i++ {
c.AssertNoNATSRequest(t, fmt.Sprintf("test.model.%d", i))
}
}
}

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

}

0 comments on commit ed3652f

Please sign in to comment.