From 9ddd4be0890024f6c4bea118cf71a96d22fad5bd Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 31 Jan 2020 12:22:48 +1030 Subject: [PATCH] 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") }