go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/filter/txnBuf/race_test.go (about) 1 // Copyright 2015 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package txnBuf 16 17 import ( 18 "context" 19 "fmt" 20 "sync" 21 "testing" 22 23 "go.chromium.org/luci/gae/impl/memory" 24 ds "go.chromium.org/luci/gae/service/datastore" 25 ) 26 27 type Counter struct { 28 ID int64 `gae:"$id"` 29 30 Value int64 31 } 32 33 func TestRace(t *testing.T) { 34 t.Parallel() 35 36 c := FilterRDS(memory.Use(context.Background())) 37 38 wg := sync.WaitGroup{} 39 for i := 0; i < 100; i++ { 40 id := int64(i + 1) 41 42 wg.Add(1) 43 go func() { 44 defer wg.Done() 45 err := ds.RunInTransaction(c, func(c context.Context) error { 46 for i := 0; i < 100; i++ { 47 err := ds.RunInTransaction(c, func(c context.Context) error { 48 ctr := &Counter{ID: id} 49 if err := ds.Get(c, ctr); err != nil && err != ds.ErrNoSuchEntity { 50 panic(fmt.Sprintf("bad Get: %s", err)) 51 } 52 ctr.Value++ 53 return ds.Put(c, ctr) 54 }, nil) 55 if err != nil { 56 panic(fmt.Sprintf("bad inner RIT: %s", err)) 57 } 58 } 59 60 return nil 61 }, nil) 62 if err != nil { 63 panic(fmt.Sprintf("bad outer RIT: %s", err)) 64 } 65 }() 66 } 67 wg.Wait() 68 }