1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00
hybridgroup.gobot/platforms/jetson/jetson_adaptor_test.go
2023-05-20 14:25:21 +02:00

241 lines
5.5 KiB
Go

package jetson
import (
"errors"
"fmt"
"strings"
"testing"
"runtime"
"strconv"
"sync"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/drivers/i2c"
"gobot.io/x/gobot/v2/drivers/spi"
"gobot.io/x/gobot/v2/gobottest"
"gobot.io/x/gobot/v2/system"
)
// make sure that this Adaptor fulfills all the required interfaces
var _ gobot.Adaptor = (*Adaptor)(nil)
var _ gobot.DigitalPinnerProvider = (*Adaptor)(nil)
var _ gobot.PWMPinnerProvider = (*Adaptor)(nil)
var _ gpio.DigitalReader = (*Adaptor)(nil)
var _ gpio.DigitalWriter = (*Adaptor)(nil)
var _ i2c.Connector = (*Adaptor)(nil)
var _ spi.Connector = (*Adaptor)(nil)
func initTestAdaptorWithMockedFilesystem(mockPaths []string) (*Adaptor, *system.MockFilesystem) {
a := NewAdaptor()
fs := a.sys.UseMockFilesystem(mockPaths)
if err := a.Connect(); err != nil {
panic(err)
}
return a, fs
}
func TestNewAdaptor(t *testing.T) {
a := NewAdaptor()
gobottest.Assert(t, strings.HasPrefix(a.Name(), "JetsonNano"), true)
a.SetName("NewName")
gobottest.Assert(t, a.Name(), "NewName")
}
func TestFinalize(t *testing.T) {
mockPaths := []string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/dev/i2c-1",
"/dev/i2c-0",
"/dev/spidev0.0",
"/dev/spidev0.1",
}
a, _ := initTestAdaptorWithMockedFilesystem(mockPaths)
a.DigitalWrite("3", 1)
a.GetI2cConnection(0xff, 0)
gobottest.Assert(t, a.Finalize(), nil)
}
func TestPWMPinsConnect(t *testing.T) {
a := NewAdaptor()
gobottest.Assert(t, a.pwmPins, (map[string]gobot.PWMPinner)(nil))
err := a.PwmWrite("33", 1)
gobottest.Assert(t, err.Error(), "not connected")
err = a.Connect()
gobottest.Assert(t, err, nil)
gobottest.Refute(t, a.pwmPins, (map[string]gobot.PWMPinner)(nil))
gobottest.Assert(t, len(a.pwmPins), 0)
}
func TestPWMPinsReConnect(t *testing.T) {
// arrange
mockPaths := []string{
"/sys/class/pwm/pwmchip0/export",
"/sys/class/pwm/pwmchip0/unexport",
"/sys/class/pwm/pwmchip0/pwm2/duty_cycle",
"/sys/class/pwm/pwmchip0/pwm2/period",
"/sys/class/pwm/pwmchip0/pwm2/enable",
}
a, _ := initTestAdaptorWithMockedFilesystem(mockPaths)
gobottest.Assert(t, len(a.pwmPins), 0)
gobottest.Assert(t, a.PwmWrite("33", 1), nil)
gobottest.Assert(t, len(a.pwmPins), 1)
gobottest.Assert(t, a.Finalize(), nil)
// act
err := a.Connect()
// assert
gobottest.Assert(t, err, nil)
gobottest.Assert(t, len(a.pwmPins), 0)
}
func TestDigitalIO(t *testing.T) {
mockPaths := []string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio216/value",
"/sys/class/gpio/gpio216/direction",
"/sys/class/gpio/gpio14/value",
"/sys/class/gpio/gpio14/direction",
}
a, fs := initTestAdaptorWithMockedFilesystem(mockPaths)
err := a.DigitalWrite("7", 1)
gobottest.Assert(t, err, nil)
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio216/value"].Contents, "1")
err = a.DigitalWrite("13", 1)
gobottest.Assert(t, err, nil)
i, err := a.DigitalRead("13")
gobottest.Assert(t, err, nil)
gobottest.Assert(t, i, 1)
gobottest.Assert(t, a.DigitalWrite("notexist", 1), errors.New("'notexist' is not a valid id for a digital pin"))
gobottest.Assert(t, a.Finalize(), nil)
}
func TestDigitalPinConcurrency(t *testing.T) {
oldProcs := runtime.GOMAXPROCS(0)
runtime.GOMAXPROCS(8)
defer runtime.GOMAXPROCS(oldProcs)
for retry := 0; retry < 20; retry++ {
a := NewAdaptor()
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
pinAsString := strconv.Itoa(i)
go func(pin string) {
defer wg.Done()
a.DigitalPin(pin)
}(pinAsString)
}
wg.Wait()
}
}
func TestSpiDefaultValues(t *testing.T) {
a := NewAdaptor()
gobottest.Assert(t, a.SpiDefaultBusNumber(), 0)
gobottest.Assert(t, a.SpiDefaultChipNumber(), 0)
gobottest.Assert(t, a.SpiDefaultMode(), 0)
gobottest.Assert(t, a.SpiDefaultMaxSpeed(), int64(10000000))
}
func TestI2cDefaultBus(t *testing.T) {
a := NewAdaptor()
gobottest.Assert(t, a.DefaultI2cBus(), 1)
}
func TestI2cFinalizeWithErrors(t *testing.T) {
// arrange
a := NewAdaptor()
a.sys.UseMockSyscall()
fs := a.sys.UseMockFilesystem([]string{"/dev/i2c-1"})
gobottest.Assert(t, a.Connect(), nil)
con, err := a.GetI2cConnection(0xff, 1)
gobottest.Assert(t, err, nil)
_, err = con.Write([]byte{0xbf})
gobottest.Assert(t, err, nil)
fs.WithCloseError = true
// act
err = a.Finalize()
// assert
gobottest.Assert(t, strings.Contains(err.Error(), "close error"), true)
}
func Test_validateSpiBusNumber(t *testing.T) {
var tests = map[string]struct {
busNr int
wantErr error
}{
"number_negative_error": {
busNr: -1,
wantErr: fmt.Errorf("Bus number -1 out of range"),
},
"number_0_ok": {
busNr: 0,
},
"number_1_ok": {
busNr: 1,
},
"number_2_error": {
busNr: 2,
wantErr: fmt.Errorf("Bus number 2 out of range"),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a := NewAdaptor()
// act
err := a.validateSpiBusNumber(tc.busNr)
// assert
gobottest.Assert(t, err, tc.wantErr)
})
}
}
func Test_validateI2cBusNumber(t *testing.T) {
var tests = map[string]struct {
busNr int
wantErr error
}{
"number_negative_error": {
busNr: -1,
wantErr: fmt.Errorf("Bus number -1 out of range"),
},
"number_0_ok": {
busNr: 0,
},
"number_1_ok": {
busNr: 1,
},
"number_2_not_ok": {
busNr: 2,
wantErr: fmt.Errorf("Bus number 2 out of range"),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a := NewAdaptor()
// act
err := a.validateI2cBusNumber(tc.busNr)
// assert
gobottest.Assert(t, err, tc.wantErr)
})
}
}