OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package dscache | 5 package dscache |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "encoding/binary" | 9 "encoding/binary" |
10 "errors" | 10 "errors" |
11 "math/rand" | 11 "math/rand" |
12 "testing" | 12 "testing" |
13 "time" | 13 "time" |
14 | 14 |
15 "github.com/luci/gae/filter/featureBreaker" | 15 "github.com/luci/gae/filter/featureBreaker" |
16 "github.com/luci/gae/impl/memory" | 16 "github.com/luci/gae/impl/memory" |
17 "github.com/luci/gae/service/datastore" | 17 "github.com/luci/gae/service/datastore" |
| 18 "github.com/luci/gae/service/datastore/serialize" |
18 "github.com/luci/gae/service/memcache" | 19 "github.com/luci/gae/service/memcache" |
19 "github.com/luci/luci-go/common/clock" | 20 "github.com/luci/luci-go/common/clock" |
20 "github.com/luci/luci-go/common/clock/testclock" | 21 "github.com/luci/luci-go/common/clock/testclock" |
21 "github.com/luci/luci-go/common/mathrand" | 22 "github.com/luci/luci-go/common/mathrand" |
22 . "github.com/smartystreets/goconvey/convey" | 23 . "github.com/smartystreets/goconvey/convey" |
23 "golang.org/x/net/context" | 24 "golang.org/x/net/context" |
24 ) | 25 ) |
25 | 26 |
26 type object struct { | 27 type object struct { |
27 ID int64 `gae:"$id"` | 28 ID int64 `gae:"$id"` |
28 | 29 |
29 Value string | 30 Value string |
30 BigData []byte | 31 BigData []byte |
31 } | 32 } |
32 | 33 |
33 type shardObj struct { // see shardsForKey() at top | 34 type shardObj struct { // see shardsForKey() at top |
34 ID int64 `gae:"$id"` | 35 ID int64 `gae:"$id"` |
35 | 36 |
36 Value string | 37 Value string |
37 } | 38 } |
38 | 39 |
39 type noCacheObj struct { // see shardsForKey() at top | 40 type noCacheObj struct { // see shardsForKey() at top |
40 ID string `gae:"$id"` | 41 ID string `gae:"$id"` |
41 | 42 |
42 Value bool | 43 Value bool |
43 } | 44 } |
44 | 45 |
45 func init() { | 46 func init() { |
46 » datastore.WritePropertyMapDeterministic = true | 47 » serialize.WritePropertyMapDeterministic = true |
47 | 48 |
48 internalValueSizeLimit = 2048 | 49 internalValueSizeLimit = 2048 |
49 } | 50 } |
50 | 51 |
51 func TestDSCache(t *testing.T) { | 52 func TestDSCache(t *testing.T) { |
52 t.Parallel() | 53 t.Parallel() |
53 | 54 |
54 zeroTime, err := time.Parse("2006-01-02T15:04:05.999999999Z", "2006-01-0
2T15:04:05.999999999Z") | 55 zeroTime, err := time.Parse("2006-01-02T15:04:05.999999999Z", "2006-01-0
2T15:04:05.999999999Z") |
55 if err != nil { | 56 if err != nil { |
56 panic(err) | 57 panic(err) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 ds := datastore.Get(c) | 91 ds := datastore.Get(c) |
91 So(dsUnder, ShouldNotBeNil) | 92 So(dsUnder, ShouldNotBeNil) |
92 So(ds, ShouldNotBeNil) | 93 So(ds, ShouldNotBeNil) |
93 So(mc, ShouldNotBeNil) | 94 So(mc, ShouldNotBeNil) |
94 | 95 |
95 Convey("basically works", func() { | 96 Convey("basically works", func() { |
96 pm := datastore.PropertyMap{ | 97 pm := datastore.PropertyMap{ |
97 "BigData": {datastore.MkProperty([]byte(
""))}, | 98 "BigData": {datastore.MkProperty([]byte(
""))}, |
98 "Value": {datastore.MkProperty("hi")}, | 99 "Value": {datastore.MkProperty("hi")}, |
99 } | 100 } |
100 » » » » buf := &bytes.Buffer{} | 101 » » » » encoded := append([]byte{0}, serialize.ToBytes(p
m)...) |
101 » » » » So(pm.Write(buf, datastore.WithoutContext), Shou
ldBeNil) | |
102 » » » » encoded := append([]byte{0}, buf.Bytes()...) | |
103 | 102 |
104 o := object{ID: 1, Value: "hi"} | 103 o := object{ID: 1, Value: "hi"} |
105 So(ds.Put(&o), ShouldBeNil) | 104 So(ds.Put(&o), ShouldBeNil) |
106 | 105 |
107 o = object{ID: 1} | 106 o = object{ID: 1} |
108 So(dsUnder.Get(&o), ShouldBeNil) | 107 So(dsUnder.Get(&o), ShouldBeNil) |
109 So(o.Value, ShouldEqual, "hi") | 108 So(o.Value, ShouldEqual, "hi") |
110 | 109 |
111 itm := itmFor(0, ds.KeyForObj(&o)) | 110 itm := itmFor(0, ds.KeyForObj(&o)) |
112 So(mc.Get(itm), ShouldEqual, memcache.ErrCacheMi
ss) | 111 So(mc.Get(itm), ShouldEqual, memcache.ErrCacheMi
ss) |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 InstanceEnabledStatic = false | 447 InstanceEnabledStatic = false |
449 defer func() { | 448 defer func() { |
450 InstanceEnabledStatic = true | 449 InstanceEnabledStatic = true |
451 }() | 450 }() |
452 | 451 |
453 c := context.Background() | 452 c := context.Background() |
454 newC := FilterRDS(c, nil) | 453 newC := FilterRDS(c, nil) |
455 So(newC, ShouldEqual, c) | 454 So(newC, ShouldEqual, c) |
456 }) | 455 }) |
457 } | 456 } |
OLD | NEW |