From c2167157bed2218d335685a19ee6812a583c1a59 Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Sun, 26 Feb 2017 21:08:08 +0100 Subject: [PATCH] Move transfer_fakelibusb to fakelibusb, used in other tests too. Change the interface a bit. --- usb/fakelibusb_test.go | 85 +++++++++++++++++++++++++++++++++ usb/transfer_fakelibusb_test.go | 60 ----------------------- 2 files changed, 85 insertions(+), 60 deletions(-) create mode 100644 usb/fakelibusb_test.go delete mode 100644 usb/transfer_fakelibusb_test.go diff --git a/usb/fakelibusb_test.go b/usb/fakelibusb_test.go new file mode 100644 index 0000000..4a73782 --- /dev/null +++ b/usb/fakelibusb_test.go @@ -0,0 +1,85 @@ +// Copyright 2017 the gousb Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package usb + +import ( + "sync" + "time" +) + +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 +} + +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), + libusbIntf: libusbImpl{}, + } +} diff --git a/usb/transfer_fakelibusb_test.go b/usb/transfer_fakelibusb_test.go deleted file mode 100644 index 319853f..0000000 --- a/usb/transfer_fakelibusb_test.go +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright 2017 the gousb Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package usb - -import "sync" - -type fakeLibusb struct { - sync.Mutex - ts map[*libusbTransfer]chan struct{} - libusbIntf -} - -func (f *fakeLibusb) submit(t *libusbTransfer) error { - f.Lock() - defer f.Unlock() - if f.ts[t] == nil { - f.ts[t] = make(chan struct{}) - } - close(f.ts[t]) - return nil -} - -func (f *fakeLibusb) cancel(t *libusbTransfer) error { return nil } - -func (f *fakeLibusb) waitForSubmit(t *usbTransfer) { - f.Lock() - if f.ts[t.xfer] == nil { - f.ts[t.xfer] = make(chan struct{}) - } - ch := f.ts[t.xfer] - f.Unlock() - <-ch -} - -func (f *fakeLibusb) runCallback(t *usbTransfer, cb func(*usbTransfer)) { - f.Lock() - defer f.Unlock() - delete(f.ts, t.xfer) - cb(t) - close(t.done) -} - -func newFakeLibusb() *fakeLibusb { - return &fakeLibusb{ - ts: make(map[*libusbTransfer]chan struct{}), - libusbIntf: libusbImpl{}, - } -}