From b9796a46f246465420baffd8e9e08cb611c7a700 Mon Sep 17 00:00:00 2001 From: Ingo Oppermann Date: Fri, 12 Jul 2024 09:00:47 +0200 Subject: [PATCH] Add test for type conversion --- restream/app/avstream_test.go | 78 +++++++ restream/app/probe.go | 41 ---- restream/app/process_test.go | 52 +++++ restream/app/progress_test.go | 389 +++++++++++++++++++++++++++++++--- 4 files changed, 495 insertions(+), 65 deletions(-) create mode 100644 restream/app/avstream_test.go diff --git a/restream/app/avstream_test.go b/restream/app/avstream_test.go new file mode 100644 index 00000000..834aad0a --- /dev/null +++ b/restream/app/avstream_test.go @@ -0,0 +1,78 @@ +package app + +import ( + "testing" + + "github.com/datarhei/core/v16/ffmpeg/parse" + "github.com/stretchr/testify/require" +) + +func TestAVstreamIO(t *testing.T) { + original := parse.AVstreamIO{ + State: "running", + Packet: 484, + Time: 4373, + Size: 4783, + } + + p := AVstreamIO{} + p.UnmarshalParser(&original) + restored := p.MarshalParser() + + require.Equal(t, original, restored) +} + +func TestAVstreamSwap(t *testing.T) { + original := parse.AVStreamSwap{ + URL: "ffdsjhhj", + Status: "none", + LastURL: "fjfd", + LastError: "none", + } + + p := AVStreamSwap{} + p.UnmarshalParser(&original) + restored := p.MarshalParser() + + require.Equal(t, original, restored) +} + +func TestAVstream(t *testing.T) { + original := parse.AVstream{ + Input: parse.AVstreamIO{ + State: "running", + Packet: 484, + Time: 4373, + Size: 4783, + }, + Output: parse.AVstreamIO{ + State: "idle", + Packet: 4843, + Time: 483, + Size: 34, + }, + Aqueue: 8574, + Queue: 5877, + Dup: 473, + Drop: 463, + Enc: 474, + Looping: true, + LoopingRuntime: 347, + Duplicating: true, + GOP: "xxx", + Mode: "yyy", + Debug: nil, + Swap: parse.AVStreamSwap{ + URL: "ffdsjhhj", + Status: "none", + LastURL: "fjfd", + LastError: "none", + }, + } + + p := AVstream{} + p.UnmarshalParser(&original) + restored := p.MarshalParser() + + require.Equal(t, &original, restored) +} diff --git a/restream/app/probe.go b/restream/app/probe.go index dcca51e8..b49a5d5d 100644 --- a/restream/app/probe.go +++ b/restream/app/probe.go @@ -48,29 +48,6 @@ func (p *ProbeIO) UnmarshalProber(pp *probe.ProbeIO) { p.Channels = pp.Channels } -/* - func (app *ProbeIO) MarshallAPI() api.ProbeIO { - return api.ProbeIO{ - Address: app.Address, - Format: app.Format, - Index: app.Index, - Stream: app.Stream, - Language: app.Language, - Type: app.Type, - Codec: app.Codec, - Coder: app.Coder, - Bitrate: json.ToNumber(app.Bitrate), - Duration: json.ToNumber(app.Duration), - FPS: json.ToNumber(app.FPS), - Pixfmt: app.Pixfmt, - Width: app.Width, - Height: app.Height, - Sampling: app.Sampling, - Layout: app.Layout, - Channels: app.Channels, - } - } -*/ type Probe struct { Streams []ProbeIO Log []string @@ -86,21 +63,3 @@ func (p *Probe) UnmarshalProber(pp *probe.Probe) { p.Streams[i].UnmarshalProber(&s) } } - -/* -// Unmarshal converts a restreamer Probe to a Probe in API representation -func (app *Probe) MarshallAPI() api.Probe { - p := api.Probe{ - Streams: make([]api.ProbeIO, len(app.Streams)), - Log: make([]string, len(app.Log)), - } - - for i, io := range app.Streams { - p.Streams[i] = io.MarshallAPI() - } - - copy(p.Log, app.Log) - - return p -} -*/ diff --git a/restream/app/process_test.go b/restream/app/process_test.go index 61f6ca1f..96889697 100644 --- a/restream/app/process_test.go +++ b/restream/app/process_test.go @@ -4,6 +4,7 @@ import ( "bytes" "testing" + "github.com/datarhei/core/v16/ffmpeg/parse" "github.com/stretchr/testify/require" ) @@ -58,3 +59,54 @@ func TestConfigHash(t *testing.T) { require.False(t, bytes.Equal(hash1, hash2)) } + +func TestProcessUsageCPU(t *testing.T) { + original := parse.UsageCPU{ + NCPU: 1.5, + Average: 0.9, + Max: 1.3, + Limit: 100, + } + + p := ProcessUsageCPU{} + p.UnmarshalParser(&original) + restored := p.MarshalParser() + + require.Equal(t, original, restored) +} + +func TestProcessUsageMemory(t *testing.T) { + original := parse.UsageMemory{ + Average: 72, + Max: 150, + Limit: 200, + } + + p := ProcessUsageMemory{} + p.UnmarshalParser(&original) + restored := p.MarshalParser() + + require.Equal(t, original, restored) +} + +func TestProcessUsage(t *testing.T) { + original := parse.Usage{ + CPU: parse.UsageCPU{ + NCPU: 1.5, + Average: 0.9, + Max: 1.3, + Limit: 100, + }, + Memory: parse.UsageMemory{ + Average: 72, + Max: 150, + Limit: 200, + }, + } + + p := ProcessUsage{} + p.UnmarshalParser(&original) + restored := p.MarshalParser() + + require.Equal(t, original, restored) +} diff --git a/restream/app/progress_test.go b/restream/app/progress_test.go index 368a6052..bfffa293 100644 --- a/restream/app/progress_test.go +++ b/restream/app/progress_test.go @@ -9,34 +9,37 @@ import ( func TestProgressIO(t *testing.T) { original := parse.ProgressIO{ - Address: "", - Index: 0, - Stream: 0, - Format: "", - Type: "", - Codec: "", - Coder: "", - Frame: 0, - Keyframe: 0, + Address: "fhdj", + Index: 2, + Stream: 4, + Format: "yuv420p", + Type: "video", + Codec: "h264", + Coder: "libx264", + Frame: 39, + Keyframe: 433, Framerate: struct { Min float64 Max float64 Average float64 - }{}, - FPS: 0, - Packet: 0, - PPS: 0, - Size: 0, - Bitrate: 0, - Extradata: 0, - Pixfmt: "", - Quantizer: 0, - Width: 0, - Height: 0, - Sampling: 0, - Layout: "", - Channels: 0, - AVstream: &parse.AVstream{}, + }{ + Min: 47.0, + Max: 97.8, + Average: 463.9, + }, + FPS: 34.8, + Packet: 4737, + PPS: 473.8, + Size: 48474, + Bitrate: 38473, + Extradata: 4874, + Pixfmt: "none", + Quantizer: 2.3, + Width: 4848, + Height: 9373, + Sampling: 4733, + Layout: "atmos", + Channels: 83, } p := ProgressIO{ @@ -47,3 +50,341 @@ func TestProgressIO(t *testing.T) { require.Equal(t, original, restored) } + +func TestProgressIOWithAVstream(t *testing.T) { + original := parse.ProgressIO{ + Address: "fhdj", + Index: 2, + Stream: 4, + Format: "yuv420p", + Type: "video", + Codec: "h264", + Coder: "libx264", + Frame: 39, + Keyframe: 433, + Framerate: struct { + Min float64 + Max float64 + Average float64 + }{ + Min: 47.0, + Max: 97.8, + Average: 463.9, + }, + FPS: 34.8, + Packet: 4737, + PPS: 473.8, + Size: 48474, + Bitrate: 38473, + Extradata: 4874, + Pixfmt: "none", + Quantizer: 2.3, + Width: 4848, + Height: 9373, + Sampling: 4733, + Layout: "atmos", + Channels: 83, + AVstream: &parse.AVstream{ + Input: parse.AVstreamIO{ + State: "running", + Packet: 484, + Time: 4373, + Size: 4783, + }, + Output: parse.AVstreamIO{ + State: "idle", + Packet: 4843, + Time: 483, + Size: 34, + }, + Aqueue: 8574, + Queue: 5877, + Dup: 473, + Drop: 463, + Enc: 474, + Looping: true, + LoopingRuntime: 347, + Duplicating: true, + GOP: "xxx", + Mode: "yyy", + Debug: nil, + Swap: parse.AVStreamSwap{ + URL: "ffdsjhhj", + Status: "none", + LastURL: "fjfd", + LastError: "none", + }, + }, + } + + p := ProgressIO{ + AVstream: nil, + } + p.UnmarshalParser(&original) + restored := p.MarshalParser() + + require.Equal(t, original, restored) +} + +func TestGraphMapping(t *testing.T) { + original := parse.GraphMapping{ + Input: 1, + Output: 3, + Index: 39, + Name: "foobar", + Copy: true, + } + + p := GraphMapping{} + p.UnmarshalParser(&original) + restored := p.MarshalParser() + + require.Equal(t, original, restored) +} + +func TestGraphElement(t *testing.T) { + original := parse.GraphElement{ + Index: 5, + Name: "foobar", + Filter: "infilter", + DstName: "outfilter_", + DstFilter: "outfilter", + Inpad: "inpad", + Outpad: "outpad", + Timebase: "100", + Type: "video", + Format: "yuv420p", + Sampling: 39944, + Layout: "atmos", + Width: 1029, + Height: 463, + } + + p := GraphElement{} + p.UnmarshalParser(&original) + restored := p.MarshalParser() + + require.Equal(t, original, restored) +} + +func TestStreamMapping(t *testing.T) { + original := parse.StreamMapping{ + Graphs: []parse.GraphElement{ + { + Index: 5, + Name: "foobar", + Filter: "infilter", + DstName: "outfilter_", + DstFilter: "outfilter", + Inpad: "inpad", + Outpad: "outpad", + Timebase: "100", + Type: "video", + Format: "yuv420p", + Sampling: 39944, + Layout: "atmos", + Width: 1029, + Height: 463, + }, + }, + Mapping: []parse.GraphMapping{ + { + Input: 1, + Output: 3, + Index: 39, + Name: "foobar", + Copy: true, + }, + }, + } + + p := StreamMapping{} + p.UnmarshalParser(&original) + restored := p.MarshalParser() + + require.Equal(t, original, restored) +} + +func TestProgress(t *testing.T) { + original := parse.Progress{ + Started: false, + Input: []parse.ProgressIO{ + { + Address: "fhd873487j", + Index: 2, + Stream: 4, + Format: "yuv420p", + Type: "video", + Codec: "h264", + Coder: "libx264", + Frame: 39, + Keyframe: 433, + Framerate: struct { + Min float64 + Max float64 + Average float64 + }{ + Min: 47.0, + Max: 97.8, + Average: 463.9, + }, + FPS: 34.8, + Packet: 4737, + PPS: 473.8, + Size: 48474, + Bitrate: 38473, + Extradata: 4874, + Pixfmt: "none", + Quantizer: 2.3, + Width: 4848, + Height: 9373, + Sampling: 4733, + Layout: "atmos", + Channels: 83, + AVstream: &parse.AVstream{ + Input: parse.AVstreamIO{ + State: "running", + Packet: 484, + Time: 4373, + Size: 4783, + }, + Output: parse.AVstreamIO{ + State: "idle", + Packet: 4843, + Time: 483, + Size: 34, + }, + Aqueue: 8574, + Queue: 5877, + Dup: 473, + Drop: 463, + Enc: 474, + Looping: true, + LoopingRuntime: 347, + Duplicating: true, + GOP: "xxx", + Mode: "yyy", + Debug: nil, + Swap: parse.AVStreamSwap{ + URL: "ffdsjhhj", + Status: "none", + LastURL: "fjfd", + LastError: "none", + }, + }, + }, + }, + Output: []parse.ProgressIO{ + { + Address: "fhdj", + Index: 2, + Stream: 4, + Format: "yuv420p", + Type: "video", + Codec: "h264", + Coder: "libx264", + Frame: 39, + Keyframe: 433, + Framerate: struct { + Min float64 + Max float64 + Average float64 + }{ + Min: 47.0, + Max: 97.8, + Average: 463.9, + }, + FPS: 34.8, + Packet: 4737, + PPS: 473.8, + Size: 48474, + Bitrate: 38473, + Extradata: 4874, + Pixfmt: "none", + Quantizer: 2.3, + Width: 4848, + Height: 9373, + Sampling: 4733, + Layout: "atmos", + Channels: 83, + AVstream: &parse.AVstream{ + Input: parse.AVstreamIO{ + State: "running", + Packet: 484, + Time: 4373, + Size: 4783, + }, + Output: parse.AVstreamIO{ + State: "idle", + Packet: 4843, + Time: 483, + Size: 34, + }, + Aqueue: 8574, + Queue: 5877, + Dup: 473, + Drop: 463, + Enc: 474, + Looping: true, + LoopingRuntime: 347, + Duplicating: true, + GOP: "xxx", + Mode: "yyy", + Debug: nil, + Swap: parse.AVStreamSwap{ + URL: "ffdsjhhj", + Status: "none", + LastURL: "fjfd", + LastError: "none", + }, + }, + }, + }, + Mapping: parse.StreamMapping{ + Graphs: []parse.GraphElement{ + { + Index: 5, + Name: "foobar", + Filter: "infilter", + DstName: "outfilter_", + DstFilter: "outfilter", + Inpad: "inpad", + Outpad: "outpad", + Timebase: "100", + Type: "video", + Format: "yuv420p", + Sampling: 39944, + Layout: "atmos", + Width: 1029, + Height: 463, + }, + }, + Mapping: []parse.GraphMapping{ + { + Input: 1, + Output: 3, + Index: 39, + Name: "foobar", + Copy: true, + }, + }, + }, + Frame: 0, + Packet: 0, + FPS: 0, + PPS: 0, + Quantizer: 0, + Size: 0, + Time: 0, + Bitrate: 0, + Speed: 0, + Drop: 0, + Dup: 0, + } + + p := Progress{} + p.UnmarshalParser(&original) + restored := p.MarshalParser() + + require.Equal(t, original, restored) +}