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 fetcher | 5 package fetcher |
6 | 6 |
7 import ( | 7 import ( |
8 "errors" | 8 "errors" |
9 "io" | 9 "io" |
10 "sync" | 10 "sync" |
(...skipping 11 matching lines...) Expand all Loading... |
22 ) | 22 ) |
23 | 23 |
24 type testSource struct { | 24 type testSource struct { |
25 sync.Mutex | 25 sync.Mutex |
26 | 26 |
27 logs map[types.MessageIndex]*logpb.LogEntry | 27 logs map[types.MessageIndex]*logpb.LogEntry |
28 err error | 28 err error |
29 panic bool | 29 panic bool |
30 | 30 |
31 terminal types.MessageIndex | 31 terminal types.MessageIndex |
32 » maxCount int | 32 » maxCount int64 |
33 maxBytes int64 | 33 maxBytes int64 |
34 | 34 |
35 history []int | 35 history []int |
36 } | 36 } |
37 | 37 |
38 func newTestSource() *testSource { | 38 func newTestSource() *testSource { |
39 return &testSource{ | 39 return &testSource{ |
40 terminal: -1, | 40 terminal: -1, |
41 logs: map[types.MessageIndex]*logpb.LogEntry{}, | 41 logs: map[types.MessageIndex]*logpb.LogEntry{}, |
42 } | 42 } |
43 } | 43 } |
44 | 44 |
45 func (ts *testSource) LogEntries(c context.Context, req *LogRequest) ([]*logpb.L
ogEntry, types.MessageIndex, error) { | 45 func (ts *testSource) LogEntries(c context.Context, req *LogRequest) ([]*logpb.L
ogEntry, types.MessageIndex, error) { |
46 ts.Lock() | 46 ts.Lock() |
47 defer ts.Unlock() | 47 defer ts.Unlock() |
48 | 48 |
49 if ts.err != nil { | 49 if ts.err != nil { |
50 if ts.panic { | 50 if ts.panic { |
51 panic(ts.err) | 51 panic(ts.err) |
52 } | 52 } |
53 return nil, 0, ts.err | 53 return nil, 0, ts.err |
54 } | 54 } |
55 | 55 |
56 maxCount := req.Count | 56 maxCount := req.Count |
57 if ts.maxCount > 0 && maxCount > ts.maxCount { | 57 if ts.maxCount > 0 && maxCount > ts.maxCount { |
58 maxCount = ts.maxCount | 58 maxCount = ts.maxCount |
59 } | 59 } |
60 if maxCount <= 0 { | 60 if maxCount <= 0 { |
61 » » maxCount = len(ts.logs) | 61 » » maxCount = int64(len(ts.logs)) |
62 } | 62 } |
63 | 63 |
64 maxBytes := req.Bytes | 64 maxBytes := req.Bytes |
65 if ts.maxBytes > 0 && maxBytes > ts.maxBytes { | 65 if ts.maxBytes > 0 && maxBytes > ts.maxBytes { |
66 maxBytes = ts.maxBytes | 66 maxBytes = ts.maxBytes |
67 } | 67 } |
68 | 68 |
69 var logs []*logpb.LogEntry | 69 var logs []*logpb.LogEntry |
70 bytes := int64(0) | 70 bytes := int64(0) |
71 index := req.Index | 71 index := req.Index |
72 for { | 72 for { |
73 » » if len(logs) >= maxCount { | 73 » » if int64(len(logs)) >= maxCount { |
74 break | 74 break |
75 } | 75 } |
76 | 76 |
77 log, ok := ts.logs[index] | 77 log, ok := ts.logs[index] |
78 if !ok { | 78 if !ok { |
79 break | 79 break |
80 } | 80 } |
81 | 81 |
82 size := int64(5) // We've rigged all logs to have size 5. | 82 size := int64(5) // We've rigged all logs to have size 5. |
83 if len(logs) > 0 && maxBytes > 0 && (bytes+size) > maxBytes { | 83 if len(logs) > 0 && maxBytes > 0 && (bytes+size) > maxBytes { |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 | 293 |
294 // First fetch will ask for exactly one log. | 294 // First fetch will ask for exactly one log. |
295 logs, err := loadLogs(f, 0) | 295 logs, err := loadLogs(f, 0) |
296 So(err, ShouldEqual, io.EOF) | 296 So(err, ShouldEqual, io.EOF) |
297 So(logs, ShouldResemble, []types.MessageIndex{1}) | 297 So(logs, ShouldResemble, []types.MessageIndex{1}) |
298 | 298 |
299 So(fs.getHistory(), ShouldResemble, []int{1}) | 299 So(fs.getHistory(), ShouldResemble, []int{1}) |
300 }) | 300 }) |
301 }) | 301 }) |
302 } | 302 } |
OLD | NEW |