mirror of
https://github.com/rclone/rclone.git
synced 2025-12-11 22:14:05 +01:00
lib/readers: add NewContextReader to error on context errors
This commit is contained in:
28
lib/readers/context_test.go
Normal file
28
lib/readers/context_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package readers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestContextReader(t *testing.T) {
|
||||
r := NewPatternReader(100)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cr := NewContextReader(ctx, r)
|
||||
|
||||
var buf = make([]byte, 3)
|
||||
|
||||
n, err := cr.Read(buf)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 3, n)
|
||||
assert.Equal(t, []byte{0, 1, 2}, buf)
|
||||
|
||||
cancel()
|
||||
|
||||
n, err = cr.Read(buf)
|
||||
assert.Equal(t, context.Canceled, err)
|
||||
assert.Equal(t, 0, n)
|
||||
}
|
||||
Reference in New Issue
Block a user