Skip to content

Commit

Permalink
Merge pull request #30 from fedorlitau/fix-slice-range-error
Browse files Browse the repository at this point in the history
Fix "slice bounds out of range" error in memoryMetric.selectPoints()
  • Loading branch information
nakabonne authored Sep 6, 2021
2 parents 9034eef + b92d1c9 commit 2ad4f71
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions memory_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ func (m *memoryMetric) selectPoints(start, end int64) []*DataPoint {
} else {
// Use binary search because points are in-order.
endIdx = sort.Search(int(size), func(i int) bool {
return m.points[i].Timestamp < end
}) + 1
return m.points[i].Timestamp >= end
})
}
return m.points[startIdx:endIdx]
}
Expand Down
38 changes: 37 additions & 1 deletion memory_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,43 @@ func Test_memoryPartition_SelectDataPoints(t *testing.T) {
want: []*DataPoint{},
},
{
name: "select multiple points",
name: "select some points",
metric: "metric1",
start: 2,
end: 4,
memoryPartition: func() *memoryPartition {
m := newMemoryPartition(nil, 0, "").(*memoryPartition)
m.insertRows([]Row{
{
Metric: "metric1",
DataPoint: DataPoint{Timestamp: 1, Value: 0.1},
},
{
Metric: "metric1",
DataPoint: DataPoint{Timestamp: 2, Value: 0.1},
},
{
Metric: "metric1",
DataPoint: DataPoint{Timestamp: 3, Value: 0.1},
},
{
Metric: "metric1",
DataPoint: DataPoint{Timestamp: 4, Value: 0.1},
},
{
Metric: "metric1",
DataPoint: DataPoint{Timestamp: 5, Value: 0.1},
},
})
return m
}(),
want: []*DataPoint{
{Timestamp: 2, Value: 0.1},
{Timestamp: 3, Value: 0.1},
},
},
{
name: "select all points",
metric: "metric1",
start: 1,
end: 4,
Expand Down

0 comments on commit 2ad4f71

Please sign in to comment.