1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
//go:build !integration
// +build !integration
package meter
import (
"io"
"io/ioutil"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestReader_New_NoUpdateFrequency(t *testing.T) {
// the original io.ReadCloser is returned if the meter update frequency
// is zero.
reader := ioutil.NopCloser(nil)
m := NewReader(reader, 0, func(uint64, time.Duration, bool) {})
assert.Equal(t, reader, m)
}
func TestReader_New(t *testing.T) {
complete := new(sync.WaitGroup)
complete.Add(1)
m := NewReader(
ioutil.NopCloser(strings.NewReader("foobar")),
50*time.Millisecond,
func(written uint64, since time.Duration, done bool) {
if done {
assert.Equal(t, uint64(6), written)
complete.Done()
}
},
)
_, err := io.Copy(ioutil.Discard, m)
assert.NoError(t, err)
assert.NoError(t, m.Close())
complete.Wait()
// another close shouldn't be a problem
assert.NoError(t, m.Close())
}
|