Skip to content

Commit

Permalink
api: support IPROTO_WATCH_ONCE request type
Browse files Browse the repository at this point in the history
Add support of `IPROTO_WATCH_ONCE` request type.
It works only for Tarantool version >= 3.0.0.

Closes #337
  • Loading branch information
DerekBum committed Oct 25, 2023
1 parent bd6aab9 commit e6775db
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
- Support `fetch_latest_metadata` option for crud requests with metadata (#335)
- Support `noreturn` option for data change crud requests (#335)
- Support `crud.schema` request (#336)
- Support `IPROTO_WATCH_ONCE` request type for Tarantool version >= 3.0.0 (#337)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/shopspring/decimal v1.3.1
github.com/stretchr/testify v1.7.1
github.com/tarantool/go-iproto v0.1.0
github.com/tarantool/go-iproto v0.1.1-0.20231025103136-cb7894473931
github.com/tarantool/go-openssl v0.0.8-0.20231004103608-336ca939d2ca
github.com/vmihailenco/msgpack/v5 v5.3.5
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tarantool/go-iproto v0.1.0 h1:zHN9AA8LDawT+JBD0/Nxgr/bIsWkkpDzpcMuaNPSIAQ=
github.com/tarantool/go-iproto v0.1.0/go.mod h1:LNCtdyZxojUed8SbOiYHoc3v9NvaZTB7p96hUySMlIo=
github.com/tarantool/go-iproto v0.1.1-0.20231025103136-cb7894473931 h1:YrsRc1sDZ6HOZccvM2eJ3Nu2TMBq7NMZMsaT5KCu5qU=
github.com/tarantool/go-iproto v0.1.1-0.20231025103136-cb7894473931/go.mod h1:LNCtdyZxojUed8SbOiYHoc3v9NvaZTB7p96hUySMlIo=
github.com/tarantool/go-openssl v0.0.8-0.20231004103608-336ca939d2ca h1:oOrBh73tDDyooIXajfr+0pfnM+89404ClAhJpTTHI7E=
github.com/tarantool/go-openssl v0.0.8-0.20231004103608-336ca939d2ca/go.mod h1:M7H4xYSbzqpW/ZRBMyH0eyqQBsnhAMfsYk5mv0yid7A=
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
Expand Down
33 changes: 33 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,3 +1354,36 @@ func (req *ExecuteRequest) Context(ctx context.Context) *ExecuteRequest {
req.ctx = ctx
return req
}

// WatchOnceRequest synchronously fetches the value currently associated with a
// specified notification key without subscribing to changes.
type WatchOnceRequest struct {
baseRequest
key string
ctx context.Context
}

// NewWatchOnceRequest returns a new watchOnceRequest.
func NewWatchOnceRequest(key string) *WatchOnceRequest {
req := new(WatchOnceRequest)
req.rtype = iproto.IPROTO_WATCH_ONCE
req.key = key
return req
}

// Body fills an msgpack.Encoder with the watchOnce request body.
func (req *WatchOnceRequest) Body(res SchemaResolver, enc *msgpack.Encoder) error {
if err := enc.EncodeMapLen(1); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_EVENT_KEY)); err != nil {
return err
}
return enc.Encode(req.key)
}

// Context sets a passed context to the request.
func (req *WatchOnceRequest) Context(ctx context.Context) *WatchOnceRequest {
req.ctx = ctx
return req
}
2 changes: 2 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func TestRequestsTypes(t *testing.T) {
{req: NewRollbackRequest(), rtype: iproto.IPROTO_ROLLBACK},
{req: NewIdRequest(validProtocolInfo), rtype: iproto.IPROTO_ID},
{req: NewBroadcastRequest(validKey), rtype: iproto.IPROTO_CALL},
{req: NewWatchOnceRequest(validKey), rtype: iproto.IPROTO_WATCH_ONCE},
}

for _, test := range tests {
Expand Down Expand Up @@ -231,6 +232,7 @@ func TestRequestsAsync(t *testing.T) {
{req: NewRollbackRequest(), async: false},
{req: NewIdRequest(validProtocolInfo), async: false},
{req: NewBroadcastRequest(validKey), async: false},
{req: NewWatchOnceRequest(validKey), async: false},
}

for _, test := range tests {
Expand Down
53 changes: 53 additions & 0 deletions tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2606,6 +2606,59 @@ func TestConnectionDoSelectRequest(t *testing.T) {
testConnectionDoSelectRequestCheck(t, resp, err, false, 10, 1010)
}

func TestConnectionDoWatchOnceRequest(t *testing.T) {
watchOnceNotSupported, err := test_helpers.IsTarantoolVersionLess(3, 0, 0)
if err != nil {
log.Fatalf("Could not check the Tarantool version")
}
if watchOnceNotSupported {
return
}

conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

_, err = conn.Do(NewBroadcastRequest("hello").Value("world")).Get()
if err != nil {
t.Fatalf("Failed to create a broadcast : %s", err.Error())
}

resp, err := conn.Do(NewWatchOnceRequest("hello")).Get()
if err != nil {
t.Fatalf("Failed to WatchOnce: %s", err.Error())
}
if resp.Code != OkCode {
t.Errorf("Failed to WatchOnce: wrong code returned %d", resp.Code)
}
if len(resp.Data) < 1 || resp.Data[0] != "world" {
t.Errorf("Failed to WatchOnce: wrong value returned %v", resp.Data)
}
}

func TestConnectionDoWatchOnceOnEmptyKey(t *testing.T) {
watchOnceNotSupported, err := test_helpers.IsTarantoolVersionLess(3, 0, 0)
if err != nil {
log.Fatalf("Could not check the Tarantool version")
}
if watchOnceNotSupported {
return
}

conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

resp, err := conn.Do(NewWatchOnceRequest("hello")).Get()
if err != nil {
t.Fatalf("Failed to WatchOnce: %s", err.Error())
}
if resp.Code != OkCode {
t.Errorf("Failed to WatchOnce: wrong code returned %d", resp.Code)
}
if len(resp.Data) > 0 {
t.Errorf("Failed to WatchOnce: wrong value returned %v", resp.Data)
}
}

func TestConnectionDoSelectRequest_fetch_pos(t *testing.T) {
test_helpers.SkipIfPaginationUnsupported(t)

Expand Down

0 comments on commit e6775db

Please sign in to comment.