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