Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: impl/memory/gkvlite_iter_test.go

Issue 1300493002: Add single-collection gkvlite iterator. (Closed) Base URL: https://github.com/luci/gae.git@refactor_service
Patch Set: remove rewind check Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « impl/memory/gkvlite_iter.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 memory
6
7 import (
8 "bytes"
9 "fmt"
10 "testing"
11
12 "github.com/luci/gkvlite"
13 "github.com/luci/luci-go/common/cmpbin"
14 . "github.com/smartystreets/goconvey/convey"
15 )
16
17 func TestIterator(t *testing.T) {
18 t.Parallel()
19
20 mkNum := func(n int64) []byte {
21 buf := &bytes.Buffer{}
22 _, err := cmpbin.WriteInt(buf, n)
23 if err != nil {
24 panic(fmt.Errorf("your RAM is busted: %s", err))
25 }
26 return buf.Bytes()
27 }
28
29 getNum := func(data []byte) int64 {
30 ret, _, err := cmpbin.ReadInt(bytes.NewBuffer(data))
31 if err != nil {
32 panic(fmt.Errorf("your RAM is (probably) busted: %s", er r))
33 }
34 return ret
35 }
36
37 s := newMemStore()
38 c := s.SetCollection("zup", nil)
39 prev := []byte{}
40 for i := 5; i < 100; i++ {
41 data := mkNum(int64(i))
42 c.Set(data, prev)
43 prev = data
44 }
45
46 get := func(c C, t *iterator) int64 {
47 ret := int64(0)
48 t.next(nil, func(i *gkvlite.Item) {
49 c.So(i, ShouldNotBeNil)
50 ret = getNum(i.Key)
51 })
52 return ret
53 }
54
55 skipGet := func(c C, t *iterator, skipTo int64) int64 {
56 ret := int64(0)
57 t.next(mkNum(skipTo), func(i *gkvlite.Item) {
58 c.So(i, ShouldNotBeNil)
59 ret = getNum(i.Key)
60 })
61 return ret
62 }
63
64 Convey("Test iterator", t, func() {
65 Convey("start at nil", func(ctx C) {
66 t := newIterable(c, nil)
67 So(get(ctx, t), ShouldEqual, 5)
68 So(get(ctx, t), ShouldEqual, 6)
69 So(get(ctx, t), ShouldEqual, 7)
70
71 Convey("And can skip", func() {
72 So(skipGet(ctx, t, 10), ShouldEqual, 10)
73 So(get(ctx, t), ShouldEqual, 11)
74
75 Convey("But not forever", func(c C) {
76 t.next(mkNum(200), func(i *gkvlite.Item) {
77 c.So(i, ShouldBeNil)
78 })
79 t.next(nil, func(i *gkvlite.Item) {
80 c.So(i, ShouldBeNil)
81 })
82 })
83 })
84
85 Convey("Can stop", func(c C) {
86 t.stop()
87 t.next(mkNum(200), func(i *gkvlite.Item) {
88 c.So(i, ShouldBeNil)
89 })
90 t.next(nil, func(i *gkvlite.Item) {
91 c.So(i, ShouldBeNil)
92 })
93 So(t.stop, ShouldNotPanic)
94 })
95
96 Convey("Going backwards is fine", func(c C) {
97 So(skipGet(ctx, t, 3), ShouldEqual, 5)
98 So(get(ctx, t), ShouldEqual, 6)
99 })
100 })
101
102 Convey("can have caps on both sides", func(ctx C) {
103 t := newIterable(c, mkNum(25))
104 So(skipGet(ctx, t, 20), ShouldEqual, 20)
105 So(get(ctx, t), ShouldEqual, 21)
106 So(get(ctx, t), ShouldEqual, 22)
107 So(get(ctx, t), ShouldEqual, 23)
108 So(get(ctx, t), ShouldEqual, 24)
109 t.next(nil, func(i *gkvlite.Item) {
110 ctx.So(i, ShouldBeNil)
111 })
112 })
113 })
114 }
OLDNEW
« no previous file with comments | « impl/memory/gkvlite_iter.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698