OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 dsQueryBatch |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "testing" |
| 10 |
| 11 "github.com/luci/gae/filter/count" |
| 12 "github.com/luci/gae/impl/memory" |
| 13 ds "github.com/luci/gae/service/datastore" |
| 14 "golang.org/x/net/context" |
| 15 |
| 16 . "github.com/smartystreets/goconvey/convey" |
| 17 ) |
| 18 |
| 19 type Item struct { |
| 20 ID int64 `gae:"$id"` |
| 21 } |
| 22 |
| 23 func TestRun(t *testing.T) { |
| 24 t.Parallel() |
| 25 |
| 26 Convey("A memory with a counting filter and data set installed", t, func
() { |
| 27 c, cf := count.FilterRDS(memory.Use(context.Background())) |
| 28 |
| 29 items := make([]*Item, 1024) |
| 30 for i := range items { |
| 31 items[i] = &Item{int64(i + 1)} |
| 32 } |
| 33 if err := ds.Get(c).PutMulti(items); err != nil { |
| 34 panic(err) |
| 35 } |
| 36 ds.Get(c).Testable().CatchupIndexes() |
| 37 |
| 38 for _, sizeBase := range []int{ |
| 39 1, |
| 40 16, |
| 41 1024, |
| 42 2048, |
| 43 } { |
| 44 // Adjust to hit edge cases. |
| 45 for _, delta := range []int{-1, 0, 1} { |
| 46 size := sizeBase + delta |
| 47 if size <= 0 { |
| 48 continue |
| 49 } |
| 50 |
| 51 Convey(fmt.Sprintf(`With a batch filter size %d
installed`, size), func() { |
| 52 c = BatchQueries(c, int32(size)) |
| 53 q := ds.NewQuery("Item") |
| 54 |
| 55 Convey(`Can retrieve all of the items.`,
func() { |
| 56 var got []*Item |
| 57 So(ds.Get(c).GetAll(q, &got), Sh
ouldBeNil) |
| 58 So(got, ShouldResemble, items) |
| 59 |
| 60 // One call for every sub-query,
plus one to hit Stop. |
| 61 runCalls := (len(items) / size)
+ 1 |
| 62 So(cf.Run.Successes(), ShouldEqu
al, runCalls) |
| 63 }) |
| 64 |
| 65 Convey(`With a limit of 128, will retrie
ve 128 items.`, func() { |
| 66 const limit = 128 |
| 67 q = q.Limit(int32(limit)) |
| 68 |
| 69 var got []*Item |
| 70 So(ds.Get(c).GetAll(q, &got), Sh
ouldBeNil) |
| 71 So(got, ShouldResemble, items[:l
imit]) |
| 72 |
| 73 // One call for every sub-query,
plus one to hit Stop. |
| 74 runCalls := (limit / size) |
| 75 if (limit % size) != 0 { |
| 76 runCalls++ |
| 77 } |
| 78 So(cf.Run.Successes(), ShouldEqu
al, runCalls) |
| 79 }) |
| 80 }) |
| 81 } |
| 82 } |
| 83 }) |
| 84 } |
OLD | NEW |