| 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 memory | 5 package memory |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "encoding/binary" | 9 "encoding/binary" |
| 10 "errors" | 10 "errors" |
| 11 "testing" | 11 "testing" |
| 12 "time" | 12 "time" |
| 13 | 13 |
| 14 "github.com/luci/luci-go/common/logdog/types" | 14 "github.com/luci/luci-go/common/logdog/types" |
| 15 "github.com/luci/luci-go/server/logdog/storage" | 15 "github.com/luci/luci-go/server/logdog/storage" |
| 16 | 16 |
| 17 . "github.com/luci/luci-go/common/testing/assertions" | 17 . "github.com/luci/luci-go/common/testing/assertions" |
| 18 . "github.com/smartystreets/goconvey/convey" | 18 . "github.com/smartystreets/goconvey/convey" |
| 19 ) | 19 ) |
| 20 | 20 |
| 21 func numRec(i types.MessageIndex) *rec { | 21 func numRec(v types.MessageIndex) *rec { |
| 22 buf := bytes.Buffer{} | 22 buf := bytes.Buffer{} |
| 23 » binary.Write(&buf, binary.BigEndian, i) | 23 » binary.Write(&buf, binary.BigEndian, v) |
| 24 return &rec{ | 24 return &rec{ |
| 25 » » index: i, | 25 » » index: v, |
| 26 data: buf.Bytes(), | 26 data: buf.Bytes(), |
| 27 } | 27 } |
| 28 } | 28 } |
| 29 | 29 |
| 30 // index builds an index of stream message number to array offset. | 30 // index builds an index of stream message number to array offset. |
| 31 func index(recs []*rec) map[types.MessageIndex]int { | 31 func index(recs []*rec) map[types.MessageIndex]int { |
| 32 index := map[types.MessageIndex]int{} | 32 index := map[types.MessageIndex]int{} |
| 33 for i, r := range recs { | 33 for i, r := range recs { |
| 34 index[r.index] = i | 34 index[r.index] = i |
| 35 } | 35 } |
| 36 return index | 36 return index |
| 37 } | 37 } |
| 38 | 38 |
| 39 func TestBigTable(t *testing.T) { | 39 func TestBigTable(t *testing.T) { |
| 40 t.Parallel() | 40 t.Parallel() |
| 41 | 41 |
| 42 Convey(`A memory Storage instance.`, t, func() { | 42 Convey(`A memory Storage instance.`, t, func() { |
| 43 st := Storage{} | 43 st := Storage{} |
| 44 defer st.Close() | 44 defer st.Close() |
| 45 | 45 |
| 46 path := types.StreamPath("testing/+/foo/bar") | 46 path := types.StreamPath("testing/+/foo/bar") |
| 47 | 47 |
| 48 Convey(`Can Put() log stream records {0..5, 7, 8, 10}.`, func()
{ | 48 Convey(`Can Put() log stream records {0..5, 7, 8, 10}.`, func()
{ |
| 49 » » » ridx := []types.MessageIndex{0, 1, 2, 3, 4, 5, 7, 8, 10} | 49 » » » var indices []types.MessageIndex |
| 50 » » » for _, idx := range ridx { | 50 |
| 51 » » » putRange := func(start types.MessageIndex, count int) er
ror { |
| 51 req := storage.PutRequest{ | 52 req := storage.PutRequest{ |
| 52 Path: path, | 53 Path: path, |
| 53 » » » » » Index: idx, | 54 » » » » » Index: start, |
| 54 » » » » » Value: numRec(idx).data, | |
| 55 } | 55 } |
| 56 | 56 » » » » for i := 0; i < count; i++ { |
| 57 » » » » So(st.Put(&req), ShouldBeNil) | 57 » » » » » index := start + types.MessageIndex(i) |
| 58 » » » » » req.Values = append(req.Values, numRec(i
ndex).data) |
| 59 » » » » » indices = append(indices, index) |
| 60 » » » » } |
| 61 » » » » return st.Put(req) |
| 58 } | 62 } |
| 59 | 63 |
| 64 So(putRange(0, 6), ShouldBeNil) |
| 65 So(putRange(7, 2), ShouldBeNil) |
| 66 So(putRange(10, 1), ShouldBeNil) |
| 67 |
| 60 // Forward-indexed records. | 68 // Forward-indexed records. |
| 61 » » » recs := make([]*rec, len(ridx)) | 69 » » » recs := make([]*rec, len(indices)) |
| 62 » » » for i, idx := range ridx { | 70 » » » for i, idx := range indices { |
| 63 recs[i] = numRec(idx) | 71 recs[i] = numRec(idx) |
| 64 } | 72 } |
| 65 | 73 |
| 66 var getRecs []*rec | 74 var getRecs []*rec |
| 67 getAllCB := func(idx types.MessageIndex, data []byte) bo
ol { | 75 getAllCB := func(idx types.MessageIndex, data []byte) bo
ol { |
| 68 getRecs = append(getRecs, &rec{ | 76 getRecs = append(getRecs, &rec{ |
| 69 index: idx, | 77 index: idx, |
| 70 data: data, | 78 data: data, |
| 71 }) | 79 }) |
| 72 return true | 80 return true |
| 73 } | 81 } |
| 74 | 82 |
| 75 Convey(`Put()`, func() { | 83 Convey(`Put()`, func() { |
| 76 Convey(`Will return ErrExists when putting an ex
isting entry.`, func() { | 84 Convey(`Will return ErrExists when putting an ex
isting entry.`, func() { |
| 77 req := storage.PutRequest{ | 85 req := storage.PutRequest{ |
| 78 » » » » » » Path: path, | 86 » » » » » » Path: path, |
| 79 » » » » » » Index: 5, | 87 » » » » » » Index: 5, |
| 80 » » » » » » Value: []byte("ohai"), | 88 » » » » » » Values: [][]byte{[]byte("ohai")}
, |
| 81 } | 89 } |
| 82 | 90 |
| 83 » » » » » So(st.Put(&req), ShouldEqual, storage.Er
rExists) | 91 » » » » » So(st.Put(req), ShouldEqual, storage.Err
Exists) |
| 84 }) | 92 }) |
| 85 | 93 |
| 86 Convey(`Will return an error if one is set.`, fu
nc() { | 94 Convey(`Will return an error if one is set.`, fu
nc() { |
| 87 st.SetErr(errors.New("test error")) | 95 st.SetErr(errors.New("test error")) |
| 88 | 96 |
| 89 req := storage.PutRequest{ | 97 req := storage.PutRequest{ |
| 90 Path: path, | 98 Path: path, |
| 91 Index: 1337, | 99 Index: 1337, |
| 92 } | 100 } |
| 93 » » » » » So(st.Put(&req), ShouldErrLike, "test er
ror") | 101 » » » » » So(st.Put(req), ShouldErrLike, "test err
or") |
| 94 }) | 102 }) |
| 95 }) | 103 }) |
| 96 | 104 |
| 97 Convey(`Get()`, func() { | 105 Convey(`Get()`, func() { |
| 98 Convey(`Can retrieve all of the records correctl
y.`, func() { | 106 Convey(`Can retrieve all of the records correctl
y.`, func() { |
| 99 recs := make([]*rec, len(ridx)) | |
| 100 for i, idx := range ridx { | |
| 101 recs[i] = numRec(idx) | |
| 102 } | |
| 103 | |
| 104 req := storage.GetRequest{ | 107 req := storage.GetRequest{ |
| 105 Path: path, | 108 Path: path, |
| 106 } | 109 } |
| 107 | 110 |
| 108 » » » » » So(st.Get(&req, getAllCB), ShouldBeNil) | 111 » » » » » So(st.Get(req, getAllCB), ShouldBeNil) |
| 109 So(getRecs, ShouldResemble, recs) | 112 So(getRecs, ShouldResemble, recs) |
| 110 }) | 113 }) |
| 111 | 114 |
| 112 Convey(`Will adhere to GetRequest limit.`, func(
) { | 115 Convey(`Will adhere to GetRequest limit.`, func(
) { |
| 113 req := storage.GetRequest{ | 116 req := storage.GetRequest{ |
| 114 Path: path, | 117 Path: path, |
| 115 Limit: 4, | 118 Limit: 4, |
| 116 } | 119 } |
| 117 | 120 |
| 118 » » » » » So(st.Get(&req, getAllCB), ShouldBeNil) | 121 » » » » » So(st.Get(req, getAllCB), ShouldBeNil) |
| 119 So(getRecs, ShouldResemble, recs[:4]) | 122 So(getRecs, ShouldResemble, recs[:4]) |
| 120 }) | 123 }) |
| 121 | 124 |
| 122 Convey(`Will adhere to hard limit.`, func() { | 125 Convey(`Will adhere to hard limit.`, func() { |
| 123 st.MaxGetCount = 3 | 126 st.MaxGetCount = 3 |
| 124 req := storage.GetRequest{ | 127 req := storage.GetRequest{ |
| 125 Path: path, | 128 Path: path, |
| 126 Limit: 4, | 129 Limit: 4, |
| 127 } | 130 } |
| 128 | 131 |
| 129 » » » » » So(st.Get(&req, getAllCB), ShouldBeNil) | 132 » » » » » So(st.Get(req, getAllCB), ShouldBeNil) |
| 130 So(getRecs, ShouldResemble, recs[:3]) | 133 So(getRecs, ShouldResemble, recs[:3]) |
| 131 }) | 134 }) |
| 132 | 135 |
| 133 Convey(`Will stop iterating if callback returns
false.`, func() { | 136 Convey(`Will stop iterating if callback returns
false.`, func() { |
| 134 req := storage.GetRequest{ | 137 req := storage.GetRequest{ |
| 135 Path: path, | 138 Path: path, |
| 136 } | 139 } |
| 137 | 140 |
| 138 count := 0 | 141 count := 0 |
| 139 » » » » » err := st.Get(&req, func(types.MessageIn
dex, []byte) bool { | 142 » » » » » err := st.Get(req, func(types.MessageInd
ex, []byte) bool { |
| 140 count++ | 143 count++ |
| 141 return false | 144 return false |
| 142 }) | 145 }) |
| 143 So(err, ShouldBeNil) | 146 So(err, ShouldBeNil) |
| 144 So(count, ShouldEqual, 1) | 147 So(count, ShouldEqual, 1) |
| 145 }) | 148 }) |
| 146 | 149 |
| 147 Convey(`Will fail to retrieve records if the str
eam doesn't exist.`, func() { | 150 Convey(`Will fail to retrieve records if the str
eam doesn't exist.`, func() { |
| 148 req := storage.GetRequest{ | 151 req := storage.GetRequest{ |
| 149 Path: "testing/+/does/not/exist"
, | 152 Path: "testing/+/does/not/exist"
, |
| 150 } | 153 } |
| 151 | 154 |
| 152 » » » » » So(st.Get(&req, getAllCB), ShouldEqual,
storage.ErrDoesNotExist) | 155 » » » » » So(st.Get(req, getAllCB), ShouldEqual, s
torage.ErrDoesNotExist) |
| 153 }) | 156 }) |
| 154 | 157 |
| 155 Convey(`Will return an error if one is set.`, fu
nc() { | 158 Convey(`Will return an error if one is set.`, fu
nc() { |
| 156 st.SetErr(errors.New("test error")) | 159 st.SetErr(errors.New("test error")) |
| 157 | 160 |
| 158 req := storage.GetRequest{ | 161 req := storage.GetRequest{ |
| 159 Path: path, | 162 Path: path, |
| 160 } | 163 } |
| 161 » » » » » So(st.Get(&req, nil), ShouldErrLike, "te
st error") | 164 » » » » » So(st.Get(req, nil), ShouldErrLike, "tes
t error") |
| 162 }) | 165 }) |
| 163 }) | 166 }) |
| 164 | 167 |
| 165 Convey(`Tail()`, func() { | 168 Convey(`Tail()`, func() { |
| 166 Convey(`Can retrieve the tail record, 10.`, func
() { | 169 Convey(`Can retrieve the tail record, 10.`, func
() { |
| 167 d, idx, err := st.Tail(path) | 170 d, idx, err := st.Tail(path) |
| 168 So(err, ShouldBeNil) | 171 So(err, ShouldBeNil) |
| 169 So(d, ShouldResemble, numRec(10).data) | 172 So(d, ShouldResemble, numRec(10).data) |
| 170 So(idx, ShouldEqual, 10) | 173 So(idx, ShouldEqual, 10) |
| 171 }) | 174 }) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 | 209 |
| 207 st.SetErr(nil) | 210 st.SetErr(nil) |
| 208 So(st.Config(storage.Config{}), ShouldBeNil) | 211 So(st.Config(storage.Config{}), ShouldBeNil) |
| 209 | 212 |
| 210 st.SetErr(errors.New("test error")) | 213 st.SetErr(errors.New("test error")) |
| 211 So(st.Config(storage.Config{}), ShouldErrLike, "
test error") | 214 So(st.Config(storage.Config{}), ShouldErrLike, "
test error") |
| 212 }) | 215 }) |
| 213 }) | 216 }) |
| 214 }) | 217 }) |
| 215 } | 218 } |
| OLD | NEW |