| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package txnBuf | 5 package txnBuf |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "sync" | 8 "sync" |
| 9 "testing" | 9 "testing" |
| 10 | 10 |
| 11 "github.com/luci/gae/impl/memory" | 11 "github.com/luci/gae/impl/memory" |
| 12 » "github.com/luci/gae/service/datastore" | 12 » ds "github.com/luci/gae/service/datastore" |
| 13 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
| 14 ) | 14 ) |
| 15 | 15 |
| 16 type Counter struct { | 16 type Counter struct { |
| 17 ID int64 `gae:"$id"` | 17 ID int64 `gae:"$id"` |
| 18 | 18 |
| 19 Value int64 | 19 Value int64 |
| 20 } | 20 } |
| 21 | 21 |
| 22 func TestRace(t *testing.T) { | 22 func TestRace(t *testing.T) { |
| 23 t.Parallel() | 23 t.Parallel() |
| 24 | 24 |
| 25 c := FilterRDS(memory.Use(context.Background())) | 25 c := FilterRDS(memory.Use(context.Background())) |
| 26 ds := datastore.Get(c) | |
| 27 | 26 |
| 28 wg := sync.WaitGroup{} | 27 wg := sync.WaitGroup{} |
| 29 for i := 0; i < 100; i++ { | 28 for i := 0; i < 100; i++ { |
| 30 id := int64(i + 1) | 29 id := int64(i + 1) |
| 31 | 30 |
| 32 wg.Add(1) | 31 wg.Add(1) |
| 33 go func() { | 32 go func() { |
| 34 defer wg.Done() | 33 defer wg.Done() |
| 35 » » » err := ds.RunInTransaction(func(c context.Context) error
{ | 34 » » » err := ds.RunInTransaction(c, func(c context.Context) er
ror { |
| 36 » » » » ds := datastore.Get(c) | |
| 37 | |
| 38 for i := 0; i < 100; i++ { | 35 for i := 0; i < 100; i++ { |
| 39 » » » » » err := ds.RunInTransaction(func(c contex
t.Context) error { | 36 » » » » » err := ds.RunInTransaction(c, func(c con
text.Context) error { |
| 40 » » » » » » ds := datastore.Get(c) | |
| 41 | |
| 42 ctr := &Counter{ID: id} | 37 ctr := &Counter{ID: id} |
| 43 » » » » » » if err := ds.Get(ctr); err != ni
l && err != datastore.ErrNoSuchEntity { | 38 » » » » » » if err := ds.Get(c, ctr); err !=
nil && err != ds.ErrNoSuchEntity { |
| 44 t.Fatal("bad Get", err) | 39 t.Fatal("bad Get", err) |
| 45 } | 40 } |
| 46 ctr.Value++ | 41 ctr.Value++ |
| 47 » » » » » » return ds.Put(ctr) | 42 » » » » » » return ds.Put(c, ctr) |
| 48 }, nil) | 43 }, nil) |
| 49 if err != nil { | 44 if err != nil { |
| 50 t.Fatal("bad inner RIT", err) | 45 t.Fatal("bad inner RIT", err) |
| 51 } | 46 } |
| 52 } | 47 } |
| 53 | 48 |
| 54 return nil | 49 return nil |
| 55 }, nil) | 50 }, nil) |
| 56 if err != nil { | 51 if err != nil { |
| 57 t.Fatal("bad outer RIT", err) | 52 t.Fatal("bad outer RIT", err) |
| 58 } | 53 } |
| 59 }() | 54 }() |
| 60 } | 55 } |
| 61 wg.Wait() | 56 wg.Wait() |
| 62 } | 57 } |
| OLD | NEW |