From 4add66ae0edde9f1751ba2c28b3bb492397ff88b Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Fri, 10 Mar 2017 07:39:51 -0500 Subject: [PATCH] move things around, to keep list of the devices at the beginning. --- usb/fakelibusb_test.go | 136 ++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 68 deletions(-) diff --git a/usb/fakelibusb_test.go b/usb/fakelibusb_test.go index 27fb98d..881ac1d 100644 --- a/usb/fakelibusb_test.go +++ b/usb/fakelibusb_test.go @@ -21,74 +21,6 @@ import ( "unsafe" ) -type fakeTransfer struct { - // done is the channel that needs to be closed when the transfer has finished. - done chan struct{} - // buf is the slice for reading/writing data between the submit() and wait() returning. - buf []byte - // status will be returned by wait() on this transfer - status TransferStatus - // length is the number of bytes used from the buffer (write) or available - // in the buffer (read). - length int -} - -type fakeLibusb struct { - libusbIntf - - mu sync.Mutex - // ts has a map of all allocated transfers, indexed by the pointer of - // underlying libusbTransfer. - ts map[*libusbTransfer]*fakeTransfer - // submitted receives a fakeTransfers when submit() is called. - submitted chan *fakeTransfer - // handlers is a map of device handles pointing at opened devices. - handlers map[*libusbDevHandle]*libusbDevice -} - -func (f *fakeLibusb) alloc(_ *libusbDevHandle, _ uint8, _ TransferType, _ time.Duration, _ int, buf []byte) (*libusbTransfer, error) { - f.mu.Lock() - defer f.mu.Unlock() - t := new(libusbTransfer) - f.ts[t] = &fakeTransfer{buf: buf} - return t, nil -} - -func (f *fakeLibusb) submit(t *libusbTransfer, done chan struct{}) error { - f.mu.Lock() - ft := f.ts[t] - f.mu.Unlock() - ft.done = done - f.submitted <- ft - return nil -} - -func (f *fakeLibusb) cancel(t *libusbTransfer) error { return nil } -func (f *fakeLibusb) free(t *libusbTransfer) { - f.mu.Lock() - defer f.mu.Unlock() - delete(f.ts, t) -} - -func (f *fakeLibusb) data(t *libusbTransfer) (int, TransferStatus) { - f.mu.Lock() - defer f.mu.Unlock() - return f.ts[t].length, f.ts[t].status -} - -func (f *fakeLibusb) waitForSubmitted() *fakeTransfer { - return <-f.submitted -} - -func newFakeLibusb() *fakeLibusb { - return &fakeLibusb{ - ts: make(map[*libusbTransfer]*fakeTransfer), - submitted: make(chan *fakeTransfer, 10), - handlers: make(map[*libusbDevHandle]*libusbDevice), - libusbIntf: libusbImpl{}, - } -} - var ( fakeDevices = map[*libusbDevice]*Descriptor{ // Bus 001 Device 001: ID 9999:0001 @@ -204,6 +136,65 @@ var ( } ) +type fakeTransfer struct { + // done is the channel that needs to be closed when the transfer has finished. + done chan struct{} + // buf is the slice for reading/writing data between the submit() and wait() returning. + buf []byte + // status will be returned by wait() on this transfer + status TransferStatus + // length is the number of bytes used from the buffer (write) or available + // in the buffer (read). + length int +} + +type fakeLibusb struct { + libusbIntf + + mu sync.Mutex + // ts has a map of all allocated transfers, indexed by the pointer of + // underlying libusbTransfer. + ts map[*libusbTransfer]*fakeTransfer + // submitted receives a fakeTransfers when submit() is called. + submitted chan *fakeTransfer + // handlers is a map of device handles pointing at opened devices. + handlers map[*libusbDevHandle]*libusbDevice +} + +func (f *fakeLibusb) alloc(_ *libusbDevHandle, _ uint8, _ TransferType, _ time.Duration, _ int, buf []byte) (*libusbTransfer, error) { + f.mu.Lock() + defer f.mu.Unlock() + t := new(libusbTransfer) + f.ts[t] = &fakeTransfer{buf: buf} + return t, nil +} + +func (f *fakeLibusb) submit(t *libusbTransfer, done chan struct{}) error { + f.mu.Lock() + ft := f.ts[t] + f.mu.Unlock() + ft.done = done + f.submitted <- ft + return nil +} + +func (f *fakeLibusb) cancel(t *libusbTransfer) error { return nil } +func (f *fakeLibusb) free(t *libusbTransfer) { + f.mu.Lock() + defer f.mu.Unlock() + delete(f.ts, t) +} + +func (f *fakeLibusb) data(t *libusbTransfer) (int, TransferStatus) { + f.mu.Lock() + defer f.mu.Unlock() + return f.ts[t].length, f.ts[t].status +} + +func (f *fakeLibusb) waitForSubmitted() *fakeTransfer { + return <-f.submitted +} + func (f *fakeLibusb) getDevices(*libusbContext) ([]*libusbDevice, error) { ret := make([]*libusbDevice, 0, len(fakeDevices)) for d := range fakeDevices { @@ -234,3 +225,12 @@ func (f *fakeLibusb) close(h *libusbDevHandle) { defer f.mu.Unlock() delete(f.handlers, h) } + +func newFakeLibusb() *fakeLibusb { + return &fakeLibusb{ + ts: make(map[*libusbTransfer]*fakeTransfer), + submitted: make(chan *fakeTransfer, 10), + handlers: make(map[*libusbDevHandle]*libusbDevice), + libusbIntf: libusbImpl{}, + } +}