Add context-aware read/write (#57)
Adds ReadContext/WriteContext methods to endpoints and to streams. Update rawread example tool to use ReadContext for implementation of "timeout" flag.
This commit is contained in:

committed by
GitHub

parent
593cfb67e9
commit
da849d96b5
@@ -15,8 +15,8 @@
|
||||
package gousb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNewTransfer(t *testing.T) {
|
||||
@@ -34,7 +34,6 @@ func TestNewTransfer(t *testing.T) {
|
||||
tt TransferType
|
||||
maxPkt int
|
||||
buf int
|
||||
timeout time.Duration
|
||||
wantIso int
|
||||
wantLength int
|
||||
wantTimeout int
|
||||
@@ -45,7 +44,6 @@ func TestNewTransfer(t *testing.T) {
|
||||
tt: TransferTypeBulk,
|
||||
maxPkt: 512,
|
||||
buf: 1024,
|
||||
timeout: time.Second,
|
||||
wantLength: 1024,
|
||||
},
|
||||
{
|
||||
@@ -62,7 +60,7 @@ func TestNewTransfer(t *testing.T) {
|
||||
Direction: tc.dir,
|
||||
TransferType: tc.tt,
|
||||
MaxPacketSize: tc.maxPkt,
|
||||
}, tc.buf, tc.timeout)
|
||||
}, tc.buf)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("newUSBTransfer(): %v", err)
|
||||
@@ -92,41 +90,37 @@ func TestTransferProtocol(t *testing.T) {
|
||||
Direction: EndpointDirectionIn,
|
||||
TransferType: TransferTypeBulk,
|
||||
MaxPacketSize: 512,
|
||||
}, 10240, time.Second)
|
||||
}, 10240)
|
||||
if err != nil {
|
||||
t.Fatalf("newUSBTransfer: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
partial := make(chan struct{})
|
||||
go func() {
|
||||
ft := f.waitForSubmitted(nil)
|
||||
ft.length = 5
|
||||
ft.status = TransferCompleted
|
||||
copy(ft.buf, []byte{1, 2, 3, 4, 5})
|
||||
ft.done <- struct{}{}
|
||||
ft.setData([]byte{1, 2, 3, 4, 5})
|
||||
ft.setStatus(TransferCompleted)
|
||||
|
||||
ft = f.waitForSubmitted(nil)
|
||||
ft.length = 99
|
||||
ft.status = TransferCompleted
|
||||
copy(ft.buf, []byte{12, 12, 12, 12, 12})
|
||||
ft.done <- struct{}{}
|
||||
ft.setData(make([]byte, 99))
|
||||
ft.setStatus(TransferCompleted)
|
||||
|
||||
ft = f.waitForSubmitted(nil)
|
||||
ft.length = 123
|
||||
ft.status = TransferCancelled
|
||||
ft.done <- struct{}{}
|
||||
ft.setData(make([]byte, 123))
|
||||
close(partial)
|
||||
}()
|
||||
|
||||
xfers[0].submit()
|
||||
xfers[1].submit()
|
||||
got, err := xfers[0].wait()
|
||||
got, err := xfers[0].wait(context.Background())
|
||||
if err != nil {
|
||||
t.Errorf("xfer#0.wait returned error %v, want nil", err)
|
||||
}
|
||||
if want := 5; got != want {
|
||||
t.Errorf("xfer#0.wait returned %d bytes, want %d", got, want)
|
||||
}
|
||||
got, err = xfers[1].wait()
|
||||
got, err = xfers[1].wait(context.Background())
|
||||
if err != nil {
|
||||
t.Errorf("xfer#0.wait returned error %v, want nil", err)
|
||||
}
|
||||
@@ -135,8 +129,9 @@ func TestTransferProtocol(t *testing.T) {
|
||||
}
|
||||
|
||||
xfers[1].submit()
|
||||
<-partial
|
||||
xfers[1].cancel()
|
||||
got, err = xfers[1].wait()
|
||||
got, err = xfers[1].wait(context.Background())
|
||||
if err == nil {
|
||||
t.Error("xfer#1(resubmitted).wait returned error nil, want non-nil")
|
||||
}
|
||||
@@ -146,7 +141,7 @@ func TestTransferProtocol(t *testing.T) {
|
||||
|
||||
for _, x := range xfers {
|
||||
x.cancel()
|
||||
x.wait()
|
||||
x.wait(context.Background())
|
||||
x.free()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user