Skip to content
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

Go: Implement Time Command #2963

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1905,3 +1905,11 @@ func (client *baseClient) ZScanWithOptions(
}
return handleScanResponse(result)
}

func (client *baseClient) Time() ([]Result[string], error) {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
result, err := client.executeCommand(C.Time, []string{})
if err != nil {
return nil, err
}
return handleStringArrayResponse(result)
}
14 changes: 14 additions & 0 deletions go/api/server_management_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,18 @@ type ServerManagementCommands interface {
//
// [valkey.io]: https://valkey.io/commands/config-set/
ConfigSet(parameters map[string]string) (string, error)

// Returns the server time.
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
//
// Return value:
// The current server time as a String array with two elements:
// A UNIX TIME and the amount of microseconds already elapsed in the current second.
// The returned array is in a [UNIX TIME, Microseconds already elapsed] format.
//
// For example:
// result, err := client.Time()
// result: [{1737051660 false} {994688 false}]
//
// [valkey.io]: https://valkey.io/commands/time/
Time() ([]Result[string], error)
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
}
36 changes: 36 additions & 0 deletions go/integTest/standalone_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package integTest

import (
"fmt"
"strconv"
"strings"

"github.com/google/uuid"
Expand Down Expand Up @@ -273,3 +274,38 @@ func (suite *GlideTestSuite) TestSelect_SwitchBetweenDatabases() {
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), value2, result.Value())
}

func (suite *GlideTestSuite) TestTime_Success() {
client := suite.defaultClient()

results, err := client.Time()
assert.Nil(suite.T(), err)
assert.Len(suite.T(), results, 2)

timestamp := results[0].Value()
microseconds := results[1].Value()

// Validate Unix timestamp
if _, err := strconv.ParseInt(timestamp, 10, 64); err != nil {
suite.T().Fatalf("Expected a valid Unix timestamp but got %s", timestamp)
}

// Validate microseconds
microsecondsInt, err := strconv.ParseInt(microseconds, 10, 64)
if err != nil || microsecondsInt < 0 || microsecondsInt >= 1000000 {
suite.T().Fatalf("Expected a valid microseconds value between 0 and 999999 but got %s", microseconds)
}
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
}

func (suite *GlideTestSuite) TestTime_Error() {
client := suite.defaultClient()

// Disconnect the client or simulate an error condition
client.Close()

results, err := client.Time()

assert.NotNil(suite.T(), err)
assert.Nil(suite.T(), results)
assert.IsType(suite.T(), &api.ClosingError{}, err)
}
Loading