Skip to content

Commit

Permalink
GO: Add BZPopMin command
Browse files Browse the repository at this point in the history
Signed-off-by: jbrinkman <[email protected]>
  • Loading branch information
jbrinkman committed Dec 20, 2024
1 parent 24a2dd0 commit 81f6eb5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
9 changes: 9 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 3 additions & 0 deletions go/api/sorted_set_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
42 changes: 42 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 81f6eb5

Please sign in to comment.