Skip to content

Commit 7320b05

Browse files
committed
api: support IPROTO_WATCH_ONCE request type
Add support of `IPROTO_WATCH_ONCE` request type. It works only for Tarantool version >= 3.0.0. Closes #337
1 parent bd6aab9 commit 7320b05

File tree

6 files changed

+92
-3
lines changed

6 files changed

+92
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
2020
- Support `fetch_latest_metadata` option for crud requests with metadata (#335)
2121
- Support `noreturn` option for data change crud requests (#335)
2222
- Support `crud.schema` request (#336)
23+
- Support `IPROTO_WATCH_ONCE` request type for Tarantool version >= 3.0.0 (#337)
2324

2425
### Changed
2526

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
88
github.com/shopspring/decimal v1.3.1
99
github.com/stretchr/testify v1.7.1
10-
github.com/tarantool/go-iproto v0.1.0
10+
github.com/tarantool/go-iproto v0.1.1-0.20231025103136-cb7894473931
1111
github.com/tarantool/go-openssl v0.0.8-0.20231004103608-336ca939d2ca
1212
github.com/vmihailenco/msgpack/v5 v5.3.5
1313
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
1919
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2020
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
2121
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
22-
github.com/tarantool/go-iproto v0.1.0 h1:zHN9AA8LDawT+JBD0/Nxgr/bIsWkkpDzpcMuaNPSIAQ=
23-
github.com/tarantool/go-iproto v0.1.0/go.mod h1:LNCtdyZxojUed8SbOiYHoc3v9NvaZTB7p96hUySMlIo=
22+
github.com/tarantool/go-iproto v0.1.1-0.20231025103136-cb7894473931 h1:YrsRc1sDZ6HOZccvM2eJ3Nu2TMBq7NMZMsaT5KCu5qU=
23+
github.com/tarantool/go-iproto v0.1.1-0.20231025103136-cb7894473931/go.mod h1:LNCtdyZxojUed8SbOiYHoc3v9NvaZTB7p96hUySMlIo=
2424
github.com/tarantool/go-openssl v0.0.8-0.20231004103608-336ca939d2ca h1:oOrBh73tDDyooIXajfr+0pfnM+89404ClAhJpTTHI7E=
2525
github.com/tarantool/go-openssl v0.0.8-0.20231004103608-336ca939d2ca/go.mod h1:M7H4xYSbzqpW/ZRBMyH0eyqQBsnhAMfsYk5mv0yid7A=
2626
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=

request.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,3 +1354,36 @@ func (req *ExecuteRequest) Context(ctx context.Context) *ExecuteRequest {
13541354
req.ctx = ctx
13551355
return req
13561356
}
1357+
1358+
// WatchOnceRequest synchronously fetches the value currently associated with a
1359+
// specified notification key without subscribing to changes.
1360+
type WatchOnceRequest struct {
1361+
baseRequest
1362+
key string
1363+
ctx context.Context
1364+
}
1365+
1366+
// NewWatchOnceRequest returns a new watchOnceRequest.
1367+
func NewWatchOnceRequest(key string) *WatchOnceRequest {
1368+
req := new(WatchOnceRequest)
1369+
req.rtype = iproto.IPROTO_WATCH_ONCE
1370+
req.key = key
1371+
return req
1372+
}
1373+
1374+
// Body fills an msgpack.Encoder with the watchOnce request body.
1375+
func (req *WatchOnceRequest) Body(res SchemaResolver, enc *msgpack.Encoder) error {
1376+
if err := enc.EncodeMapLen(1); err != nil {
1377+
return err
1378+
}
1379+
if err := enc.EncodeUint(uint64(iproto.IPROTO_EVENT_KEY)); err != nil {
1380+
return err
1381+
}
1382+
return enc.Encode(req.key)
1383+
}
1384+
1385+
// Context sets a passed context to the request.
1386+
func (req *WatchOnceRequest) Context(ctx context.Context) *WatchOnceRequest {
1387+
req.ctx = ctx
1388+
return req
1389+
}

request_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ func TestRequestsTypes(t *testing.T) {
196196
{req: NewRollbackRequest(), rtype: iproto.IPROTO_ROLLBACK},
197197
{req: NewIdRequest(validProtocolInfo), rtype: iproto.IPROTO_ID},
198198
{req: NewBroadcastRequest(validKey), rtype: iproto.IPROTO_CALL},
199+
{req: NewWatchOnceRequest(validKey), rtype: iproto.IPROTO_WATCH_ONCE},
199200
}
200201

201202
for _, test := range tests {
@@ -231,6 +232,7 @@ func TestRequestsAsync(t *testing.T) {
231232
{req: NewRollbackRequest(), async: false},
232233
{req: NewIdRequest(validProtocolInfo), async: false},
233234
{req: NewBroadcastRequest(validKey), async: false},
235+
{req: NewWatchOnceRequest(validKey), async: false},
234236
}
235237

236238
for _, test := range tests {

tarantool_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,6 +2606,59 @@ func TestConnectionDoSelectRequest(t *testing.T) {
26062606
testConnectionDoSelectRequestCheck(t, resp, err, false, 10, 1010)
26072607
}
26082608

2609+
func TestConnectionDoWatchOnceRequest(t *testing.T) {
2610+
watchOnceNotSupported, err := test_helpers.IsTarantoolVersionLess(3, 0, 0)
2611+
if err != nil {
2612+
log.Fatalf("Could not check the Tarantool version")
2613+
}
2614+
if watchOnceNotSupported {
2615+
return
2616+
}
2617+
2618+
conn := test_helpers.ConnectWithValidation(t, server, opts)
2619+
defer conn.Close()
2620+
2621+
_, err = conn.Do(NewBroadcastRequest("hello").Value("world")).Get()
2622+
if err != nil {
2623+
t.Fatalf("Failed to create a broadcast : %s", err.Error())
2624+
}
2625+
2626+
resp, err := conn.Do(NewWatchOnceRequest("hello")).Get()
2627+
if err != nil {
2628+
t.Fatalf("Failed to WatchOnce: %s", err.Error())
2629+
}
2630+
if resp.Code != OkCode {
2631+
t.Errorf("Failed to WatchOnce: wrong code returned %d", resp.Code)
2632+
}
2633+
if len(resp.Data) < 1 || resp.Data[0] != "world" {
2634+
t.Errorf("Failed to WatchOnce: wrong value returned %v", resp.Data)
2635+
}
2636+
}
2637+
2638+
func TestConnectionDoWatchOnceOnEmptyKey(t *testing.T) {
2639+
watchOnceNotSupported, err := test_helpers.IsTarantoolVersionLess(3, 0, 0)
2640+
if err != nil {
2641+
log.Fatalf("Could not check the Tarantool version")
2642+
}
2643+
if watchOnceNotSupported {
2644+
return
2645+
}
2646+
2647+
conn := test_helpers.ConnectWithValidation(t, server, opts)
2648+
defer conn.Close()
2649+
2650+
resp, err := conn.Do(NewWatchOnceRequest("notexists!")).Get()
2651+
if err != nil {
2652+
t.Fatalf("Failed to WatchOnce: %s", err.Error())
2653+
}
2654+
if resp.Code != OkCode {
2655+
t.Errorf("Failed to WatchOnce: wrong code returned %d", resp.Code)
2656+
}
2657+
if len(resp.Data) > 0 {
2658+
t.Errorf("Failed to WatchOnce: wrong value returned %v", resp.Data)
2659+
}
2660+
}
2661+
26092662
func TestConnectionDoSelectRequest_fetch_pos(t *testing.T) {
26102663
test_helpers.SkipIfPaginationUnsupported(t)
26112664

0 commit comments

Comments
 (0)