Chromium Code Reviews| 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 archive | 5 package archive |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "fmt" | 9 "fmt" |
| 10 "io" | 10 "io" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 err error | 76 err error |
| 77 } | 77 } |
| 78 | 78 |
| 79 func (w *errWriter) Write(d []byte) (int, error) { | 79 func (w *errWriter) Write(d []byte) (int, error) { |
| 80 if w.err != nil { | 80 if w.err != nil { |
| 81 return 0, w.err | 81 return 0, w.err |
| 82 } | 82 } |
| 83 return w.Writer.Write(d) | 83 return w.Writer.Write(d) |
| 84 } | 84 } |
| 85 | 85 |
| 86 // indexParams umarshals an index from a byte stream and removes any entries. | |
|
dnj
2016/10/17 22:12:12
(We now test that the generated index has the righ
| |
| 87 func indexParams(d []byte) logpb.LogIndex { | |
| 88 var index logpb.LogIndex | |
| 89 if err := proto.Unmarshal(d, &index); err != nil { | |
| 90 panic(fmt.Errorf("failed to unmarshal index protobuf: %v", err)) | |
| 91 } | |
| 92 index.Entries = nil | |
| 93 return index | |
| 94 } | |
| 95 | |
| 86 type indexChecker struct { | 96 type indexChecker struct { |
| 87 fixedSize int | 97 fixedSize int |
| 88 } | 98 } |
| 89 | 99 |
| 90 func (ic *indexChecker) size(pb proto.Message) int { | 100 func (ic *indexChecker) size(pb proto.Message) int { |
| 91 if ic.fixedSize > 0 { | 101 if ic.fixedSize > 0 { |
| 92 return ic.fixedSize | 102 return ic.fixedSize |
| 93 } | 103 } |
| 94 return proto.Size(pb) | 104 return proto.Size(pb) |
| 95 } | 105 } |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 LogWriter: &logB, | 232 LogWriter: &logB, |
| 223 IndexWriter: &indexB, | 233 IndexWriter: &indexB, |
| 224 DataWriter: &dataB, | 234 DataWriter: &dataB, |
| 225 } | 235 } |
| 226 | 236 |
| 227 Convey(`A sequence of logs will build a complete index.`, func() { | 237 Convey(`A sequence of logs will build a complete index.`, func() { |
| 228 ts.add(0, 1, 2, 3, 4, 5, 6) | 238 ts.add(0, 1, 2, 3, 4, 5, 6) |
| 229 So(Archive(m), ShouldBeNil) | 239 So(Archive(m), ShouldBeNil) |
| 230 | 240 |
| 231 So(&indexB, ic.shouldContainIndexFor, desc, &logB) | 241 So(&indexB, ic.shouldContainIndexFor, desc, &logB) |
| 242 So(indexParams(indexB.Bytes()), ShouldResemble, logpb.Lo gIndex{ | |
| 243 Desc: desc, | |
| 244 LastPrefixIndex: 12, | |
| 245 LastStreamIndex: 6, | |
| 246 LogEntryCount: 7, | |
| 247 }) | |
| 232 So(dataB.String(), ShouldEqual, "0\n1\n2\n3\n4\n5\n6\n") | 248 So(dataB.String(), ShouldEqual, "0\n1\n2\n3\n4\n5\n6\n") |
| 233 }) | 249 }) |
| 234 | 250 |
| 235 Convey(`A sequence of non-contiguous logs will build a complete index.`, func() { | 251 Convey(`A sequence of non-contiguous logs will build a complete index.`, func() { |
| 236 ts.add(0, 1, 3, 6) | 252 ts.add(0, 1, 3, 6) |
| 237 So(Archive(m), ShouldBeNil) | 253 So(Archive(m), ShouldBeNil) |
| 238 | 254 |
| 239 So(&indexB, ic.shouldContainIndexFor, desc, &logB, 0, 1, 3, 6) | 255 So(&indexB, ic.shouldContainIndexFor, desc, &logB, 0, 1, 3, 6) |
| 256 So(indexParams(indexB.Bytes()), ShouldResemble, logpb.Lo gIndex{ | |
| 257 Desc: desc, | |
| 258 LastPrefixIndex: 12, | |
| 259 LastStreamIndex: 6, | |
| 260 LogEntryCount: 4, | |
| 261 }) | |
| 240 So(dataB.String(), ShouldEqual, "0\n1\n3\n6\n") | 262 So(dataB.String(), ShouldEqual, "0\n1\n3\n6\n") |
| 241 }) | 263 }) |
| 242 | 264 |
| 243 Convey(`Out of order logs are ignored`, func() { | 265 Convey(`Out of order logs are ignored`, func() { |
| 244 Convey(`When StreamIndex is out of order.`, func() { | 266 Convey(`When StreamIndex is out of order.`, func() { |
| 245 ts.add(0, 2, 1, 3) | 267 ts.add(0, 2, 1, 3) |
| 246 So(Archive(m), ShouldBeNil) | 268 So(Archive(m), ShouldBeNil) |
| 247 | 269 |
| 248 So(&indexB, ic.shouldContainIndexFor, desc, &log B, 0, 2, 3) | 270 So(&indexB, ic.shouldContainIndexFor, desc, &log B, 0, 2, 3) |
| 271 So(indexParams(indexB.Bytes()), ShouldResemble, logpb.LogIndex{ | |
| 272 Desc: desc, | |
| 273 LastPrefixIndex: 6, | |
| 274 LastStreamIndex: 3, | |
| 275 LogEntryCount: 3, | |
| 276 }) | |
| 249 }) | 277 }) |
| 250 | 278 |
| 251 Convey(`When PrefixIndex is out of order.`, func() { | 279 Convey(`When PrefixIndex is out of order.`, func() { |
| 252 ts.add(0, 1) | 280 ts.add(0, 1) |
| 253 le := gen(2) | 281 le := gen(2) |
| 254 le.PrefixIndex = 1 | 282 le.PrefixIndex = 1 |
| 255 ts.addEntries(le) | 283 ts.addEntries(le) |
| 256 ts.add(3, 4) | 284 ts.add(3, 4) |
| 257 So(Archive(m), ShouldBeNil) | 285 So(Archive(m), ShouldBeNil) |
| 258 | 286 |
| 259 So(&indexB, ic.shouldContainIndexFor, desc, &log B, 0, 1, 3, 4) | 287 So(&indexB, ic.shouldContainIndexFor, desc, &log B, 0, 1, 3, 4) |
| 288 So(indexParams(indexB.Bytes()), ShouldResemble, logpb.LogIndex{ | |
| 289 Desc: desc, | |
| 290 LastPrefixIndex: 8, | |
| 291 LastStreamIndex: 4, | |
| 292 LogEntryCount: 4, | |
| 293 }) | |
| 260 }) | 294 }) |
| 261 | 295 |
| 262 Convey(`When Sequence is out of order.`, func() { | 296 Convey(`When Sequence is out of order.`, func() { |
| 263 ts.add(0, 1) | 297 ts.add(0, 1) |
| 264 le := gen(2) | 298 le := gen(2) |
| 265 le.Sequence = 0 | 299 le.Sequence = 0 |
| 266 ts.addEntries(le) | 300 ts.addEntries(le) |
| 267 ts.add(3, 4) | 301 ts.add(3, 4) |
| 268 So(Archive(m), ShouldBeNil) | 302 So(Archive(m), ShouldBeNil) |
| 269 | 303 |
| 270 So(&indexB, ic.shouldContainIndexFor, desc, &log B, 0, 1, 3, 4) | 304 So(&indexB, ic.shouldContainIndexFor, desc, &log B, 0, 1, 3, 4) |
| 305 So(indexParams(indexB.Bytes()), ShouldResemble, logpb.LogIndex{ | |
| 306 Desc: desc, | |
| 307 LastPrefixIndex: 8, | |
| 308 LastStreamIndex: 4, | |
| 309 LogEntryCount: 4, | |
| 310 }) | |
| 271 }) | 311 }) |
| 272 }) | 312 }) |
| 273 | 313 |
| 274 Convey(`When TimeOffset is out of order, it is bumped to max.`, func() { | 314 Convey(`When TimeOffset is out of order, it is bumped to max.`, func() { |
| 275 ts.add(0, 1) | 315 ts.add(0, 1) |
| 276 le := gen(2) | 316 le := gen(2) |
| 277 le.TimeOffset = nil // 0 | 317 le.TimeOffset = nil // 0 |
| 278 ts.addEntries(le) | 318 ts.addEntries(le) |
| 279 ts.add(3, 4) | 319 ts.add(3, 4) |
| 280 So(Archive(m), ShouldBeNil) | 320 So(Archive(m), ShouldBeNil) |
| 281 | 321 |
| 282 So(&indexB, ic.shouldContainIndexFor, desc, &logB, 0, 1, 2, 3, 4) | 322 So(&indexB, ic.shouldContainIndexFor, desc, &logB, 0, 1, 2, 3, 4) |
| 323 So(indexParams(indexB.Bytes()), ShouldResemble, logpb.Lo gIndex{ | |
| 324 Desc: desc, | |
| 325 LastPrefixIndex: 8, | |
| 326 LastStreamIndex: 4, | |
| 327 LogEntryCount: 5, | |
| 328 }) | |
| 283 }) | 329 }) |
| 284 | 330 |
| 285 Convey(`Source errors will be returned`, func() { | 331 Convey(`Source errors will be returned`, func() { |
| 286 Convey(`nil LogEntry`, func() { | 332 Convey(`nil LogEntry`, func() { |
| 287 ts.addLogEntry(nil) | 333 ts.addLogEntry(nil) |
| 288 So(Archive(m), ShouldErrLike, "nil LogEntry") | 334 So(Archive(m), ShouldErrLike, "nil LogEntry") |
| 289 }) | 335 }) |
| 290 | 336 |
| 291 Convey(`Error returned`, func() { | 337 Convey(`Error returned`, func() { |
| 292 ts.add(0, 1, 2, 3, 4, 5) | 338 ts.add(0, 1, 2, 3, 4, 5) |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 322 }) | 368 }) |
| 323 | 369 |
| 324 Convey(`When building sparse index`, func() { | 370 Convey(`When building sparse index`, func() { |
| 325 ts.add(0, 1, 2, 3, 4, 5) | 371 ts.add(0, 1, 2, 3, 4, 5) |
| 326 | 372 |
| 327 Convey(`Can build an index for every 3 StreamIndex.`, fu nc() { | 373 Convey(`Can build an index for every 3 StreamIndex.`, fu nc() { |
| 328 m.StreamIndexRange = 3 | 374 m.StreamIndexRange = 3 |
| 329 So(Archive(m), ShouldBeNil) | 375 So(Archive(m), ShouldBeNil) |
| 330 | 376 |
| 331 So(&indexB, ic.shouldContainIndexFor, desc, &log B, 0, 3) | 377 So(&indexB, ic.shouldContainIndexFor, desc, &log B, 0, 3) |
| 378 So(indexParams(indexB.Bytes()), ShouldResemble, logpb.LogIndex{ | |
| 379 Desc: desc, | |
| 380 LastPrefixIndex: 10, | |
| 381 LastStreamIndex: 5, | |
| 382 LogEntryCount: 6, | |
| 383 }) | |
| 332 }) | 384 }) |
| 333 | 385 |
| 334 Convey(`Can build an index for every 3 PrefixIndex.`, fu nc() { | 386 Convey(`Can build an index for every 3 PrefixIndex.`, fu nc() { |
| 335 m.PrefixIndexRange = 3 | 387 m.PrefixIndexRange = 3 |
| 336 So(Archive(m), ShouldBeNil) | 388 So(Archive(m), ShouldBeNil) |
| 337 | 389 |
| 338 // Note that in our generated logs, PrefixIndex = 2*StreamIndex. | 390 // Note that in our generated logs, PrefixIndex = 2*StreamIndex. |
| 339 So(&indexB, ic.shouldContainIndexFor, desc, &log B, 0, 2, 4) | 391 So(&indexB, ic.shouldContainIndexFor, desc, &log B, 0, 2, 4) |
| 392 So(indexParams(indexB.Bytes()), ShouldResemble, logpb.LogIndex{ | |
| 393 Desc: desc, | |
| 394 LastPrefixIndex: 10, | |
| 395 LastStreamIndex: 5, | |
| 396 LogEntryCount: 6, | |
| 397 }) | |
| 340 }) | 398 }) |
| 341 | 399 |
| 342 Convey(`Can build an index for every 13 bytes.`, func() { | 400 Convey(`Can build an index for every 13 bytes.`, func() { |
| 343 ic.fixedSize = 5 | 401 ic.fixedSize = 5 |
| 344 m.ByteRange = 13 | 402 m.ByteRange = 13 |
| 345 m.sizeFunc = func(pb proto.Message) int { | 403 m.sizeFunc = func(pb proto.Message) int { |
| 346 // Stub all LogEntry to be 5 bytes. | 404 // Stub all LogEntry to be 5 bytes. |
| 347 return 5 | 405 return 5 |
| 348 } | 406 } |
| 349 So(Archive(m), ShouldBeNil) | 407 So(Archive(m), ShouldBeNil) |
| 350 | 408 |
| 351 So(&indexB, ic.shouldContainIndexFor, desc, &log B, 0, 2, 5) | 409 So(&indexB, ic.shouldContainIndexFor, desc, &log B, 0, 2, 5) |
| 410 So(indexParams(indexB.Bytes()), ShouldResemble, logpb.LogIndex{ | |
| 411 Desc: desc, | |
| 412 LastPrefixIndex: 10, | |
| 413 LastStreamIndex: 5, | |
| 414 LogEntryCount: 6, | |
| 415 }) | |
| 352 }) | 416 }) |
| 353 }) | 417 }) |
| 354 }) | 418 }) |
| 355 } | 419 } |
| OLD | NEW |