| 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 "bytes" | 8 "bytes" |
| 9 "encoding/binary" | 9 "encoding/binary" |
| 10 "errors" | 10 "errors" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 // index builds an index of stream message number to array offset. | 31 // index builds an index of stream message number to array offset. |
| 32 func index(recs []*rec) map[types.MessageIndex]int { | 32 func index(recs []*rec) map[types.MessageIndex]int { |
| 33 index := map[types.MessageIndex]int{} | 33 index := map[types.MessageIndex]int{} |
| 34 for i, r := range recs { | 34 for i, r := range recs { |
| 35 index[r.index] = i | 35 index[r.index] = i |
| 36 } | 36 } |
| 37 return index | 37 return index |
| 38 } | 38 } |
| 39 | 39 |
| 40 func mustGetIndex(e *storage.Entry) types.MessageIndex { |
| 41 idx, err := e.GetStreamIndex() |
| 42 if err != nil { |
| 43 panic(err) |
| 44 } |
| 45 return idx |
| 46 } |
| 47 |
| 40 func TestBigTable(t *testing.T) { | 48 func TestBigTable(t *testing.T) { |
| 41 t.Parallel() | 49 t.Parallel() |
| 42 | 50 |
| 43 Convey(`A memory Storage instance.`, t, func() { | 51 Convey(`A memory Storage instance.`, t, func() { |
| 44 st := Storage{} | 52 st := Storage{} |
| 45 defer st.Close() | 53 defer st.Close() |
| 46 | 54 |
| 47 project := config.ProjectName("test-project") | 55 project := config.ProjectName("test-project") |
| 48 path := types.StreamPath("testing/+/foo/bar") | 56 path := types.StreamPath("testing/+/foo/bar") |
| 49 | 57 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 68 So(putRange(7, 2), ShouldBeNil) | 76 So(putRange(7, 2), ShouldBeNil) |
| 69 So(putRange(10, 1), ShouldBeNil) | 77 So(putRange(10, 1), ShouldBeNil) |
| 70 | 78 |
| 71 // Forward-indexed records. | 79 // Forward-indexed records. |
| 72 recs := make([]*rec, len(indices)) | 80 recs := make([]*rec, len(indices)) |
| 73 for i, idx := range indices { | 81 for i, idx := range indices { |
| 74 recs[i] = numRec(idx) | 82 recs[i] = numRec(idx) |
| 75 } | 83 } |
| 76 | 84 |
| 77 var getRecs []*rec | 85 var getRecs []*rec |
| 78 » » » getAllCB := func(idx types.MessageIndex, data []byte) bo
ol { | 86 » » » getAllCB := func(e *storage.Entry) bool { |
| 79 getRecs = append(getRecs, &rec{ | 87 getRecs = append(getRecs, &rec{ |
| 80 » » » » » index: idx, | 88 » » » » » index: mustGetIndex(e), |
| 81 » » » » » data: data, | 89 » » » » » data: e.D, |
| 82 }) | 90 }) |
| 83 return true | 91 return true |
| 84 } | 92 } |
| 85 | 93 |
| 86 Convey(`Put()`, func() { | 94 Convey(`Put()`, func() { |
| 87 req := storage.PutRequest{ | 95 req := storage.PutRequest{ |
| 88 Project: project, | 96 Project: project, |
| 89 Path: path, | 97 Path: path, |
| 90 } | 98 } |
| 91 | 99 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 Convey(`Will adhere to hard limit.`, func() { | 132 Convey(`Will adhere to hard limit.`, func() { |
| 125 st.MaxGetCount = 3 | 133 st.MaxGetCount = 3 |
| 126 req.Limit = 4 | 134 req.Limit = 4 |
| 127 | 135 |
| 128 So(st.Get(req, getAllCB), ShouldBeNil) | 136 So(st.Get(req, getAllCB), ShouldBeNil) |
| 129 So(getRecs, ShouldResemble, recs[:3]) | 137 So(getRecs, ShouldResemble, recs[:3]) |
| 130 }) | 138 }) |
| 131 | 139 |
| 132 Convey(`Will stop iterating if callback returns
false.`, func() { | 140 Convey(`Will stop iterating if callback returns
false.`, func() { |
| 133 count := 0 | 141 count := 0 |
| 134 » » » » » err := st.Get(req, func(types.MessageInd
ex, []byte) bool { | 142 » » » » » err := st.Get(req, func(*storage.Entry)
bool { |
| 135 count++ | 143 count++ |
| 136 return false | 144 return false |
| 137 }) | 145 }) |
| 138 So(err, ShouldBeNil) | 146 So(err, ShouldBeNil) |
| 139 So(count, ShouldEqual, 1) | 147 So(count, ShouldEqual, 1) |
| 140 }) | 148 }) |
| 141 | 149 |
| 142 Convey(`Will fail to retrieve records if the pro
ject doesn't exist.`, func() { | 150 Convey(`Will fail to retrieve records if the pro
ject doesn't exist.`, func() { |
| 143 req.Project = "project-does-not-exist" | 151 req.Project = "project-does-not-exist" |
| 144 | 152 |
| 145 So(st.Get(req, getAllCB), ShouldEqual, s
torage.ErrDoesNotExist) | 153 So(st.Get(req, getAllCB), ShouldEqual, s
torage.ErrDoesNotExist) |
| 146 }) | 154 }) |
| 147 | 155 |
| 148 Convey(`Will fail to retrieve records if the pat
h doesn't exist.`, func() { | 156 Convey(`Will fail to retrieve records if the pat
h doesn't exist.`, func() { |
| 149 req.Path = "testing/+/does/not/exist" | 157 req.Path = "testing/+/does/not/exist" |
| 150 | 158 |
| 151 So(st.Get(req, getAllCB), ShouldEqual, s
torage.ErrDoesNotExist) | 159 So(st.Get(req, getAllCB), ShouldEqual, s
torage.ErrDoesNotExist) |
| 152 }) | 160 }) |
| 153 | 161 |
| 154 Convey(`Will return an error if one is set.`, fu
nc() { | 162 Convey(`Will return an error if one is set.`, fu
nc() { |
| 155 st.SetErr(errors.New("test error")) | 163 st.SetErr(errors.New("test error")) |
| 156 | 164 |
| 157 So(st.Get(req, nil), ShouldErrLike, "tes
t error") | 165 So(st.Get(req, nil), ShouldErrLike, "tes
t error") |
| 158 }) | 166 }) |
| 159 }) | 167 }) |
| 160 | 168 |
| 161 Convey(`Tail()`, func() { | 169 Convey(`Tail()`, func() { |
| 162 Convey(`Can retrieve the tail record, 10.`, func
() { | 170 Convey(`Can retrieve the tail record, 10.`, func
() { |
| 163 » » » » » d, idx, err := st.Tail(project, path) | 171 » » » » » e, err := st.Tail(project, path) |
| 164 So(err, ShouldBeNil) | 172 So(err, ShouldBeNil) |
| 165 » » » » » So(d, ShouldResemble, numRec(10).data) | 173 » » » » » So(e.D, ShouldResemble, numRec(10).data) |
| 166 » » » » » So(idx, ShouldEqual, 10) | 174 » » » » » So(mustGetIndex(e), ShouldEqual, 10) |
| 167 }) | 175 }) |
| 168 | 176 |
| 169 Convey(`Will fail to retrieve records if the pro
ject doesn't exist.`, func() { | 177 Convey(`Will fail to retrieve records if the pro
ject doesn't exist.`, func() { |
| 170 » » » » » _, _, err := st.Tail("project-does-not-e
xist", path) | 178 » » » » » _, err := st.Tail("project-does-not-exis
t", path) |
| 171 So(err, ShouldEqual, storage.ErrDoesNotE
xist) | 179 So(err, ShouldEqual, storage.ErrDoesNotE
xist) |
| 172 }) | 180 }) |
| 173 | 181 |
| 174 Convey(`Will fail to retrieve records if the pat
h doesn't exist.`, func() { | 182 Convey(`Will fail to retrieve records if the pat
h doesn't exist.`, func() { |
| 175 » » » » » _, _, err := st.Tail(project, "testing/+
/does/not/exist") | 183 » » » » » _, err := st.Tail(project, "testing/+/do
es/not/exist") |
| 176 So(err, ShouldEqual, storage.ErrDoesNotE
xist) | 184 So(err, ShouldEqual, storage.ErrDoesNotE
xist) |
| 177 }) | 185 }) |
| 178 | 186 |
| 179 Convey(`Will return an error if one is set.`, fu
nc() { | 187 Convey(`Will return an error if one is set.`, fu
nc() { |
| 180 st.SetErr(errors.New("test error")) | 188 st.SetErr(errors.New("test error")) |
| 181 » » » » » _, _, err := st.Tail("", "") | 189 » » » » » _, err := st.Tail("", "") |
| 182 So(err, ShouldErrLike, "test error") | 190 So(err, ShouldErrLike, "test error") |
| 183 }) | 191 }) |
| 184 }) | 192 }) |
| 185 | 193 |
| 186 Convey(`Config()`, func() { | 194 Convey(`Config()`, func() { |
| 187 cfg := storage.Config{ | 195 cfg := storage.Config{ |
| 188 MaxLogAge: time.Hour, | 196 MaxLogAge: time.Hour, |
| 189 } | 197 } |
| 190 | 198 |
| 191 Convey(`Can update the configuration.`, func() { | 199 Convey(`Can update the configuration.`, func() { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 207 | 215 |
| 208 st.SetErr(nil) | 216 st.SetErr(nil) |
| 209 So(st.Config(storage.Config{}), ShouldBeNil) | 217 So(st.Config(storage.Config{}), ShouldBeNil) |
| 210 | 218 |
| 211 st.SetErr(errors.New("test error")) | 219 st.SetErr(errors.New("test error")) |
| 212 So(st.Config(storage.Config{}), ShouldErrLike, "
test error") | 220 So(st.Config(storage.Config{}), ShouldErrLike, "
test error") |
| 213 }) | 221 }) |
| 214 }) | 222 }) |
| 215 }) | 223 }) |
| 216 } | 224 } |
| OLD | NEW |