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

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
297caea
Implemented Time Command
niharikabhavaraju Jan 16, 2025
1177732
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 17, 2025
1244741
Fixed code review changes
niharikabhavaraju Jan 17, 2025
579f0f3
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 19, 2025
3716426
Added Time command (cluster)
niharikabhavaraju Jan 19, 2025
b13f411
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 20, 2025
d20ea15
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 22, 2025
bcc00a7
Fixed code review comments
niharikabhavaraju Jan 22, 2025
92886ba
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 27, 2025
2ce5dbc
Fixed code review comments for multi cluster value
niharikabhavaraju Jan 27, 2025
0e04a4e
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 27, 2025
5364841
Fixed linting error
niharikabhavaraju Jan 27, 2025
fb97914
Merge branch 'niharika-timecommand' of github.com:niharikabhavaraju/v…
niharikabhavaraju Jan 27, 2025
ff976a7
Fixed code review changes
niharikabhavaraju Jan 28, 2025
6a64db9
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 28, 2025
355b339
Fix linting error
niharikabhavaraju Jan 28, 2025
6e55e05
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 28, 2025
27aa766
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 29, 2025
55d5d83
Merge branch 'niharika-timecommand' of github.com:niharikabhavaraju/v…
niharikabhavaraju Jan 29, 2025
88c3e2d
Fixed code review comments and failing tests
niharikabhavaraju Jan 29, 2025
4ee885f
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 30, 2025
e1c22a0
Merge branch 'main' into niharika-timecommand
niharikabhavaraju Jan 30, 2025
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
21 changes: 21 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2032,3 +2032,24 @@ func (client *baseClient) ObjectEncoding(key string) (Result[string], error) {
}
return handleStringOrNilResponse(result)
}

// Returns the server time.
//
// 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} {994688}]
//
// [valkey.io]: https://valkey.io/commands/time/
func (client *baseClient) Time() ([]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 handleRawStringArrayResponse(result)
}
26 changes: 26 additions & 0 deletions go/api/response_handlers.go
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,29 @@ func handleXPendingDetailResponse(response *C.struct_CommandResponse) ([]XPendin

return pendingDetails, nil
}

func handleRawStringArrayResponse(response *C.struct_CommandResponse) ([]string, error) {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
defer C.free_command_response(response)

typeErr := checkResponseType(response, C.Array, false)
if typeErr != nil {
return nil, typeErr
}

slice := make([]string, 0, response.array_value_len)
for _, v := range unsafe.Slice(response.array_value, response.array_value_len) {
err := checkResponseType(&v, C.String, false)
if err != nil {
return nil, err
}

if v.string_value == nil {
return nil, &RequestError{"Unexpected nil string in array"}
}

byteSlice := C.GoBytes(unsafe.Pointer(v.string_value), C.int(int64(v.string_value_len)))
slice = append(slice, string(byteSlice))
}

return slice, nil
}
2 changes: 2 additions & 0 deletions go/api/server_management_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ type ServerManagementCommands interface {
//
// [valkey.io]: https://valkey.io/commands/config-set/
ConfigSet(parameters map[string]string) (string, error)

Time() ([]string, error)
}
33 changes: 33 additions & 0 deletions go/integTest/standalone_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package integTest

import (
"fmt"
"strconv"
"strings"
"time"

"github.com/google/uuid"
"github.com/valkey-io/valkey-glide/go/glide/api"
Expand Down Expand Up @@ -273,3 +275,34 @@ 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)

now := time.Now().Unix() - 1

timestamp, err := strconv.ParseInt(results[0], 10, 64)
assert.Nil(suite.T(), err)
assert.Greater(suite.T(), timestamp, now)

microseconds, err := strconv.ParseInt(results[1], 10, 64)
assert.Nil(suite.T(), err)
assert.Less(suite.T(), microseconds, int64(1000000))
}

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