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