Skip to content

Commit

Permalink
api: use iproto.Feature insted of ProtocolFeature
Browse files Browse the repository at this point in the history
Replaced the local `ProtocolFeature` type with the
`iproto.Feature`.

Closes #337
  • Loading branch information
DerekBum committed Oct 26, 2023
1 parent a34b863 commit ae39139
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 104 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
`pool.Connect` and `pool.Add` now accept context as first argument, which
user may cancel in process. If `pool.Connect` is canceled in progress, an
error will be returned. All created connections will be closed.
- `iproto.Feature` type now used instead of `ProtocolFeature` (#337)

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ func subscribeWatchChannel(conn *Connection, key string) (chan watchState, error
return st, nil
}

func isFeatureInSlice(expected ProtocolFeature, actualSlice []ProtocolFeature) bool {
func isFeatureInSlice(expected iproto.Feature, actualSlice []iproto.Feature) bool {
for _, actual := range actualSlice {
if expected == actual {
return true
Expand Down
7 changes: 4 additions & 3 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tarantool/go-iproto"

. "github.com/tarantool/go-tarantool/v2"
)
Expand All @@ -12,20 +13,20 @@ func TestOptsClonePreservesRequiredProtocolFeatures(t *testing.T) {
original := Opts{
RequiredProtocolInfo: ProtocolInfo{
Version: ProtocolVersion(100),
Features: []ProtocolFeature{ProtocolFeature(99), ProtocolFeature(100)},
Features: []iproto.Feature{iproto.Feature(99), iproto.Feature(100)},
},
}

origCopy := original.Clone()

original.RequiredProtocolInfo.Features[1] = ProtocolFeature(98)
original.RequiredProtocolInfo.Features[1] = iproto.Feature(98)

require.Equal(t,
origCopy,
Opts{
RequiredProtocolInfo: ProtocolInfo{
Version: ProtocolVersion(100),
Features: []ProtocolFeature{ProtocolFeature(99), ProtocolFeature(100)},
Features: []iproto.Feature{iproto.Feature(99), iproto.Feature(100)},
},
})
}
5 changes: 3 additions & 2 deletions dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tarantool/go-iproto"

"github.com/tarantool/go-tarantool/v2"
"github.com/tarantool/go-tarantool/v2/test_helpers"
Expand Down Expand Up @@ -72,7 +73,7 @@ func TestDialer_Dial_passedOpts(t *testing.T) {
RequiredProtocol: tarantool.ProtocolInfo{
Auth: tarantool.ChapSha1Auth,
Version: 33,
Features: []tarantool.ProtocolFeature{
Features: []iproto.Feature{
tarantool.ErrorExtensionFeature,
},
},
Expand Down Expand Up @@ -302,7 +303,7 @@ func TestConn_ProtocolInfo(t *testing.T) {
info := tarantool.ProtocolInfo{
Auth: tarantool.ChapSha1Auth,
Version: 33,
Features: []tarantool.ProtocolFeature{
Features: []iproto.Feature{
tarantool.ErrorExtensionFeature,
},
}
Expand Down
18 changes: 10 additions & 8 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"github.com/tarantool/go-iproto"

"github.com/tarantool/go-tarantool/v2"
"github.com/tarantool/go-tarantool/v2/test_helpers"
)
Expand Down Expand Up @@ -627,12 +629,12 @@ func ExampleProtocolVersion() {
}
// Output:
// Connector client protocol version: 6
// Connector client protocol feature: StreamsFeature
// Connector client protocol feature: TransactionsFeature
// Connector client protocol feature: ErrorExtensionFeature
// Connector client protocol feature: WatchersFeature
// Connector client protocol feature: PaginationFeature
// Connector client protocol feature: WatchOnceFeature
// Connector client protocol feature: IPROTO_FEATURE_STREAMS
// Connector client protocol feature: IPROTO_FEATURE_TRANSACTIONS
// Connector client protocol feature: IPROTO_FEATURE_ERROR_EXTENSION
// Connector client protocol feature: IPROTO_FEATURE_WATCHERS
// Connector client protocol feature: IPROTO_FEATURE_PAGINATION
// Connector client protocol feature: IPROTO_FEATURE_WATCH_ONCE
}

func getTestTxnOpts() tarantool.Opts {
Expand All @@ -641,7 +643,7 @@ func getTestTxnOpts() tarantool.Opts {
// Assert that server supports expected protocol features
txnOpts.RequiredProtocolInfo = tarantool.ProtocolInfo{
Version: tarantool.ProtocolVersion(1),
Features: []tarantool.ProtocolFeature{
Features: []iproto.Feature{
tarantool.StreamsFeature,
tarantool.TransactionsFeature,
},
Expand Down Expand Up @@ -1168,7 +1170,7 @@ func ExampleConnection_NewWatcher() {
Pass: "test",
// You need to require the feature to create a watcher.
RequiredProtocolInfo: tarantool.ProtocolInfo{
Features: []tarantool.ProtocolFeature{tarantool.WatchersFeature},
Features: []iproto.Feature{tarantool.WatchersFeature},
},
}
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
Expand Down
4 changes: 2 additions & 2 deletions pool/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,8 @@ func (p *ConnectionPool) NewWatcher(key string,
}
}
if !watchersRequired {
return nil, errors.New("the feature WatchersFeature must be " +
"required by connection options to create a watcher")
return nil, errors.New("the feature IPROTO_FEATURE_WATCHERS must " +
"be required by connection options to create a watcher")
}

watcher := &poolWatcher{
Expand Down
17 changes: 9 additions & 8 deletions pool/connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tarantool/go-iproto"
"github.com/vmihailenco/msgpack/v5"

"github.com/tarantool/go-tarantool/v2"
Expand Down Expand Up @@ -2832,7 +2833,7 @@ func TestConnectionPool_NewWatcher_noWatchersFeature(t *testing.T) {
roles := []bool{true, false, false, true, true}

opts := connOpts.Clone()
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{}
opts.RequiredProtocolInfo.Features = []iproto.Feature{}
err := test_helpers.SetClusterRO(servers, opts, roles)
require.Nilf(t, err, "fail to set roles for cluster")

Expand All @@ -2847,8 +2848,8 @@ func TestConnectionPool_NewWatcher_noWatchersFeature(t *testing.T) {
func(event tarantool.WatchEvent) {}, pool.ANY)
require.Nilf(t, watcher, "watcher must not be created")
require.NotNilf(t, err, "an error is expected")
expected := "the feature WatchersFeature must be required by connection " +
"options to create a watcher"
expected := "the feature IPROTO_FEATURE_WATCHERS must be required by " +
"connection options to create a watcher"
require.Equal(t, expected, err.Error())
}

Expand All @@ -2860,7 +2861,7 @@ func TestConnectionPool_NewWatcher_modes(t *testing.T) {
roles := []bool{true, false, false, true, true}

opts := connOpts.Clone()
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
opts.RequiredProtocolInfo.Features = []iproto.Feature{
tarantool.WatchersFeature,
}
err := test_helpers.SetClusterRO(servers, opts, roles)
Expand Down Expand Up @@ -2941,7 +2942,7 @@ func TestConnectionPool_NewWatcher_update(t *testing.T) {
roles := []bool{true, false, false, true, true}

opts := connOpts.Clone()
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
opts.RequiredProtocolInfo.Features = []iproto.Feature{
tarantool.WatchersFeature,
}
err := test_helpers.SetClusterRO(servers, opts, roles)
Expand Down Expand Up @@ -3030,7 +3031,7 @@ func TestWatcher_Unregister(t *testing.T) {
roles := []bool{true, false, false, true, true}

opts := connOpts.Clone()
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
opts.RequiredProtocolInfo.Features = []iproto.Feature{
tarantool.WatchersFeature,
}
err := test_helpers.SetClusterRO(servers, opts, roles)
Expand Down Expand Up @@ -3091,7 +3092,7 @@ func TestConnectionPool_NewWatcher_concurrent(t *testing.T) {
roles := []bool{true, false, false, true, true}

opts := connOpts.Clone()
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
opts.RequiredProtocolInfo.Features = []iproto.Feature{
tarantool.WatchersFeature,
}
err := test_helpers.SetClusterRO(servers, opts, roles)
Expand Down Expand Up @@ -3133,7 +3134,7 @@ func TestWatcher_Unregister_concurrent(t *testing.T) {
roles := []bool{true, false, false, true, true}

opts := connOpts.Clone()
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
opts.RequiredProtocolInfo.Features = []iproto.Feature{
tarantool.WatchersFeature,
}
err := test_helpers.SetClusterRO(servers, opts, roles)
Expand Down
20 changes: 15 additions & 5 deletions pool/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package pool_test

import (
"fmt"
"strings"
"time"

"github.com/tarantool/go-iproto"

"github.com/tarantool/go-tarantool/v2"
"github.com/tarantool/go-tarantool/v2/pool"
"github.com/tarantool/go-tarantool/v2/test_helpers"
Expand Down Expand Up @@ -92,7 +95,7 @@ func ExampleConnectionPool_NewWatcher() {
const value = "bar"

opts := connOpts.Clone()
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{
opts.RequiredProtocolInfo.Features = []iproto.Feature{
tarantool.WatchersFeature,
}

Expand Down Expand Up @@ -123,7 +126,7 @@ func ExampleConnectionPool_NewWatcher_noWatchersFeature() {
const key = "foo"

opts := connOpts.Clone()
opts.RequiredProtocolInfo.Features = []tarantool.ProtocolFeature{}
opts.RequiredProtocolInfo.Features = []iproto.Feature{}

connPool, err := examplePool(testRoles, connOpts)
if err != nil {
Expand All @@ -134,10 +137,17 @@ func ExampleConnectionPool_NewWatcher_noWatchersFeature() {
callback := func(event tarantool.WatchEvent) {}
watcher, err := connPool.NewWatcher(key, callback, pool.ANY)
fmt.Println(watcher)
fmt.Println(err)
if err != nil {
str := err.Error()
fmt.Println(strings.Trim(str[:56], " "))
fmt.Println(str[56:])
} else {
fmt.Println(err)
}
// Output:
// <nil>
// the feature WatchersFeature must be required by connection options to create a watcher
// the feature IPROTO_FEATURE_WATCHERS must be required by
// connection options to create a watcher
}

func getTestTxnOpts() tarantool.Opts {
Expand All @@ -146,7 +156,7 @@ func getTestTxnOpts() tarantool.Opts {
// Assert that server supports expected protocol features
txnOpts.RequiredProtocolInfo = tarantool.ProtocolInfo{
Version: tarantool.ProtocolVersion(1),
Features: []tarantool.ProtocolFeature{
Features: []iproto.Feature{
tarantool.StreamsFeature,
tarantool.TransactionsFeature,
},
Expand Down
43 changes: 9 additions & 34 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tarantool

import (
"context"
"fmt"

"github.com/tarantool/go-iproto"
"github.com/vmihailenco/msgpack/v5"
Expand All @@ -11,17 +10,14 @@ import (
// ProtocolVersion type stores Tarantool protocol version.
type ProtocolVersion uint64

// ProtocolVersion type stores a Tarantool protocol feature.
type ProtocolFeature iproto.Feature

// ProtocolInfo type aggregates Tarantool protocol version and features info.
type ProtocolInfo struct {
// Auth is an authentication method.
Auth Auth
// Version is the supported protocol version.
Version ProtocolVersion
// Features are supported protocol features.
Features []ProtocolFeature
Features []iproto.Feature
}

// Clone returns an exact copy of the ProtocolInfo object.
Expand All @@ -30,7 +26,7 @@ func (info ProtocolInfo) Clone() ProtocolInfo {
infoCopy := info

if info.Features != nil {
infoCopy.Features = make([]ProtocolFeature, len(info.Features))
infoCopy.Features = make([]iproto.Feature, len(info.Features))
copy(infoCopy.Features, info.Features)
}

Expand All @@ -39,44 +35,23 @@ func (info ProtocolInfo) Clone() ProtocolInfo {

const (
// StreamsFeature represents streams support (supported by connector).
StreamsFeature ProtocolFeature = 0
StreamsFeature iproto.Feature = 0
// TransactionsFeature represents interactive transactions support.
// (supported by connector).
TransactionsFeature ProtocolFeature = 1
TransactionsFeature iproto.Feature = 1
// ErrorExtensionFeature represents support of MP_ERROR objects over MessagePack
// (supported by connector).
ErrorExtensionFeature ProtocolFeature = 2
ErrorExtensionFeature iproto.Feature = 2
// WatchersFeature represents support of watchers
// (supported by connector).
WatchersFeature ProtocolFeature = 3
WatchersFeature iproto.Feature = 3
// PaginationFeature represents support of pagination
// (supported by connector).
PaginationFeature ProtocolFeature = 4
PaginationFeature iproto.Feature = 4
// WatchOnceFeature represents support of WatchOnce request types.
WatchOnceFeature ProtocolFeature = 6
WatchOnceFeature iproto.Feature = 6
)

// String returns the name of a Tarantool feature.
// If value X is not a known feature, returns "Unknown feature (code X)" string.
func (ftr ProtocolFeature) String() string {
switch ftr {
case StreamsFeature:
return "StreamsFeature"
case TransactionsFeature:
return "TransactionsFeature"
case ErrorExtensionFeature:
return "ErrorExtensionFeature"
case WatchersFeature:
return "WatchersFeature"
case PaginationFeature:
return "PaginationFeature"
case WatchOnceFeature:
return "WatchOnceFeature"
default:
return fmt.Sprintf("Unknown feature (code %d)", ftr)
}
}

var clientProtocolInfo ProtocolInfo = ProtocolInfo{
// Protocol version supported by connector. Version 3
// was introduced in Tarantool 2.10.0, version 4 was
Expand All @@ -96,7 +71,7 @@ var clientProtocolInfo ProtocolInfo = ProtocolInfo{
// (Tarantool 3.0.0), in connector since 2.0.0.
// WatchOnce request type was introduces in protocol version 6
// (Tarantool 3.0.0), in connector since 2.0.0.
Features: []ProtocolFeature{
Features: []iproto.Feature{
StreamsFeature,
TransactionsFeature,
ErrorExtensionFeature,
Expand Down
Loading

0 comments on commit ae39139

Please sign in to comment.