Skip to content

Commit

Permalink
Merge branch 'release/v1.7.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Jan 6, 2022
2 parents 07aff00 + d6fa427 commit 76fddf3
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 29 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ module github.com/resgateio/resgate
go 1.13

require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.4.2
github.com/jirenius/timerqueue v1.0.0
github.com/nats-io/nats.go v1.10.0
github.com/nats-io/nats-server/v2 v2.6.6 // indirect
github.com/nats-io/nats.go v1.13.1-0.20211122170419-d7c1d78a50fc
github.com/posener/wstest v1.2.0
github.com/rs/xid v1.3.0
google.golang.org/protobuf v1.27.1 // indirect
)
73 changes: 58 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,32 +1,75 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/jirenius/timerqueue v1.0.0 h1:TgcUQlrxKBBHYmStXPzLdMPJFfmqkWZZ1s7BA5G1d9E=
github.com/jirenius/timerqueue v1.0.0/go.mod h1:pUEjy16BUruJMjLIsjWvWQh9Bu9CSXCIfGADZf37WIk=
github.com/nats-io/jwt v0.3.2 h1:+RB5hMpXUUA2dfxuhBTEkMOrYmM+gKIZYS1KjSostMI=
github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU=
github.com/nats-io/nats.go v1.10.0 h1:L8qnKaofSfNFbXg0C5F71LdjPRnmQwSsA4ukmkt1TvY=
github.com/nats-io/nats.go v1.10.0/go.mod h1:AjGArbfyR50+afOUotNX2Xs5SYHf+CoOa5HH1eEl2HE=
github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nkeys v0.1.4 h1:aEsHIssIk6ETN5m2/MD8Y4B2X7FfXrBAUdkyRvbVYzA=
github.com/nats-io/nkeys v0.1.4/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1tqEu/s=
github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s=
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/minio/highwayhash v1.0.1 h1:dZ6IIu8Z14VlC0VpfKofAhCy74wu/Qb5gcn52yWoz/0=
github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
github.com/nats-io/jwt/v2 v2.2.0 h1:Yg/4WFK6vsqMudRg91eBb7Dh6XeVcDMPHycDE8CfltE=
github.com/nats-io/jwt/v2 v2.2.0/go.mod h1:0tqz9Hlu6bCBFLWAASKhE5vUA4c24L9KPUUgvwumE/k=
github.com/nats-io/nats-server/v2 v2.6.6 h1:t6LcqHuMXhylQ/j8078zDUSc7sE0FBMcN8jwObAriTc=
github.com/nats-io/nats-server/v2 v2.6.6/go.mod h1:9sdEkBhyZMQG1M9TevnlYUwMusRACn2vlgOeqoHKwVo=
github.com/nats-io/nats.go v1.13.1-0.20211122170419-d7c1d78a50fc h1:SHr4MUUZJ/fAC0uSm2OzWOJYsHpapmR86mpw7q1qPXU=
github.com/nats-io/nats.go v1.13.1-0.20211122170419-d7c1d78a50fc/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8=
github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/wstest v1.2.0 h1:PAY0cRybxOjh0yqSDCrlAGUwtx+GNKpuUfid/08pv48=
github.com/posener/wstest v1.2.0/go.mod h1:GkplCx9zskpudjrMp23LyZHrSonab0aZzh2x0ACGRbU=
github.com/rs/xid v1.3.0 h1:6NjYksEUlhurdVehpc7S7dk6DAmcKv8V9gG0FsVN2U4=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 h1:3zb4D3T4G8jdExgVU/95+vQXfpEPiMdCaZgmGVxjNHM=
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
6 changes: 6 additions & 0 deletions nats/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ func (c *Client) listener(ch chan *nats.Msg, stopped chan struct{}) {

if ok {
if rc.isReq {
// Handle no responders header, if available
if len(msg.Data) == 0 && msg.Header.Get("Status") == "503" {
c.Tracef("x=> (%s) No responders", inboxSubstr(msg.Subject))
rc.f("", nil, mq.ErrNoResponders)
continue
}
c.Tracef("==> (%s): %s", inboxSubstr(msg.Subject), msg.Data)
} else {
c.Tracef("=>> %s: %s", msg.Subject, msg.Data)
Expand Down
2 changes: 1 addition & 1 deletion server/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "time"

const (
// Version is the current version for the server.
Version = "1.7.4"
Version = "1.7.5"

// ProtocolVersion is the implemented RES protocol version.
ProtocolVersion = "1.2.2"
Expand Down
4 changes: 4 additions & 0 deletions server/mq/mq.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type Client interface {
SetClosedHandler(cb func(error))
}

// ErrNoResponders is the error the client should pass to the Response
// when a call to SendRequest has no reponders.
var ErrNoResponders = reserr.ErrNotFound

// ErrRequestTimeout is the error the client should pass to the Response
// when a call to SendRequest times out
var ErrRequestTimeout = reserr.ErrTimeout
Expand Down
2 changes: 2 additions & 0 deletions server/reserr/reserr.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
CodeInvalidRequest = "system.invalidRequest"
CodeUnsupportedProtocol = "system.unsupportedProtocol"
CodeSubjectTooLong = "system.subjectTooLong"
CodeDeleted = "system.deleted"
// HTTP only error codes
CodeBadRequest = "system.badRequest"
CodeMethodNotAllowed = "system.methodNotAllowed"
Expand All @@ -70,6 +71,7 @@ var (
ErrInvalidRequest = &Error{Code: CodeInvalidRequest, Message: "Invalid request"}
ErrUnsupportedProtocol = &Error{Code: CodeUnsupportedProtocol, Message: "Unsupported protocol"}
ErrSubjectTooLong = &Error{Code: CodeSubjectTooLong, Message: "Subject too long"}
ErrDeleted = &Error{Code: CodeDeleted, Message: "Deleted"}
// HTTP only errors
ErrBadRequest = &Error{Code: CodeBadRequest, Message: "Bad request"}
ErrMethodNotAllowed = &Error{Code: CodeMethodNotAllowed, Message: "Method not allowed"}
Expand Down
16 changes: 13 additions & 3 deletions server/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,8 @@ func (s *Subscription) processCollectionEvent(event *rescache.ResourceEvent) {

case "delete":
s.state = stateDeleted
fallthrough
s.c.Send(rpc.NewEvent(s.rid, event.Event, event.Payload))
s.unsubscribeDirect(reserr.ErrDeleted)
default:
s.c.Send(rpc.NewEvent(s.rid, event.Event, event.Payload))
}
Expand Down Expand Up @@ -728,7 +729,8 @@ func (s *Subscription) processModelEvent(event *rescache.ResourceEvent) {
}
case "delete":
s.state = stateDeleted
fallthrough
s.c.Send(rpc.NewEvent(s.rid, event.Event, event.Payload))
s.unsubscribeDirect(reserr.ErrDeleted)
default:
s.c.Send(rpc.NewEvent(s.rid, event.Event, event.Payload))
}
Expand Down Expand Up @@ -759,8 +761,16 @@ func (s *Subscription) handleReaccess(t *rescache.Throttle) {
func (s *Subscription) validateAccess(a *rescache.Access) {
err := a.CanGet()
if err != nil {
s.unsubscribeDirect(reserr.RESError(err))
}
}

// unsubscribeDirect removes any direct subscription of the resource and sends
// an unsubscribe event if any direct subscriptions existed.
func (s *Subscription) unsubscribeDirect(reason *reserr.Error) {
if s.direct > 0 {
s.c.Unsubscribe(s, true, s.direct, true)
s.c.Send(rpc.NewEvent(s.rid, "unsubscribe", rpc.UnsubscribeEvent{Reason: reserr.RESError(err)}))
s.c.Send(rpc.NewEvent(s.rid, "unsubscribe", rpc.UnsubscribeEvent{Reason: reason}))
}
}

Expand Down
3 changes: 1 addition & 2 deletions test/10reaccess_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func TestReaccessEventTriggersAccessCallOnSubscribedResources(t *testing.T) {
func TestReaccessEventTriggersUnsubscribeOnDeniedAccessCall(t *testing.T) {
runTest(t, func(s *Session) {
event := json.RawMessage(`{"foo":"bar"}`)
reasonAccessDenied := json.RawMessage(`{"reason":{"code":"system.accessDenied","message":"Access denied"}}`)

c := s.Connect()

Expand All @@ -72,7 +71,7 @@ func TestReaccessEventTriggersUnsubscribeOnDeniedAccessCall(t *testing.T) {
s.GetRequest(t).AssertSubject(t, "access.test.model.parent").RespondSuccess(json.RawMessage(`{"get":false}`))

// Validate unsubscribe events are sent to client
c.GetEvent(t).AssertEventName(t, "test.model.parent.unsubscribe").AssertData(t, reasonAccessDenied)
c.GetEvent(t).AssertEventName(t, "test.model.parent.unsubscribe").AssertData(t, mock.UnsubscribeReasonAccessDenied)

// Send event on model and validate client event
s.ResourceEvent("test.model", "custom", event)
Expand Down
6 changes: 4 additions & 2 deletions test/11system_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ func TestSystemReset_NotFoundResponseOnModel_GeneratesDeleteEvent(t *testing.T)
// Respond to get request with system.notFound error
s.GetRequest(t).AssertSubject(t, "get.test.model").RespondError(reserr.ErrNotFound)
// Validate delete event is sent to client
c.GetEvent(t).AssertEventName(t, "test.model.delete").AssertData(t, nil)
c.GetEvent(t).Equals(t, "test.model.delete", nil)
c.GetEvent(t).Equals(t, "test.model.unsubscribe", mock.UnsubscribeReasonDeleted)
// Validate subsequent events are not sent to client
s.ResourceEvent("test.model", "custom", common.CustomEvent())
c.AssertNoEvent(t, "test.model")
Expand All @@ -255,7 +256,8 @@ func TestSystemReset_NotFoundResponseOnCollection_GeneratesDeleteEvent(t *testin
// Respond to get request with system.notFound error
s.GetRequest(t).AssertSubject(t, "get.test.collection").RespondError(reserr.ErrNotFound)
// Validate delete event is sent to client
c.GetEvent(t).AssertEventName(t, "test.collection.delete").AssertData(t, nil)
c.GetEvent(t).Equals(t, "test.collection.delete", nil)
c.GetEvent(t).Equals(t, "test.collection.unsubscribe", mock.UnsubscribeReasonDeleted)
// Validate subsequent events are not sent to client
s.ResourceEvent("test.collection", "custom", common.CustomEvent())
c.AssertNoEvent(t, "test.collection")
Expand Down
6 changes: 4 additions & 2 deletions test/13query_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,8 @@ func TestQueryEvent_DeleteEventOnModel_DeletesFromCache(t *testing.T) {
// Respond to query request with an error
s.GetRequest(t).RespondSuccess(json.RawMessage(`{"events":[{"event":"delete"},{"event":"change","data":{"values":{"string":"bar","int":-12}}}]}`))
// Validate only delete event is sent to client
c.GetEvent(t).AssertEventName(t, "test.model?q=foo&f=bar.delete").AssertData(t, nil)
c.GetEvent(t).Equals(t, "test.model?q=foo&f=bar.delete", nil)
c.GetEvent(t).Equals(t, "test.model?q=foo&f=bar.unsubscribe", mock.UnsubscribeReasonDeleted)
c.AssertNoEvent(t, "test.model")
// Validate subsequent query events does not send request
s.ResourceEvent("test.model", "query", json.RawMessage(`{"subject":"_EVENT_02_"}`))
Expand All @@ -713,7 +714,8 @@ func TestQueryEvent_DeleteEventOnCollection_DeletesFromCache(t *testing.T) {
// Respond to query request with an error
s.GetRequest(t).RespondSuccess(json.RawMessage(`{"events":[{"event":"delete"},{"event":"add","data":{"idx":1,"value":"bar"}}]}`))
// Validate only delete event is sent to client
c.GetEvent(t).AssertEventName(t, "test.collection?q=foo&f=bar.delete").AssertData(t, nil)
c.GetEvent(t).Equals(t, "test.collection?q=foo&f=bar.delete", nil)
c.GetEvent(t).Equals(t, "test.collection?q=foo&f=bar.unsubscribe", mock.UnsubscribeReasonDeleted)
c.AssertNoEvent(t, "test.collection")
// Validate subsequent query events does not send request
s.ResourceEvent("test.collection", "query", json.RawMessage(`{"subject":"_EVENT_02_"}`))
Expand Down
10 changes: 7 additions & 3 deletions test/29delete_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ func TestDeleteEvent_OnModel_SentToClient(t *testing.T) {
// Send delete event
s.ResourceEvent("test.model", "delete", nil)

// Validate the delete event is sent to client
// Validate the delete and unsubscribe event is sent to client
c.GetEvent(t).Equals(t, "test.model.delete", nil)
c.GetEvent(t).Equals(t, "test.model.unsubscribe", mock.UnsubscribeReasonDeleted)
})
}

Expand All @@ -28,6 +29,7 @@ func TestDeleteEvent_OnCollection_SentToClient(t *testing.T) {

// Validate the delete event is sent to client
c.GetEvent(t).Equals(t, "test.collection.delete", nil)
c.GetEvent(t).Equals(t, "test.collection.unsubscribe", mock.UnsubscribeReasonDeleted)
})
}

Expand All @@ -38,6 +40,7 @@ func TestDeleteEvent_AndCustomEventOnModel_CustomEventNotSentToClient(t *testing
// Send delete event
s.ResourceEvent("test.model", "delete", nil)
c.GetEvent(t).Equals(t, "test.model.delete", nil)
c.GetEvent(t).Equals(t, "test.model.unsubscribe", mock.UnsubscribeReasonDeleted)
// Send custom event on model and validate no event
s.ResourceEvent("test.model", "custom", common.CustomEvent())
c.AssertNoEvent(t, "test.model")
Expand All @@ -51,6 +54,7 @@ func TestDeleteEvent_AndCustomEventOnCollection_CustomEventNotSentToClient(t *te
// Send delete event
s.ResourceEvent("test.collection", "delete", nil)
c.GetEvent(t).Equals(t, "test.collection.delete", nil)
c.GetEvent(t).Equals(t, "test.collection.unsubscribe", mock.UnsubscribeReasonDeleted)
// Send custom event on collection and validate no event
s.ResourceEvent("test.collection", "custom", common.CustomEvent())
c.AssertNoEvent(t, "test.collection")
Expand Down Expand Up @@ -91,6 +95,7 @@ func TestDeleteEvent_FollowedBySubscribe_IsNotCached(t *testing.T) {
s.ResourceEvent("test.model", "delete", nil)
// Validate the delete event is sent to client
c1.GetEvent(t).Equals(t, "test.model.delete", nil)
c1.GetEvent(t).Equals(t, "test.model.unsubscribe", mock.UnsubscribeReasonDeleted)

// Subscribe with second client
subscribeToTestModel(t, s, c2)
Expand All @@ -111,12 +116,11 @@ func TestDeleteEvent_FollowedByResubscribe_IsNotCached(t *testing.T) {
s.ResourceEvent("test.model", "delete", nil)
// Validate the delete event is sent to client
c.GetEvent(t).Equals(t, "test.model.delete", nil)
c.GetEvent(t).Equals(t, "test.model.unsubscribe", mock.UnsubscribeReasonDeleted)
// Send custom event and assert event not sent to client
s.ResourceEvent("test.model", "custom", common.CustomEvent())
c.AssertNoEvent(t, "test.model")
// Resubscribe
creq := c.Request("unsubscribe.test.model", nil)
creq.GetResponse(t)
subscribeToTestModel(t, s, c)
// Send custom event and assert event is sent to client
s.ResourceEvent("test.model", "custom", common.CustomEvent())
Expand Down
10 changes: 10 additions & 0 deletions test/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import (
"github.com/resgateio/resgate/server/reserr"
)

type mockData struct {
UnsubscribeReasonAccessDenied json.RawMessage
UnsubscribeReasonDeleted json.RawMessage
}

var mock = mockData{
UnsubscribeReasonAccessDenied: json.RawMessage(`{"reason":{"code":"system.accessDenied","message":"Access denied"}}`),
UnsubscribeReasonDeleted: json.RawMessage(`{"reason":{"code":"system.deleted","message":"Deleted"}}`),
}

// The following cyclic groups exist
// a -> a

Expand Down

0 comments on commit 76fddf3

Please sign in to comment.