From 9a0e52a8f322cdd91d3a5b417f3404aef0bc4da9 Mon Sep 17 00:00:00 2001 From: Scott Date: Tue, 28 Jan 2020 16:52:46 +1030 Subject: [PATCH 1/9] device: add IsRunning method to device interface This commit adds the IsRunning method to the AVDevice interface. It doesn't implement it yet, that will be for a future commit. --- device/alsa/alsa.go | 6 ++++++ device/device.go | 3 +++ device/file/file.go | 6 ++++++ device/geovision/geovision.go | 6 ++++++ device/raspivid/raspivid.go | 6 ++++++ device/webcam/webcam.go | 6 ++++++ 6 files changed, 33 insertions(+) diff --git a/device/alsa/alsa.go b/device/alsa/alsa.go index 5f83d016..4718286e 100644 --- a/device/alsa/alsa.go +++ b/device/alsa/alsa.go @@ -462,3 +462,9 @@ func nearestPowerOfTwo(n int) int { } return v } + +// IsRunning is used to determine if the ALSA device is running. +func (d *ALSA) IsRunning() bool { + panic("not implemented") + return false +} diff --git a/device/device.go b/device/device.go index a75ccb0a..efdc7043 100644 --- a/device/device.go +++ b/device/device.go @@ -55,6 +55,9 @@ type AVDevice interface { // Stop will stop the AVDevice from capturing media data. From this point // Reads will no longer be successful. Stop() error + + // IsRunning is used to determine if the device is running. + IsRunning() bool } // multiError implements the built in error interface. multiError is used here diff --git a/device/file/file.go b/device/file/file.go index 3d990b93..299a45e6 100644 --- a/device/file/file.go +++ b/device/file/file.go @@ -76,3 +76,9 @@ func (m *AVFile) Read(p []byte) (int, error) { } return 0, errors.New("AV file is closed") } + +// IsRunning is used to determine if the AVFile device is running. +func (m *AVFile) IsRunning() bool { + panic("not implemented") + return false +} diff --git a/device/geovision/geovision.go b/device/geovision/geovision.go index d09054ad..cb090d8e 100644 --- a/device/geovision/geovision.go +++ b/device/geovision/geovision.go @@ -339,3 +339,9 @@ func parseSvrRTCPPort(resp rtsp.Response) (int, error) { } return 0, errors.New("SETUP response did not provide RTCP port") } + +// IsRunning is used to determine if the geovision is running. +func (g *GeoVision) IsRunning() bool { + panic("not implemented") + return false +} diff --git a/device/raspivid/raspivid.go b/device/raspivid/raspivid.go index cbae8c83..4168098f 100644 --- a/device/raspivid/raspivid.go +++ b/device/raspivid/raspivid.go @@ -312,3 +312,9 @@ func (r *Raspivid) Stop() error { } return r.out.Close() } + +// IsRunning is used to determine if the pi's camera is running. +func (r *Raspivid) IsRunning() bool { + panic("not implemented") + return false +} diff --git a/device/webcam/webcam.go b/device/webcam/webcam.go index 2d5e27e3..96780376 100644 --- a/device/webcam/webcam.go +++ b/device/webcam/webcam.go @@ -212,3 +212,9 @@ func (w *Webcam) Read(p []byte) (int, error) { } return 0, errors.New("webcam not streaming") } + +// IsRunning is used to determine if the webcam is running. +func (w *Webcam) IsRunning() bool { + panic("not implemented") + return false +} From f1d1fe2cadb99da3fa2a0bb6b47b3730a83a7ba1 Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 30 Jan 2020 11:21:06 +1030 Subject: [PATCH 2/9] device: add tests for IsRunning To check that the IsRunning method is working, some tests are needed. These tests shall be skipped when an AVdevice is not connected, such as when testing within Circle-CI. --- device/alsa/alsa_test.go | 39 ++++++++++++++++- device/file/file_test.go | 62 +++++++++++++++++++++++++++ device/geovision/geovision_test.go | 68 ++++++++++++++++++++++++++++++ device/raspivid/raspivid_test.go | 66 +++++++++++++++++++++++++++++ device/webcam/webcam_test.go | 66 +++++++++++++++++++++++++++++ 5 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 device/file/file_test.go create mode 100644 device/geovision/geovision_test.go create mode 100644 device/raspivid/raspivid_test.go create mode 100644 device/webcam/webcam_test.go diff --git a/device/alsa/alsa_test.go b/device/alsa/alsa_test.go index 44e54e38..5c8e9302 100644 --- a/device/alsa/alsa_test.go +++ b/device/alsa/alsa_test.go @@ -4,9 +4,10 @@ NAME AUTHOR Trek Hopton + Scott Barnard LICENSE - This file is Copyright (C) 2019 the Australian Ocean Lab (AusOcean) + This file is Copyright (C) 2019-2020 the Australian Ocean Lab (AusOcean) It is free software: you can redistribute it and/or modify them under the terms of the GNU General Public License as published by the @@ -25,6 +26,7 @@ LICENSE package alsa import ( + "bytes" "io/ioutil" "os" "strconv" @@ -106,3 +108,38 @@ func TestNearestPowerOfTwo(t *testing.T) { }) } } + +func TestIsRunning(t *testing.T) { + l := logger.New(logger.Debug, &bytes.Buffer{}, true) // Discard logs. + d := New(l) + + var err error + err = d.Set(config.Config{ + SampleRate: 1000, + Channels: 1, + BitDepth: 16, + RecPeriod: 1, + InputCodec: codecutil.ADPCM, + }) + if err != nil { + t.Skipf("could not set device: %w", err) + } + + err = d.Start() + if err != nil { + t.Skipf("could not start device %w", err) + } + time.Sleep(250 * time.Millisecond) + if !d.IsRunning() { + t.Error("device isn't running, when it should be") + } + + err = d.Stop() + if err != nil { + t.Error(err.Error()) + } + time.Sleep(250 * time.Millisecond) + if d.IsRunning() { + t.Error("device is running, when it should not be") + } +} diff --git a/device/file/file_test.go b/device/file/file_test.go new file mode 100644 index 00000000..f2a5d180 --- /dev/null +++ b/device/file/file_test.go @@ -0,0 +1,62 @@ +/* +DESCRIPTION + file_test.go tests the file AVDevice. + +AUTHORS + Scott Barnard + +LICENSE + Copyright (C) 2020 the Australian Ocean Lab (AusOcean) + + It is free software: you can redistribute it and/or modify them + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + It is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + in gpl.txt. If not, see http://www.gnu.org/licenses. +*/ + +package file + +import ( + "testing" + "time" + + "bitbucket.org/ausocean/av/revid/config" +) + +func TestIsRunning(t *testing.T) { + d := New() + + var err error + d.Set(config.Config{ + InputPath: "../../../test/test-data/av/input/motion-detection/mjpeg/school.mjpeg", + }) + if err != nil { + t.Skipf("could not set device: %w", err) + } + + err = d.Start() + if err != nil { + t.Skipf("could not start device %w", err) + } + time.Sleep(250 * time.Millisecond) + if !d.IsRunning() { + t.Error("device isn't running, when it should be") + } + + err = d.Stop() + if err != nil { + t.Error(err.Error()) + } + time.Sleep(250 * time.Millisecond) + if d.IsRunning() { + t.Error("device is running, when it should not be") + } +} diff --git a/device/geovision/geovision_test.go b/device/geovision/geovision_test.go new file mode 100644 index 00000000..f57b957b --- /dev/null +++ b/device/geovision/geovision_test.go @@ -0,0 +1,68 @@ +/* +DESCRIPTION + geovision_test.go tests the geovision AVDevice. + +AUTHORS + Scott Barnard + +LICENSE + Copyright (C) 2020 the Australian Ocean Lab (AusOcean) + + It is free software: you can redistribute it and/or modify them + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + It is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + in gpl.txt. If not, see http://www.gnu.org/licenses. +*/ + +package geovision + +import ( + "bytes" + "testing" + "time" + + "bitbucket.org/ausocean/av/codec/codecutil" + "bitbucket.org/ausocean/av/revid/config" + "bitbucket.org/ausocean/utils/logger" +) + +func TestIsRunning(t *testing.T) { + l := logger.New(logger.Debug, &bytes.Buffer{}, true) // Discard logs. + d := New(l) + + var err error + d.Set(config.Config{ + Logger: l, + InputCodec: codecutil.H264, + CameraIP: "192.168.4.20", + }) + if err != nil { + t.Skipf("could not set device: %w", err) + } + + err = d.Start() + if err != nil { + t.Skipf("could not start device %w", err) + } + time.Sleep(50 * time.Millisecond) + if !d.IsRunning() { + t.Error("device isn't running, when it should be") + } + + err = d.Stop() + if err != nil { + t.Error(err.Error()) + } + time.Sleep(50 * time.Millisecond) + if d.IsRunning() { + t.Error("device is running, when it should not be") + } +} diff --git a/device/raspivid/raspivid_test.go b/device/raspivid/raspivid_test.go new file mode 100644 index 00000000..22f5117a --- /dev/null +++ b/device/raspivid/raspivid_test.go @@ -0,0 +1,66 @@ +/* +DESCRIPTION + raspivid_test.go tests the raspivid AVDevice. + +AUTHORS + Scott Barnard + +LICENSE + Copyright (C) 2020 the Australian Ocean Lab (AusOcean) + + It is free software: you can redistribute it and/or modify them + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + It is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + in gpl.txt. If not, see http://www.gnu.org/licenses. +*/ +package raspivid + +import ( + "bytes" + "testing" + "time" + + "bitbucket.org/ausocean/av/codec/codecutil" + "bitbucket.org/ausocean/av/revid/config" + "bitbucket.org/ausocean/utils/logger" +) + +func TestIsRunning(t *testing.T) { + l := logger.New(logger.Debug, &bytes.Buffer{}, true) // Discard logs. + d := New(l) + + var err error + d.Set(config.Config{ + Logger: l, + InputCodec: codecutil.H264, + }) + if err != nil { + t.Skipf("could not set device: %w", err) + } + + err = d.Start() + if err != nil { + t.Skipf("could not start device %w", err) + } + time.Sleep(250 * time.Millisecond) + if !d.IsRunning() { + t.Error("device isn't running, when it should be") + } + + err = d.Stop() + if err != nil { + t.Error(err.Error()) + } + time.Sleep(250 * time.Millisecond) + if d.IsRunning() { + t.Error("device is running, when it should not be") + } +} diff --git a/device/webcam/webcam_test.go b/device/webcam/webcam_test.go new file mode 100644 index 00000000..c0d15211 --- /dev/null +++ b/device/webcam/webcam_test.go @@ -0,0 +1,66 @@ +/* +DESCRIPTION + webcam_test.go tests the webcam AVDevice. + +AUTHORS + Scott Barnard + +LICENSE + Copyright (C) 2020 the Australian Ocean Lab (AusOcean) + + It is free software: you can redistribute it and/or modify them + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + It is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + in gpl.txt. If not, see http://www.gnu.org/licenses. +*/ +package webcam + +import ( + "bytes" + "testing" + "time" + + "bitbucket.org/ausocean/av/codec/codecutil" + "bitbucket.org/ausocean/av/revid/config" + "bitbucket.org/ausocean/utils/logger" +) + +func TestIsRunning(t *testing.T) { + l := logger.New(logger.Debug, &bytes.Buffer{}, true) // Discard logs. + d := New(l) + + var err error + d.Set(config.Config{ + Logger: l, + InputCodec: codecutil.H264, + }) + if err != nil { + t.Skipf("could not set device: %w", err) + } + + err = d.Start() + if err != nil { + t.Skipf("could not start device %w", err) + } + time.Sleep(250 * time.Millisecond) + if !d.IsRunning() { + t.Error("device isn't running, when it should be") + } + + err = d.Stop() + if err != nil { + t.Error(err.Error()) + } + time.Sleep(250 * time.Millisecond) + if d.IsRunning() { + t.Error("device is running, when it should not be") + } +} From 5c5672486ebd57bf8cec62d37f7c308edcc77a8d Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 30 Jan 2020 11:21:39 +1030 Subject: [PATCH 3/9] device/webcam: implement IsRunning method for webcam --- device/webcam/webcam.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/device/webcam/webcam.go b/device/webcam/webcam.go index 96780376..583a8158 100644 --- a/device/webcam/webcam.go +++ b/device/webcam/webcam.go @@ -62,11 +62,12 @@ var ( // Webcam is an implementation of the AVDevice interface for a Webcam. Webcam // uses an ffmpeg process to pipe the video data from the webcam. type Webcam struct { - out io.ReadCloser - log config.Logger - cfg config.Config - cmd *exec.Cmd - done chan struct{} + out io.ReadCloser + log config.Logger + cfg config.Config + cmd *exec.Cmd + done chan struct{} + isRunning bool } // New returns a new Webcam. @@ -161,6 +162,8 @@ func (w *Webcam) Start() error { return fmt.Errorf("could not pipe command error: %w", err) } + w.isRunning = true + go func() { for { select { @@ -202,7 +205,12 @@ func (w *Webcam) Stop() error { if err != nil { return fmt.Errorf("could not kill ffmpeg process: %w", err) } - return w.out.Close() + err = w.out.Close() + if err == nil { + w.isRunning = false + return nil + } + return err } // Read implements io.Reader. If the pipe is nil a read error is returned. @@ -215,6 +223,5 @@ func (w *Webcam) Read(p []byte) (int, error) { // IsRunning is used to determine if the webcam is running. func (w *Webcam) IsRunning() bool { - panic("not implemented") - return false + return w.isRunning } From 4c651736048235b53fb59c8696f86ca993df99e8 Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 30 Jan 2020 11:21:56 +1030 Subject: [PATCH 4/9] device/file: implement IsRunning method for file --- device/file/file.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/device/file/file.go b/device/file/file.go index 299a45e6..d068ace6 100644 --- a/device/file/file.go +++ b/device/file/file.go @@ -36,8 +36,9 @@ import ( // AVFile is an implementation of the AVDevice interface for a file containg // audio or video data. type AVFile struct { - f io.ReadCloser - cfg config.Config + f io.ReadCloser + cfg config.Config + isRunning bool } // NewAVFile returns a new AVFile. @@ -62,11 +63,19 @@ func (m *AVFile) Start() error { if err != nil { return fmt.Errorf("could not open media file: %w", err) } + m.isRunning = true return nil } // Stop will close the file such that any further reads will fail. -func (m *AVFile) Stop() error { return m.f.Close() } +func (m *AVFile) Stop() error { + err := m.f.Close() + if err == nil { + m.isRunning = false + return nil + } + return err +} // Read implements io.Reader. If start has not been called, or Start has been // called and Stop has since been called, an error is returned. @@ -79,6 +88,5 @@ func (m *AVFile) Read(p []byte) (int, error) { // IsRunning is used to determine if the AVFile device is running. func (m *AVFile) IsRunning() bool { - panic("not implemented") - return false + return m.f != nil && m.isRunning } From 25d68cf1248c174a78357d8d3a0b0aefab5509de Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 30 Jan 2020 11:22:11 +1030 Subject: [PATCH 5/9] device/raspivid: implement IsRunning method for raspivid --- device/raspivid/raspivid.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/device/raspivid/raspivid.go b/device/raspivid/raspivid.go index 4168098f..d47fc346 100644 --- a/device/raspivid/raspivid.go +++ b/device/raspivid/raspivid.go @@ -107,11 +107,12 @@ var AutoWhiteBalanceModes = [...]string{ // Raspivid is an implementation of AVDevice that provides control over the // raspivid command to allow reading of data from a Raspberry Pi camera. type Raspivid struct { - cfg config.Config - cmd *exec.Cmd - out io.ReadCloser - log config.Logger - done chan struct{} + cfg config.Config + cmd *exec.Cmd + out io.ReadCloser + log config.Logger + done chan struct{} + isRunning bool } // New returns a new Raspivid. @@ -287,6 +288,7 @@ func (r *Raspivid) Start() error { if err != nil { return fmt.Errorf("could not start raspivid command: %w", err) } + r.isRunning = true return nil } @@ -310,11 +312,11 @@ func (r *Raspivid) Stop() error { if err != nil { return fmt.Errorf("could not kill raspivid process: %w", err) } + r.isRunning = false return r.out.Close() } // IsRunning is used to determine if the pi's camera is running. func (r *Raspivid) IsRunning() bool { - panic("not implemented") - return false + return r.isRunning } From 0bd21b9003b0a799e8d4207fa94b44ac4cdaeb0f Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 30 Jan 2020 11:22:24 +1030 Subject: [PATCH 6/9] device/alsa: implement IsRunning method for alsa --- device/alsa/alsa.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/device/alsa/alsa.go b/device/alsa/alsa.go index 4718286e..f08667f5 100644 --- a/device/alsa/alsa.go +++ b/device/alsa/alsa.go @@ -465,6 +465,5 @@ func nearestPowerOfTwo(n int) int { // IsRunning is used to determine if the ALSA device is running. func (d *ALSA) IsRunning() bool { - panic("not implemented") - return false + return d.mode == running } From a123789cac45e9c8b9a9ab5f97243d8edcf0a989 Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 30 Jan 2020 11:22:41 +1030 Subject: [PATCH 7/9] device/geovision: implement IsRunning method for geovision --- device/geovision/geovision.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/device/geovision/geovision.go b/device/geovision/geovision.go index cb090d8e..decf1e80 100644 --- a/device/geovision/geovision.go +++ b/device/geovision/geovision.go @@ -88,11 +88,12 @@ var ( // IP camera. This has been designed to implement the GV-BX4700-8F in particular. // Any other models are untested. type GeoVision struct { - cfg avconfig.Config - log avconfig.Logger - rtpClt *rtp.Client - rtspClt *rtsp.Client - rtcpClt *rtcp.Client + cfg avconfig.Config + log avconfig.Logger + rtpClt *rtp.Client + rtspClt *rtsp.Client + rtcpClt *rtcp.Client + isRunning bool } // NewGeoVision returns a new GeoVision. @@ -278,6 +279,7 @@ func (g *GeoVision) Start() error { } g.log.Log(logger.Debug, pkg+"RTSP server PLAY response", "response", resp.String()) g.log.Log(logger.Info, pkg+"play requested, now receiving stream") + g.isRunning = true return nil } @@ -299,6 +301,8 @@ func (g *GeoVision) Stop() error { g.log.Log(logger.Info, pkg+"RTP, RTSP and RTCP clients stopped and closed") + g.isRunning = false + return nil } @@ -342,6 +346,5 @@ func parseSvrRTCPPort(resp rtsp.Response) (int, error) { // IsRunning is used to determine if the geovision is running. func (g *GeoVision) IsRunning() bool { - panic("not implemented") - return false + return g.isRunning } From 9ddd4be0890024f6c4bea118cf71a96d22fad5bd Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 31 Jan 2020 12:22:48 +1030 Subject: [PATCH 8/9] device: use constants for test code --- device/alsa/alsa_test.go | 25 +++++++++++++++++-------- device/file/file_test.go | 16 +++++++++++----- device/geovision/geovision_test.go | 16 +++++++++++----- device/raspivid/raspivid_test.go | 13 +++++++++---- device/webcam/webcam_test.go | 13 +++++++++---- 5 files changed, 57 insertions(+), 26 deletions(-) diff --git a/device/alsa/alsa_test.go b/device/alsa/alsa_test.go index 5c8e9302..b612ec96 100644 --- a/device/alsa/alsa_test.go +++ b/device/alsa/alsa_test.go @@ -110,15 +110,20 @@ func TestNearestPowerOfTwo(t *testing.T) { } func TestIsRunning(t *testing.T) { + const dur = 250 * time.Millisecond + const sampleRate = 1000 + const channels = 1 + const bitDepth = 16 + const recPeriod = 1 + l := logger.New(logger.Debug, &bytes.Buffer{}, true) // Discard logs. d := New(l) - var err error - err = d.Set(config.Config{ - SampleRate: 1000, - Channels: 1, - BitDepth: 16, - RecPeriod: 1, + err := d.Set(config.Config{ + SampleRate: sampleRate, + Channels: channels, + BitDepth: bitDepth, + RecPeriod: recPeriod, InputCodec: codecutil.ADPCM, }) if err != nil { @@ -129,7 +134,9 @@ func TestIsRunning(t *testing.T) { if err != nil { t.Skipf("could not start device %w", err) } - time.Sleep(250 * time.Millisecond) + + time.Sleep(dur) + if !d.IsRunning() { t.Error("device isn't running, when it should be") } @@ -138,7 +145,9 @@ func TestIsRunning(t *testing.T) { if err != nil { t.Error(err.Error()) } - time.Sleep(250 * time.Millisecond) + + time.Sleep(dur) + if d.IsRunning() { t.Error("device is running, when it should not be") } diff --git a/device/file/file_test.go b/device/file/file_test.go index f2a5d180..0a960f11 100644 --- a/device/file/file_test.go +++ b/device/file/file_test.go @@ -32,11 +32,13 @@ import ( ) func TestIsRunning(t *testing.T) { + const dur = 250 * time.Millisecond + const path = "../../../test/test-data/av/input/motion-detection/mjpeg/school.mjpeg" + d := New() - var err error - d.Set(config.Config{ - InputPath: "../../../test/test-data/av/input/motion-detection/mjpeg/school.mjpeg", + err := d.Set(config.Config{ + InputPath: path, }) if err != nil { t.Skipf("could not set device: %w", err) @@ -46,7 +48,9 @@ func TestIsRunning(t *testing.T) { if err != nil { t.Skipf("could not start device %w", err) } - time.Sleep(250 * time.Millisecond) + + time.Sleep(dur) + if !d.IsRunning() { t.Error("device isn't running, when it should be") } @@ -55,7 +59,9 @@ func TestIsRunning(t *testing.T) { if err != nil { t.Error(err.Error()) } - time.Sleep(250 * time.Millisecond) + + time.Sleep(dur) + if d.IsRunning() { t.Error("device is running, when it should not be") } diff --git a/device/geovision/geovision_test.go b/device/geovision/geovision_test.go index f57b957b..191c9e5e 100644 --- a/device/geovision/geovision_test.go +++ b/device/geovision/geovision_test.go @@ -35,14 +35,16 @@ import ( ) func TestIsRunning(t *testing.T) { + const dur = 250 * time.Millisecond + const ip = "192.168.4.20" + l := logger.New(logger.Debug, &bytes.Buffer{}, true) // Discard logs. d := New(l) - var err error - d.Set(config.Config{ + err := d.Set(config.Config{ Logger: l, InputCodec: codecutil.H264, - CameraIP: "192.168.4.20", + CameraIP: ip, }) if err != nil { t.Skipf("could not set device: %w", err) @@ -52,7 +54,9 @@ func TestIsRunning(t *testing.T) { if err != nil { t.Skipf("could not start device %w", err) } - time.Sleep(50 * time.Millisecond) + + time.Sleep(dur) + if !d.IsRunning() { t.Error("device isn't running, when it should be") } @@ -61,7 +65,9 @@ func TestIsRunning(t *testing.T) { if err != nil { t.Error(err.Error()) } - time.Sleep(50 * time.Millisecond) + + time.Sleep(dur) + if d.IsRunning() { t.Error("device is running, when it should not be") } diff --git a/device/raspivid/raspivid_test.go b/device/raspivid/raspivid_test.go index 22f5117a..bdf5df84 100644 --- a/device/raspivid/raspivid_test.go +++ b/device/raspivid/raspivid_test.go @@ -34,11 +34,12 @@ import ( ) func TestIsRunning(t *testing.T) { + const dur = 250 * time.Millisecond + l := logger.New(logger.Debug, &bytes.Buffer{}, true) // Discard logs. d := New(l) - var err error - d.Set(config.Config{ + err := d.Set(config.Config{ Logger: l, InputCodec: codecutil.H264, }) @@ -50,7 +51,9 @@ func TestIsRunning(t *testing.T) { if err != nil { t.Skipf("could not start device %w", err) } - time.Sleep(250 * time.Millisecond) + + time.Sleep(dur) + if !d.IsRunning() { t.Error("device isn't running, when it should be") } @@ -59,7 +62,9 @@ func TestIsRunning(t *testing.T) { if err != nil { t.Error(err.Error()) } - time.Sleep(250 * time.Millisecond) + + time.Sleep(dur) + if d.IsRunning() { t.Error("device is running, when it should not be") } diff --git a/device/webcam/webcam_test.go b/device/webcam/webcam_test.go index c0d15211..608f78ee 100644 --- a/device/webcam/webcam_test.go +++ b/device/webcam/webcam_test.go @@ -34,11 +34,12 @@ import ( ) func TestIsRunning(t *testing.T) { + const dur = 250 * time.Millisecond + l := logger.New(logger.Debug, &bytes.Buffer{}, true) // Discard logs. d := New(l) - var err error - d.Set(config.Config{ + err := d.Set(config.Config{ Logger: l, InputCodec: codecutil.H264, }) @@ -50,7 +51,9 @@ func TestIsRunning(t *testing.T) { if err != nil { t.Skipf("could not start device %w", err) } - time.Sleep(250 * time.Millisecond) + + time.Sleep(dur) + if !d.IsRunning() { t.Error("device isn't running, when it should be") } @@ -59,7 +62,9 @@ func TestIsRunning(t *testing.T) { if err != nil { t.Error(err.Error()) } - time.Sleep(250 * time.Millisecond) + + time.Sleep(dur) + if d.IsRunning() { t.Error("device is running, when it should not be") } From c682f490711cccff28d8a65c61ae33358bc59e9a Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 31 Jan 2020 14:02:45 +1030 Subject: [PATCH 9/9] device: use fatal instead of skip in tests --- device/alsa/alsa_test.go | 2 +- device/file/file_test.go | 2 +- device/geovision/geovision_test.go | 2 +- device/raspivid/raspivid_test.go | 2 +- device/webcam/webcam_test.go | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/device/alsa/alsa_test.go b/device/alsa/alsa_test.go index b612ec96..aeeb6ac9 100644 --- a/device/alsa/alsa_test.go +++ b/device/alsa/alsa_test.go @@ -132,7 +132,7 @@ func TestIsRunning(t *testing.T) { err = d.Start() if err != nil { - t.Skipf("could not start device %w", err) + t.Fatalf("could not start device %w", err) } time.Sleep(dur) diff --git a/device/file/file_test.go b/device/file/file_test.go index 0a960f11..6bf547f4 100644 --- a/device/file/file_test.go +++ b/device/file/file_test.go @@ -46,7 +46,7 @@ func TestIsRunning(t *testing.T) { err = d.Start() if err != nil { - t.Skipf("could not start device %w", err) + t.Fatalf("could not start device %w", err) } time.Sleep(dur) diff --git a/device/geovision/geovision_test.go b/device/geovision/geovision_test.go index 191c9e5e..784313e3 100644 --- a/device/geovision/geovision_test.go +++ b/device/geovision/geovision_test.go @@ -52,7 +52,7 @@ func TestIsRunning(t *testing.T) { err = d.Start() if err != nil { - t.Skipf("could not start device %w", err) + t.Fatalf("could not start device %w", err) } time.Sleep(dur) diff --git a/device/raspivid/raspivid_test.go b/device/raspivid/raspivid_test.go index bdf5df84..a1822317 100644 --- a/device/raspivid/raspivid_test.go +++ b/device/raspivid/raspivid_test.go @@ -49,7 +49,7 @@ func TestIsRunning(t *testing.T) { err = d.Start() if err != nil { - t.Skipf("could not start device %w", err) + t.Fatalf("could not start device %w", err) } time.Sleep(dur) diff --git a/device/webcam/webcam_test.go b/device/webcam/webcam_test.go index 608f78ee..45f8e205 100644 --- a/device/webcam/webcam_test.go +++ b/device/webcam/webcam_test.go @@ -49,7 +49,7 @@ func TestIsRunning(t *testing.T) { err = d.Start() if err != nil { - t.Skipf("could not start device %w", err) + t.Fatalf("could not start device %w", err) } time.Sleep(dur)