mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-27 07:57:30 +08:00
test dag reader context cancellation
License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
This commit is contained in:
@ -122,6 +122,41 @@ func TestSeekAndReadLarge(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReadAndCancel(t *testing.T) {
|
||||||
|
dserv := testu.GetDAGServ()
|
||||||
|
inbuf := make([]byte, 20000)
|
||||||
|
rand.Read(inbuf)
|
||||||
|
|
||||||
|
node := testu.GetNode(t, dserv, inbuf, testu.UseProtoBufLeaves)
|
||||||
|
ctx, closer := context.WithCancel(context.Background())
|
||||||
|
defer closer()
|
||||||
|
|
||||||
|
reader, err := NewDagReader(ctx, node, dserv)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
buf := make([]byte, 100)
|
||||||
|
_, err = reader.CtxReadFull(ctx, buf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(buf, inbuf[0:100]) {
|
||||||
|
t.Fatal("read failed")
|
||||||
|
}
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
b, err := ioutil.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal(inbuf[100:], b) {
|
||||||
|
t.Fatal("buffers not equal")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRelativeSeek(t *testing.T) {
|
func TestRelativeSeek(t *testing.T) {
|
||||||
dserv := testu.GetDAGServ()
|
dserv := testu.GetDAGServ()
|
||||||
ctx, closer := context.WithCancel(context.Background())
|
ctx, closer := context.WithCancel(context.Background())
|
||||||
|
Reference in New Issue
Block a user