| 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 memory | 5 package memory |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "sync" | 8 "sync" |
| 9 "sync/atomic" | 9 "sync/atomic" |
| 10 "testing" | 10 "testing" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 err := ds.RunInTransaction(func(c context.Context) error
{ | 31 err := ds.RunInTransaction(func(c context.Context) error
{ |
| 32 atomic.AddInt32(&num, 1) | 32 atomic.AddInt32(&num, 1) |
| 33 | 33 |
| 34 ds := datastore.Get(c) | 34 ds := datastore.Get(c) |
| 35 | 35 |
| 36 obj := pmap("$key", ds.MakeKey("Obj", 1)) | 36 obj := pmap("$key", ds.MakeKey("Obj", 1)) |
| 37 if err := ds.Get(obj); err != nil && err != data
store.ErrNoSuchEntity { | 37 if err := ds.Get(obj); err != nil && err != data
store.ErrNoSuchEntity { |
| 38 t.Fatal("error get", err) | 38 t.Fatal("error get", err) |
| 39 } | 39 } |
| 40 cur := int64(0) | 40 cur := int64(0) |
| 41 » » » » if ps, ok := obj["Value"]; ok { | 41 » » » » if ps := obj.Slice("Value"); len(ps) > 0 { |
| 42 cur = ps[0].Value().(int64) | 42 cur = ps[0].Value().(int64) |
| 43 } | 43 } |
| 44 | 44 |
| 45 cur++ | 45 cur++ |
| 46 » » » » obj["Value"] = []datastore.Property{prop(cur)} | 46 » » » » obj["Value"] = prop(cur) |
| 47 | 47 |
| 48 return ds.Put(obj) | 48 return ds.Put(obj) |
| 49 }, &datastore.TransactionOptions{Attempts: 200}) | 49 }, &datastore.TransactionOptions{Attempts: 200}) |
| 50 | 50 |
| 51 if err != nil { | 51 if err != nil { |
| 52 t.Fatal("error during transaction", err) | 52 t.Fatal("error during transaction", err) |
| 53 } | 53 } |
| 54 | 54 |
| 55 atomic.AddInt32(&value, 1) | 55 atomic.AddInt32(&value, 1) |
| 56 }() | 56 }() |
| 57 } | 57 } |
| 58 wg.Wait() | 58 wg.Wait() |
| 59 | 59 |
| 60 obj := pmap("$key", ds.MakeKey("Obj", 1)) | 60 obj := pmap("$key", ds.MakeKey("Obj", 1)) |
| 61 if ds.Get(obj) != nil { | 61 if ds.Get(obj) != nil { |
| 62 t.FailNow() | 62 t.FailNow() |
| 63 } | 63 } |
| 64 t.Logf("Ran %d inner functions", num) | 64 t.Logf("Ran %d inner functions", num) |
| 65 » if int64(value) != obj["Value"][0].Value().(int64) { | 65 » if int64(value) != obj.Slice("Value")[0].Value().(int64) { |
| 66 » » t.Fatalf("value wrong value %d v %d", value, obj["Value"][0].Val
ue().(int64)) | 66 » » t.Fatalf("value wrong value %d v %d", value, obj.Slice("Value")[
0].Value().(int64)) |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 func TestRaceNonConflictingPuts(t *testing.T) { | 70 func TestRaceNonConflictingPuts(t *testing.T) { |
| 71 t.Parallel() | 71 t.Parallel() |
| 72 | 72 |
| 73 ds := datastore.Get(Use(context.Background())) | 73 ds := datastore.Get(Use(context.Background())) |
| 74 | 74 |
| 75 num := int32(0) | 75 num := int32(0) |
| 76 | 76 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 92 } | 92 } |
| 93 atomic.AddInt32(&num, 1) | 93 atomic.AddInt32(&num, 1) |
| 94 }() | 94 }() |
| 95 } | 95 } |
| 96 wg.Wait() | 96 wg.Wait() |
| 97 | 97 |
| 98 if num != 100 { | 98 if num != 100 { |
| 99 t.Fatal("expected 100 runs, got", num) | 99 t.Fatal("expected 100 runs, got", num) |
| 100 } | 100 } |
| 101 } | 101 } |
| OLD | NEW |