av/revid/revid_test.go

69 lines
1.5 KiB
Go
Raw Normal View History

2019-03-08 10:15:38 +03:00
package revid
import (
"fmt"
"os"
"runtime"
"testing"
2019-03-08 13:13:01 +03:00
2019-03-08 10:15:38 +03:00
"bitbucket.org/ausocean/iot/pi/netsender"
)
2019-03-08 13:13:01 +03:00
const raspividPath = "/usr/local/bin/raspivid"
// Suppress all test logging, except for t.Errorf output.
2019-03-08 10:15:38 +03:00
var silent bool
// TestRaspivid tests that raspivid starts correctly.
// It is intended to be run on a Raspberry Pi.
2019-03-08 10:15:38 +03:00
func TestRaspivid(t *testing.T) {
2019-03-08 13:13:01 +03:00
if _, err := os.Stat(raspividPath); os.IsNotExist(err) {
t.Skip("Skipping TestRaspivid since no raspivid found.")
}
2019-03-08 10:15:38 +03:00
var logger testLogger
ns, err := netsender.New(&logger, nil, nil, nil)
if err != nil {
t.Errorf("netsender.New failed with error %v", err)
}
2019-03-08 13:13:01 +03:00
2019-03-08 10:15:38 +03:00
var c Config
c.Logger = &logger
c.Input = Raspivid
c.Outputs = make([]uint8, 1)
rv, err := New(c, ns)
if err != nil {
t.Errorf("revid.New failed with error %v", err)
}
2019-03-08 13:13:01 +03:00
2019-03-08 10:15:38 +03:00
err = rv.Start()
if err != nil {
t.Errorf("revid.Start failed with error %v", err)
}
}
// testLogger implements a netsender.Logger.
2019-03-08 13:13:01 +03:00
type testLogger struct{}
2019-03-08 10:15:38 +03:00
// SetLevel normally sets the logging level, but it is a no-op in our case.
2019-03-08 10:15:38 +03:00
func (tl *testLogger) SetLevel(level int8) {
}
// Log requests the Logger to write a message at the given level.
func (tl *testLogger) Log(level int8, msg string, params ...interface{}) {
logLevels := [...]string{"Debug", "Info", "Warn", "Error", "", "", "Fatal"}
if level < -1 || level > 5 {
panic("Invalid log level")
}
if !silent {
fmt.Printf("%s: %s\n", logLevels[level+1], msg)
}
if level == 5 {
2019-03-08 10:15:38 +03:00
buf := make([]byte, 1<<16)
size := runtime.Stack(buf, true)
fmt.Printf("%s\n", string(buf[:size]))
os.Exit(1)
}
}