From 81f6eb5aeb086c4f3e3d785f8ff29105d2d3e6f6 Mon Sep 17 00:00:00 2001 From: jbrinkman Date: Fri, 20 Dec 2024 15:14:14 -0500 Subject: [PATCH] GO: Add BZPopMin command Signed-off-by: jbrinkman --- go/api/base_client.go | 9 ++++++ go/api/sorted_set_commands.go | 3 ++ go/integTest/shared_commands_test.go | 42 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/go/api/base_client.go b/go/api/base_client.go index 0e4aea3995..0c023a3ac2 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -1290,3 +1290,12 @@ func (client *baseClient) ZAddIncrWithOptions( return client.zAddIncrBase(key, incrOpts) } + +func (client *baseClient) BZPopMin(keys []string, timeoutSecs float64) ([]Result[string], error) { + result, err := client.executeCommand(C.BZPopMin, append(keys, utils.FloatToString(timeoutSecs))) + if err != nil { + return nil, err + } + + return handleStringArrayOrNullResponse(result) +} diff --git a/go/api/sorted_set_commands.go b/go/api/sorted_set_commands.go index 8eecf70120..22169e6864 100644 --- a/go/api/sorted_set_commands.go +++ b/go/api/sorted_set_commands.go @@ -88,4 +88,7 @@ type SortedSetCommands interface { // // [valkey.io]: https://valkey.io/commands/zadd/ ZAddIncrWithOptions(key string, member string, increment float64, opts *options.ZAddOptions) (Result[float64], error) + + // [valkey bzpopmin]: https://valkey.io/commands/bzpopmin/ + BZPopMin(keys []string, timeoutSecs float64) ([]Result[string], error) } diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index 972dfd8593..51cbce2fea 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -3873,3 +3873,45 @@ func (suite *GlideTestSuite) TestZAddAndZAddIncr() { assert.True(suite.T(), resIncr.IsNil()) }) } + +func (suite *GlideTestSuite) TestBZPopMin() { + suite.runWithDefaultClients(func(client api.BaseClient) { + key1 := "{listKey}-1-" + uuid.NewString() + // key2 := "{listKey}-2-" + uuid.NewString() + + // Add elements to key1 + zaddResult1, err := client.ZAdd(key1, map[string]float64{"a": 1.0, "b": 1.5}) + assert.Nil(suite.T(), err) + assert.Equal(suite.T(), int64(2), zaddResult1.Value()) + + // Add elements to key2 + // zaddResult2, err := client.ZAdd(key2, map[string]float64{"c": 2.0}) + // assert.Nil(suite.T(), err) + // assert.Equal(suite.T(), int64(1), zaddResult2.Value()) + + // // Pop minimum element from key1 and key2 + // bzpopminResult1, err := client.BZPopMin([]string{key1, key2}, 500*time.Millisecond) + // assert.NoError(suite.T(), err) + // assert.Equal(suite.T() []interface{}{key1, "a", 1.0}, bzpopminResult1) + + // // Attempt to pop from non-existent key3 + // bzpopminResult2, err := client.BZPopMin([]string{key3}, 1*time.Second) + // assert.NoError(suite.T() err) + // assert.Nil(suite.T() bzpopminResult2) + + // // Pop minimum element from key2 + // bzpopminResult3, err := client.BZPopMin([]string{key3, key2}, 500*time.Millisecond) + // assert.NoError(suite.T() err) + // assert.Equal(suite.T() []interface{}{key2, "c", 2.0}, bzpopminResult3) + + // // Set key3 to a non-sorted set value + // setResult, err := client.Set(key3, "value") + // assert.NoError(suite.T() err) + // assert.Equal(suite.T() "OK", setResult) + + // // Attempt to pop from key3 which is not a sorted set + // _, err = client.BZPopMin([]string{key3}, 500*time.Millisecond) + // assert.Error(suite.T() err) + // assert.IsType(suite.T() RequestException{}, err) + }) +}