Add tests for Janus stream selection and simplify code.

This commit is contained in:
Joachim Bauch 2026-02-02 16:31:59 +01:00
commit 806ef1f564
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
3 changed files with 115 additions and 30 deletions

View file

@ -88,6 +88,7 @@ func TestPublisherStatsPrometheus(t *testing.T) {
t.Parallel()
RegisterStats()
UnregisterStats()
}
func TestPublisherStatsCounter(t *testing.T) {

View file

@ -22,35 +22,35 @@
package janus
import (
"database/sql"
"fmt"
"github.com/strukturag/nextcloud-spreed-signaling/api"
"github.com/strukturag/nextcloud-spreed-signaling/internal"
)
type streamSelection struct {
substream sql.NullInt16
temporal sql.NullInt16
audio sql.NullBool
video sql.NullBool
substream *int
temporal *int
audio *bool
video *bool
}
func (s *streamSelection) HasValues() bool {
return s.substream.Valid || s.temporal.Valid || s.audio.Valid || s.video.Valid
return s.substream != nil || s.temporal != nil || s.audio != nil || s.video != nil
}
func (s *streamSelection) AddToMessage(message api.StringMap) {
if s.substream.Valid {
message["substream"] = s.substream.Int16
if s.substream != nil {
message["substream"] = *s.substream
}
if s.temporal.Valid {
message["temporal"] = s.temporal.Int16
if s.temporal != nil {
message["temporal"] = *s.temporal
}
if s.audio.Valid {
message["audio"] = s.audio.Bool
if s.audio != nil {
message["audio"] = *s.audio
}
if s.video.Valid {
message["video"] = s.video.Bool
if s.video != nil {
message["video"] = *s.video
}
}
@ -59,14 +59,11 @@ func parseStreamSelection(payload api.StringMap) (*streamSelection, error) {
if value, found := payload["substream"]; found {
switch value := value.(type) {
case int:
stream.substream.Valid = true
stream.substream.Int16 = int16(value)
stream.substream = &value
case float32:
stream.substream.Valid = true
stream.substream.Int16 = int16(value)
stream.substream = internal.MakePtr(int(value))
case float64:
stream.substream.Valid = true
stream.substream.Int16 = int16(value)
stream.substream = internal.MakePtr(int(value))
default:
return nil, fmt.Errorf("unsupported substream value: %v", value)
}
@ -75,14 +72,11 @@ func parseStreamSelection(payload api.StringMap) (*streamSelection, error) {
if value, found := payload["temporal"]; found {
switch value := value.(type) {
case int:
stream.temporal.Valid = true
stream.temporal.Int16 = int16(value)
stream.temporal = &value
case float32:
stream.temporal.Valid = true
stream.temporal.Int16 = int16(value)
stream.temporal = internal.MakePtr(int(value))
case float64:
stream.temporal.Valid = true
stream.temporal.Int16 = int16(value)
stream.temporal = internal.MakePtr(int(value))
default:
return nil, fmt.Errorf("unsupported temporal value: %v", value)
}
@ -91,8 +85,7 @@ func parseStreamSelection(payload api.StringMap) (*streamSelection, error) {
if value, found := payload["audio"]; found {
switch value := value.(type) {
case bool:
stream.audio.Valid = true
stream.audio.Bool = value
stream.audio = &value
default:
return nil, fmt.Errorf("unsupported audio value: %v", value)
}
@ -101,8 +94,7 @@ func parseStreamSelection(payload api.StringMap) (*streamSelection, error) {
if value, found := payload["video"]; found {
switch value := value.(type) {
case bool:
stream.video.Valid = true
stream.video.Bool = value
stream.video = &value
default:
return nil, fmt.Errorf("unsupported video value: %v", value)
}

View file

@ -0,0 +1,92 @@
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2026 struktur AG
*
* @author Joachim Bauch <bauch@struktur.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package janus
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/strukturag/nextcloud-spreed-signaling/api"
)
func TestStreamSelection(t *testing.T) {
t.Parallel()
assert := assert.New(t)
testcases := []api.StringMap{
{},
{
"substream": 1.0,
},
{
"temporal": 1.0,
},
{
"substream": float32(1.0),
},
{
"temporal": float32(1.0),
},
{
"substream": 1,
"temporal": 3,
},
{
"substream": 1,
"audio": true,
"video": false,
},
}
for idx, tc := range testcases {
parsed, err := parseStreamSelection(tc)
if assert.NoError(err, "failed for testcase %d: %+v", idx, tc) {
assert.Equal(len(tc) > 0, parsed.HasValues(), "failed for testcase %d: %+v", idx, tc)
m := make(api.StringMap)
parsed.AddToMessage(m)
for k, v := range tc {
assert.EqualValues(v, m[k], "failed for key %s in testcase %d", k, idx)
}
}
}
_, err := parseStreamSelection(api.StringMap{
"substream": "foo",
})
assert.ErrorContains(err, "unsupported substream value")
_, err = parseStreamSelection(api.StringMap{
"temporal": "foo",
})
assert.ErrorContains(err, "unsupported temporal value")
_, err = parseStreamSelection(api.StringMap{
"audio": 1,
})
assert.ErrorContains(err, "unsupported audio value")
_, err = parseStreamSelection(api.StringMap{
"video": "true",
})
assert.ErrorContains(err, "unsupported video value")
}