Skip to content

Commit

Permalink
fix(boom): fix PendingKey handling fixes #30 thanks @sinmetal
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Dec 6, 2017
1 parent b297eeb commit eaa5729
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
3 changes: 3 additions & 0 deletions boom/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func (tx *Transaction) PutMulti(src interface{}) ([]datastore.PendingKey, error)
tx.m.Lock()
defer tx.m.Unlock()
for idx, pKey := range pKeys {
if !keys[idx].Incomplete() {
continue
}
tx.pendingKeysLater = append(tx.pendingKeysLater, &setKeyLater{
pendingKey: pKey,
src: v.Index(idx).Interface(),
Expand Down
10 changes: 6 additions & 4 deletions boom/tx_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ func (b *TransactionBatch) Put(src interface{}) chan *datastore.TransactionPutRe
b.tx.m.Lock()
defer b.tx.m.Unlock()

b.tx.pendingKeysLater = append(b.tx.pendingKeysLater, &setKeyLater{
pendingKey: putResult.PendingKey,
src: src,
})
if keys[0].Incomplete() {
b.tx.pendingKeysLater = append(b.tx.pendingKeysLater, &setKeyLater{
pendingKey: putResult.PendingKey,
src: src,
})
}

c <- putResult
}()
Expand Down
50 changes: 50 additions & 0 deletions boom/tx_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,56 @@ func TestBoom_TransactionBatchPut(t *testing.T) {
}
}

func TestBoom_TransactionBatchPutWithCompleteKey(t *testing.T) {
ctx, client, cleanUp := testutils.SetupCloudDatastore(t)
defer cleanUp()

type Data struct {
ID int64 `datastore:"-" boom:"id"`
}

const size = 25

bm := FromClient(ctx, client)

var list []*Data
tx, err := bm.NewTransaction()
if err != nil {
t.Fatal(err)
}
b := tx.Batch()
for i := 0; i < size; i++ {
obj := &Data{ID: int64(i + 1)}
b.Put(obj)
list = append(list, obj)
}

err = b.Exec()
if err != nil {
t.Fatal(err)
}

if v := len(list); v != size {
t.Errorf("unexpected: %v", v)
}
for idx, obj := range list {
if v := obj.ID; v != int64(idx+1) {
t.Errorf("unexpected: %v", v)
}
}

_, err = tx.Commit()
if err != nil {
t.Fatal(err)
}

for idx, obj := range list {
if v := obj.ID; v != int64(idx+1) {
t.Errorf("unexpected: %v", v)
}
}
}

func TestBoom_TransactionBatchDelete(t *testing.T) {
ctx, client, cleanUp := testutils.SetupCloudDatastore(t)
defer cleanUp()
Expand Down

0 comments on commit eaa5729

Please sign in to comment.