forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature/box: first box and box.info implementation #414
Open
KaymeKaydex
wants to merge
1
commit into
tarantool:master
Choose a base branch
from
KaymeKaydex:feature/box
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package box | ||
|
||
import ( | ||
"github.com/tarantool/go-tarantool/v2" | ||
) | ||
|
||
// Box defines an interface for interacting with a Tarantool instance. | ||
// It includes the Info method, which retrieves instance information. | ||
type Box interface { | ||
Info() (Info, error) // Retrieves detailed information about the Tarantool instance. | ||
} | ||
|
||
// box is a concrete implementation of the Box interface. | ||
// It holds a connection to the Tarantool instance via the Doer interface. | ||
type box struct { | ||
conn tarantool.Doer // Connection interface for interacting with Tarantool. | ||
} | ||
|
||
// By returns a new instance of the box structure, which implements the Box interface. | ||
func By(conn tarantool.Doer) Box { | ||
return &box{ | ||
conn: conn, // Assigns the provided Tarantool connection. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package box_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tarantool/go-tarantool/v2/box" | ||
) | ||
|
||
func TestBy(t *testing.T) { | ||
// We expect a panic because we are passing a nil connection (nil Doer) to the By function. | ||
// The library does not control this zone, and the nil connection would cause a runtime error | ||
// when we attempt to call methods (like Info) on it. | ||
// This test ensures that such an invalid state is correctly handled by causing a panic, | ||
// as it's outside of the library's responsibility. | ||
require.Panics(t, func() { | ||
// Create a box instance with a nil connection. This should lead to a panic later. | ||
b := box.By(nil) | ||
|
||
// Ensure the box instance is not nil (which it shouldn't be), but this is not meaningful | ||
// since we will panic when we call the Info method with the nil connection. | ||
require.NotNil(t, b) | ||
|
||
// Calling Info on a box with a nil connection will result in a panic, since the underlying | ||
// connection (Doer) cannot perform the requested action (it's nil). | ||
_, _ = b.Info() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package box | ||
|
||
import "github.com/tarantool/go-tarantool/v2" | ||
|
||
// ClusterInfo represents information about the cluster. | ||
// It contains the unique identifier (UUID) of the cluster. | ||
type ClusterInfo struct { | ||
UUID string `msgpack:"uuid"` | ||
} | ||
|
||
// Info represents detailed information about the Tarantool instance. | ||
// It includes version, node ID, read-only status, process ID, cluster information, and more. | ||
type Info struct { | ||
// The Version of the Tarantool instance. | ||
Version string `msgpack:"version"` | ||
// The node ID (nullable). | ||
ID *int `msgpack:"id"` | ||
// Read-only (RO) status of the instance. | ||
RO bool `msgpack:"ro"` | ||
// UUID - Unique identifier of the instance. | ||
UUID string `msgpack:"uuid"` | ||
// Process ID of the instance. | ||
PID int `msgpack:"pid"` | ||
// Status - Current status of the instance (e.g., running, unconfigured). | ||
Status string `msgpack:"status"` | ||
// LSN - Log sequence number of the instance. | ||
LSN uint64 `msgpack:"lsn"` | ||
// Cluster information, including cluster UUID. | ||
Cluster ClusterInfo `msgpack:"cluster"` | ||
} | ||
|
||
// Info retrieves the current information of the Tarantool instance. | ||
// It calls the "box.info" function and parses the result into the Info structure. | ||
func (b *box) Info() (Info, error) { | ||
var info Info | ||
|
||
// Call "box.info" to get instance information from Tarantool. | ||
fut := b.conn.Do(tarantool.NewCallRequest("box.info")) | ||
|
||
// Parse the result into the Info structure. | ||
err := fut.GetTyped(&[]interface{}{&info}) | ||
if err != nil { | ||
return Info{}, err | ||
} | ||
|
||
// Return the parsed info and any potential error. | ||
return info, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package box_test | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tarantool/go-tarantool/v2" | ||
"github.com/tarantool/go-tarantool/v2/box" | ||
"github.com/tarantool/go-tarantool/v2/test_helpers" | ||
) | ||
|
||
var server = "127.0.0.1:3013" | ||
var dialer = tarantool.NetDialer{ | ||
Address: server, | ||
User: "test", | ||
Password: "test", | ||
} | ||
|
||
func TestBox_Info(t *testing.T) { | ||
ctx := context.TODO() | ||
|
||
conn, err := tarantool.Connect(ctx, dialer, tarantool.Opts{}) | ||
require.NoError(t, err) | ||
|
||
info, err := box.By(conn).Info() | ||
require.NoError(t, err) | ||
|
||
// check all fields run correctly | ||
_, err = uuid.Parse(info.UUID) | ||
require.NoErrorf(t, err, "validate instance uuid is valid") | ||
|
||
require.NotEmpty(t, info.Version) | ||
// check that pid parsed correctly | ||
require.NotEqual(t, info.PID, 0) | ||
|
||
} | ||
|
||
func runTestMain(m *testing.M) int { | ||
instance, err := test_helpers.StartTarantool(test_helpers.StartOpts{ | ||
Dialer: dialer, | ||
InitScript: "testdata/config.lua", | ||
Listen: server, | ||
WaitStart: 100 * time.Millisecond, | ||
ConnectRetry: 10, | ||
RetryTimeout: 500 * time.Millisecond, | ||
}) | ||
defer test_helpers.StopTarantoolWithCleanup(instance) | ||
|
||
if err != nil { | ||
log.Printf("Failed to prepare test Tarantool: %s", err) | ||
return 1 | ||
} | ||
|
||
return m.Run() | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
code := runTestMain(m) | ||
os.Exit(code) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- Do not set listen for now so connector won't be | ||
-- able to send requests until everything is configured. | ||
box.cfg{ | ||
work_dir = os.getenv("TEST_TNT_WORK_DIR"), | ||
} | ||
|
||
box.schema.user.create('test', { password = 'test' , if_not_exists = true }) | ||
box.schema.user.grant('test', 'execute', 'universe', nil, { if_not_exists = true }) | ||
|
||
-- Set listen only when every other thing is configured. | ||
box.cfg{ | ||
listen = os.getenv("TEST_TNT_LISTEN"), | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the Tarantool 3 the field has name
replicaset
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there should be a custom decoder for this type or extend the type to avoid problems, this implementation is rather minimal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, that is, I propose in this case, for example, to remove this field altogether, leaving a minimal non-conflicting implementation