Skip to content

Commit

Permalink
migration: Fix uint64 convertions
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-jukovec committed May 19, 2022
1 parent 5d08b3f commit 29ae9a6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
30 changes: 29 additions & 1 deletion queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,40 @@ func (q *queue) produce(cmd string, params ...interface{}) (string, error) {
return qd.task.status, nil
}

func convertUint64(v interface{}) (result uint64, err error) {
switch v := v.(type) {
case uint:
result = uint64(v)
case uint8:
result = uint64(v)
case uint16:
result = uint64(v)
case uint32:
result = uint64(v)
case uint64:
result = uint64(v)
case int:
result = uint64(v)
case int8:
result = uint64(v)
case int16:
result = uint64(v)
case int32:
result = uint64(v)
case int64:
result = uint64(v)
default:
err = fmt.Errorf("Non-number value %T", v)
}
return
}

// Reverse the effect of a bury request on one or more tasks.
func (q *queue) Kick(count uint64) (uint64, error) {
resp, err := q.conn.Call(q.cmds.kick, []interface{}{count})
var id uint64
if err == nil {
id = resp.Data[0].([]interface{})[0].(uint64)
id, err = convertUint64(resp.Data[0].([]interface{})[0])
}
return id, err
}
Expand Down
45 changes: 36 additions & 9 deletions tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ func (m *Member) decodeMsgpackImpl(d *Decoder) error {
return nil
}

func convertUint64(v interface{}) (result uint64, err error) {
switch v := v.(type) {
case uint:
result = uint64(v)
case uint8:
result = uint64(v)
case uint16:
result = uint64(v)
case uint32:
result = uint64(v)
case uint64:
result = uint64(v)
case int:
result = uint64(v)
case int8:
result = uint64(v)
case int16:
result = uint64(v)
case int32:
result = uint64(v)
case int64:
result = uint64(v)
default:
err = fmt.Errorf("Non-number value %T", v)
}
return
}

var server = "127.0.0.1:3013"
var spaceNo = uint32(512)
var spaceName = "test"
Expand Down Expand Up @@ -454,7 +482,7 @@ func TestClient(t *testing.T) {
if len(tpl) != 3 {
t.Errorf("Unexpected body of Insert (tuple len)")
}
if id, ok := tpl[0].(uint64); !ok || id != 1 {
if id, err := convertUint64(tpl[0]); err != nil || id != 1 {
t.Errorf("Unexpected body of Insert (0)")
}
if h, ok := tpl[1].(string); !ok || h != "hello" {
Expand Down Expand Up @@ -487,7 +515,7 @@ func TestClient(t *testing.T) {
if len(tpl) != 3 {
t.Errorf("Unexpected body of Delete (tuple len)")
}
if id, ok := tpl[0].(uint64); !ok || id != 1 {
if id, err := convertUint64(tpl[0]); err != nil || id != 1 {
t.Errorf("Unexpected body of Delete (0)")
}
if h, ok := tpl[1].(string); !ok || h != "hello" {
Expand Down Expand Up @@ -529,7 +557,7 @@ func TestClient(t *testing.T) {
if len(tpl) != 3 {
t.Errorf("Unexpected body of Replace (tuple len)")
}
if id, ok := tpl[0].(uint64); !ok || id != 2 {
if id, err := convertUint64(tpl[0]); err != nil || id != 2 {
t.Errorf("Unexpected body of Replace (0)")
}
if h, ok := tpl[1].(string); !ok || h != "hi" {
Expand All @@ -554,7 +582,7 @@ func TestClient(t *testing.T) {
if len(tpl) != 2 {
t.Errorf("Unexpected body of Update (tuple len)")
}
if id, ok := tpl[0].(uint64); !ok || id != 2 {
if id, err := convertUint64(tpl[0]); err != nil || id != 2 {
t.Errorf("Unexpected body of Update (0)")
}
if h, ok := tpl[1].(string); !ok || h != "bye" {
Expand Down Expand Up @@ -603,7 +631,7 @@ func TestClient(t *testing.T) {
if tpl, ok := resp.Data[0].([]interface{}); !ok {
t.Errorf("Unexpected body of Select")
} else {
if id, ok := tpl[0].(uint64); !ok || id != 10 {
if id, err := convertUint64(tpl[0]); err != nil || id != 10 {
t.Errorf("Unexpected body of Select (0)")
}
if h, ok := tpl[1].(string); !ok || h != "val 10" {
Expand Down Expand Up @@ -698,15 +726,15 @@ func TestClient(t *testing.T) {
if err != nil {
t.Errorf("Failed to use Call")
}
if resp.Data[0].([]interface{})[0].(uint64) != 2 {
if val, err := convertUint64(resp.Data[0].([]interface{})[0]); err != nil || val != 2 {
t.Errorf("result is not {{1}} : %v", resp.Data)
}

resp, err = conn.Call17("simple_incr", []interface{}{1})
if err != nil {
t.Errorf("Failed to use Call17")
}
if resp.Data[0].(uint64) != 2 {
if val, err := convertUint64(resp.Data[0]); err != nil || val != 2 {
t.Errorf("result is not {{1}} : %v", resp.Data)
}

Expand All @@ -721,8 +749,7 @@ func TestClient(t *testing.T) {
if len(resp.Data) < 1 {
t.Errorf("Response.Data is empty after Eval")
}
val := resp.Data[0].(uint64)
if val != 11 {
if val, err := convertUint64(resp.Data[0]); err != nil || val != 11 {
t.Errorf("5 + 6 == 11, but got %v", val)
}
}
Expand Down

0 comments on commit 29ae9a6

Please sign in to comment.