Skip to content

Commit

Permalink
Bugfix for conflict resolution in GetRaw()
Browse files Browse the repository at this point in the history
  • Loading branch information
zegl committed Feb 9, 2017
1 parent cece9d9 commit e8bc7cc
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
7 changes: 5 additions & 2 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,12 @@ func (c *Command) GetJSON(key string, output interface{}) *GetRawCommand {
WithKey(key)

return &GetRawCommand{
c: c,
key: key,

bucket: c.bucket,
bucketType: c.bucketType,

builder: builder,
key: key,
output: output,
}
}
11 changes: 7 additions & 4 deletions query_get_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import (
)

type GetRawCommand struct {
c *Command

// Riak builder type for SetValue
// Other commands populate riakComand directly
// SetJSON and SetRaw will populate these values instead
builder *riak.FetchValueCommandBuilder

key string
key string

// Used in conflict resolution
bucket string
bucketType string

output interface{}
outputBytes *[]byte

Expand Down Expand Up @@ -64,7 +67,7 @@ func (c *GetRawCommand) fetchValueWithResolver(session *Session, values []*riak.
}

// Save resolution
Bucket(c.c.bucket, c.c.bucketType).
Bucket(c.bucket, c.bucketType).
SetRaw(useObj.Value).
Key(c.key).
WithContext(useObj.VClock).
Expand Down
3 changes: 3 additions & 0 deletions raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func (c *Command) SetRaw(value []byte) *SetRawCommand {
// The output will be written to output by Run().
func (c *Command) GetRaw(key string, output *[]byte) *GetRawCommand {
cmd := &GetRawCommand{
bucket: c.bucket,
bucketType: c.bucketType,

key: key,
outputBytes: output,
isRawOutput: true,
Expand Down
70 changes: 69 additions & 1 deletion siblings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func TestSiblings(t *testing.T) {
func TestSiblingsJSON(t *testing.T) {
key := randomKey()

a1 := Bucket("sibs", "tests").SetJSON("bob").Key(key)
Expand Down Expand Up @@ -74,6 +74,74 @@ func TestSiblings(t *testing.T) {
}
}

func TestSiblingsRaw(t *testing.T) {
key := randomKey()

a1 := Bucket("sibs", "tests").SetRaw([]byte("foo")).Key(key)
a2 := Bucket("sibs", "tests").SetRaw([]byte("bar")).Key(key)

c := con()

_, err := a1.Run(c)

if err != nil {
t.Error(err)
}

_, err = a2.Run(c)

if err != nil {
t.Error(err)
}

didConflictResolution := false

resolver := func(objects []ConflictObject) ResolvedConflict {

if len(objects) != 2 {
t.Errorf("Did not receive 2 objects to conflict resolution. Got %d", len(objects))
}

for _, obj := range objects {
if string(obj.Value) == "foo" {
didConflictResolution = true
return obj.GetResolved()
}
}

return objects[0].GetResolved()
}

var out []byte
_, err = Bucket("sibs", "tests").
GetRaw(key, &out).
ConflictResolver(resolver).
Run(c)

if err != nil {
t.Error(err)
}

if !didConflictResolution {
t.Error("Did not do conflict resolution")
}

didConflictResolution = false

_, err = Bucket("sibs", "tests").
GetRaw(key, &out).
ConflictResolver(resolver).
Run(c)

if err != nil {
t.Error(err)
}

if didConflictResolution {
t.Error("Did resolution after already being resolved?")
}
}

type ourTypeWithResolveInterface struct {
Score int
}
Expand Down

0 comments on commit e8bc7cc

Please sign in to comment.