2014-04-28 08:17:05 +08:00
|
|
|
package neurosky
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
import (
|
2014-12-19 06:42:59 +08:00
|
|
|
"errors"
|
|
|
|
"io"
|
2017-04-03 04:08:10 +08:00
|
|
|
"sync"
|
2014-06-14 04:40:24 +08:00
|
|
|
"testing"
|
2014-07-23 09:00:54 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
2016-10-01 23:44:12 +08:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
2016-08-27 17:56:01 +08:00
|
|
|
|
2017-04-03 04:08:10 +08:00
|
|
|
type NullReadWriteCloser struct {
|
|
|
|
mtx sync.Mutex
|
|
|
|
readError error
|
|
|
|
closeError error
|
|
|
|
}
|
2014-12-19 06:07:48 +08:00
|
|
|
|
2017-04-03 04:08:10 +08:00
|
|
|
// func NewNullReadWriteCloser() *NullReadWriteCloser {
|
|
|
|
// return NullReadWriteCloser{
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
func (n *NullReadWriteCloser) ReadError(e error) {
|
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
n.readError = e
|
2014-12-19 06:07:48 +08:00
|
|
|
}
|
2014-12-19 06:42:59 +08:00
|
|
|
|
2017-04-03 04:08:10 +08:00
|
|
|
func (n *NullReadWriteCloser) CloseError(e error) {
|
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
n.closeError = e
|
|
|
|
}
|
2014-12-19 06:42:59 +08:00
|
|
|
|
2017-04-03 04:08:10 +08:00
|
|
|
func (n *NullReadWriteCloser) Write(p []byte) (int, error) {
|
|
|
|
return len(p), nil
|
2014-12-19 06:07:48 +08:00
|
|
|
}
|
2014-12-19 06:42:59 +08:00
|
|
|
|
2017-04-03 04:08:10 +08:00
|
|
|
func (n *NullReadWriteCloser) Read(b []byte) (int, error) {
|
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
return len(b), n.readError
|
|
|
|
}
|
2014-12-19 06:42:59 +08:00
|
|
|
|
2017-04-03 04:08:10 +08:00
|
|
|
func (n *NullReadWriteCloser) Close() error {
|
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
return n.closeError
|
2014-12-19 06:07:48 +08:00
|
|
|
}
|
|
|
|
|
2016-10-01 23:44:12 +08:00
|
|
|
func initTestNeuroskyAdaptor() *Adaptor {
|
|
|
|
a := NewAdaptor("/dev/null")
|
|
|
|
a.connect = func(n *Adaptor) (io.ReadWriteCloser, error) {
|
2014-12-19 06:42:59 +08:00
|
|
|
return &NullReadWriteCloser{}, nil
|
2014-07-23 09:00:54 +08:00
|
|
|
}
|
|
|
|
return a
|
2014-06-14 04:40:24 +08:00
|
|
|
}
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2014-12-19 06:42:59 +08:00
|
|
|
func TestNeuroskyAdaptor(t *testing.T) {
|
2016-10-01 23:44:12 +08:00
|
|
|
a := NewAdaptor("/dev/null")
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, a.Port(), "/dev/null")
|
2014-12-19 06:42:59 +08:00
|
|
|
}
|
2016-10-01 23:44:12 +08:00
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func TestNeuroskyAdaptorConnect(t *testing.T) {
|
|
|
|
a := initTestNeuroskyAdaptor()
|
2016-11-08 03:00:26 +08:00
|
|
|
gobottest.Assert(t, a.Connect(), nil)
|
2014-12-19 06:42:59 +08:00
|
|
|
|
2016-10-01 23:44:12 +08:00
|
|
|
a.connect = func(n *Adaptor) (io.ReadWriteCloser, error) {
|
2014-12-19 06:42:59 +08:00
|
|
|
return nil, errors.New("connection error")
|
|
|
|
}
|
2016-11-08 03:00:26 +08:00
|
|
|
gobottest.Assert(t, a.Connect(), errors.New("connection error"))
|
2014-06-14 04:40:24 +08:00
|
|
|
}
|
2014-07-23 09:00:54 +08:00
|
|
|
|
|
|
|
func TestNeuroskyAdaptorFinalize(t *testing.T) {
|
2017-04-03 04:08:10 +08:00
|
|
|
rwc := &NullReadWriteCloser{}
|
|
|
|
a := NewAdaptor("/dev/null")
|
|
|
|
a.connect = func(n *Adaptor) (io.ReadWriteCloser, error) {
|
|
|
|
return rwc, nil
|
|
|
|
}
|
2014-07-23 09:00:54 +08:00
|
|
|
a.Connect()
|
2016-11-08 03:00:26 +08:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2014-12-19 06:42:59 +08:00
|
|
|
|
2017-04-03 04:08:10 +08:00
|
|
|
rwc.CloseError(errors.New("close error"))
|
2014-12-19 06:42:59 +08:00
|
|
|
a.Connect()
|
2016-11-08 03:00:26 +08:00
|
|
|
gobottest.Assert(t, a.Finalize(), errors.New("close error"))
|
2014-07-23 09:00:54 +08:00
|
|
|
}
|