Skip to content

Commit

Permalink
cleanup response handler for BZPopMin
Browse files Browse the repository at this point in the history
Signed-off-by: jbrinkman <[email protected]>
  • Loading branch information
jbrinkman committed Dec 24, 2024
1 parent a46df52 commit d6f02e2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
49 changes: 28 additions & 21 deletions go/api/response_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,34 +373,41 @@ func handleStringSetResponse(response *C.struct_CommandResponse) (map[Result[str
return slice, nil
}

func describe(i interface{}) {
fmt.Printf("(%v, %T)\n", i, i)
}

func handleKeyWithMemberAndScoreResponse(response *C.struct_CommandResponse) (Result[KeyWithMemberAndScore], error) {
defer C.free_command_response(response)

describe(response)
typeErr := checkResponseType(response, C.Array, false)
if response == nil || response.response_type == uint32(C.Null) {
return CreateNilKeyWithMemberAndScoreResult(), nil
}

typeErr := checkResponseType(response, C.Array, true)
if typeErr != nil {
return CreateNilKeyWithMemberAndScoreResult(), typeErr
}
if response.array_value_len != 3 {
return CreateNilKeyWithMemberAndScoreResult(), &RequestError{"Unexpected number of elements in response"}
}

return CreateNilKeyWithMemberAndScoreResult(), nil
// m := make(map[Result[string]]Result[float64], response.array_value_len)
// for _, v := range unsafe.Slice(response.array_value, response.array_value_len) {
// key, err := convertCharArrayToString(v.map_key, true)
// if err != nil {
// return nil, err
// }
// value, err := handleDoubleResponse(v.map_value)
// if err != nil {
// return nil, err
// }
// m[key] = value
// }

// return m, nil
slice, err := parseArray(response)
if err != nil {
return CreateNilKeyWithMemberAndScoreResult(), err
}

arr := slice.([]interface{})
key, ok := arr[0].(string)
if !ok {
return CreateNilKeyWithMemberAndScoreResult(), &RequestError{"Unexpected type of key"}
}
member, ok := arr[1].(string)
if !ok {
return CreateNilKeyWithMemberAndScoreResult(), &RequestError{"Unexpected type of member"}
}
score := arr[2].(float64)
if !ok {
return CreateNilKeyWithMemberAndScoreResult(), &RequestError{"Unexpected type of score"}
}
kms := KeyWithMemberAndScore{key, member, score}
return CreateKeyWithMemberAndScoreResult(kms), nil
}

func handleScanResponse(
Expand Down
23 changes: 12 additions & 11 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3988,28 +3988,29 @@ func (suite *GlideTestSuite) TestBZPopMin() {
assert.Equal(suite.T(), int64(1), zaddResult2.Value())

// Pop minimum element from key1 and key2
bzpopminResult1, err := client.BZPopMin([]string{key1, key2}, float64(500*time.Millisecond))
bzpopminResult1, err := client.BZPopMin([]string{key1, key2}, float64(.5))
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), []interface{}{key1, "a", 1.0}, bzpopminResult1)
assert.Equal(suite.T(), api.KeyWithMemberAndScore{Key: key1, Member: "a", Score: 1.0}, bzpopminResult1.Value())

// Attempt to pop from non-existent key3
bzpopminResult2, err := client.BZPopMin([]string{key3}, float64(1*time.Second))
bzpopminResult2, err := client.BZPopMin([]string{key3}, float64(1))
assert.Nil(suite.T(), err)
assert.Nil(suite.T(), bzpopminResult2)
assert.True(suite.T(), bzpopminResult2.IsNil())

// Pop minimum element from key2
bzpopminResult3, err := client.BZPopMin([]string{key3, key2}, float64(500*time.Millisecond))
bzpopminResult3, err := client.BZPopMin([]string{key3, key2}, float64(.5))
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), []interface{}{key2, "c", 2.0}, bzpopminResult3)
assert.Equal(suite.T(), api.KeyWithMemberAndScore{Key: key2, Member: "c", Score: 2.0}, bzpopminResult3.Value())

// Set key3 to a non-sorted set value
setResult, err := client.Set(key3, "value")
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "OK", setResult)
assert.Equal(suite.T(), "OK", setResult.Value())

// // Attempt to pop from key3 which is not a sorted set
// _, err = client.BZPopMin([]string{key3}, float64(500*time.Millisecond))
// assert.Error(suite.T(), err)
// assert.IsType(suite.T(), RequestException{}, err)
// Attempt to pop from key3 which is not a sorted set
_, err = client.BZPopMin([]string{key3}, float64(.5))
if assert.Error(suite.T(), err) {
assert.IsType(suite.T(), &api.RequestError{}, err)
}
})
}

0 comments on commit d6f02e2

Please sign in to comment.