git-lfs/lfs/batcher_test.go
Taylor Blau e947838aae lfs/batcher: remove Lot type for []Transferable
@rubyist brings up a good point that the Lot type, being sent down to clients of
the `Batcher` type, introduces some complexity in that clients must know how
`Lot`s work. I don't think the `Lot` type gives a clear enough advantage to
warrant the added complexity for clients.

This commit drops the `Lot` type for operations directly on `[]Transferable`,
but keeps some of the refactors from earlier in PR #615.
2015-08-28 11:34:33 -04:00

73 lines
1.5 KiB
Go

package lfs
import (
"testing"
"github.com/github/git-lfs/vendor/_nuts/github.com/technoweenie/assert"
)
func TestBatcherSizeMet(t *testing.T) {
runBatcherTests([]batcherTestCase{
{2, 4, false},
{3, 5, false},
{0, 0, false},
}, t)
}
func TestBatcherExit(t *testing.T) {
runBatcherTests([]batcherTestCase{
{2, 4, true},
{3, 5, true},
{0, 0, true},
}, t)
}
// batcherTestCase specifies information about how to run a particular test
// around the type lfs.Batcher.
type batcherTestCase struct {
BatchSize int
ItemCount int
ShouldExit bool
}
// Batcher makes and returns a lfs.Batcher according to the specification given
// in this instance of batcherTestCase. When returned, it is filled with the
// given amount of items, and has exited if it was told to.
func (b batcherTestCase) Batcher() *Batcher {
batcher := NewBatcher(b.BatchSize)
for i := 0; i < b.ItemCount; i++ {
batcher.Add(&Downloadable{})
}
if b.ShouldExit {
batcher.Exit()
}
return batcher
}
// Batches returns the number of individual batches expected to be processed by
// the batcher under test.
func (b batcherTestCase) Batches() int {
if b.BatchSize == 0 {
return b.ItemCount
}
return b.ItemCount / b.BatchSize
}
// runBatcherTests processes all test cases, throwing assertion errors if they
// fail.
func runBatcherTests(cases []batcherTestCase, t *testing.T) {
for _, c := range cases {
b := c.Batcher()
items := c.ItemCount
for i := 0; i < c.Batches(); i++ {
group := b.Next()
assert.Equal(t, c.BatchSize, len(group))
items -= c.BatchSize
}
}
}