Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

147 řádky
3.5KB

  1. package factory
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/jan/fm-rds-tx/internal/config"
  6. )
  7. func TestBuildSourceNone(t *testing.T) {
  8. cfg := config.Default()
  9. cfg.Ingest.Kind = "none"
  10. src, ingress, err := BuildSource(cfg, Deps{})
  11. if err != nil {
  12. t.Fatalf("build source: %v", err)
  13. }
  14. if src != nil || ingress != nil {
  15. t.Fatalf("expected nil source and ingress for kind=none")
  16. }
  17. }
  18. func TestBuildSourceHTTPRawProvidesIngress(t *testing.T) {
  19. cfg := config.Default()
  20. cfg.Ingest.Kind = "http-raw"
  21. src, ingress, err := BuildSource(cfg, Deps{})
  22. if err != nil {
  23. t.Fatalf("build source: %v", err)
  24. }
  25. if src == nil {
  26. t.Fatalf("expected source")
  27. }
  28. if ingress == nil {
  29. t.Fatalf("expected ingress for http-raw")
  30. }
  31. }
  32. func TestBuildSourceKindIsNormalized(t *testing.T) {
  33. cfg := config.Default()
  34. cfg.Ingest.Kind = " HTTP-RAW "
  35. src, ingress, err := BuildSource(cfg, Deps{})
  36. if err != nil {
  37. t.Fatalf("build source: %v", err)
  38. }
  39. if src == nil || ingress == nil {
  40. t.Fatalf("expected source and ingress for normalized http-raw kind")
  41. }
  42. if got := src.Descriptor().Kind; got != "http-raw" {
  43. t.Fatalf("source kind=%q want http-raw", got)
  44. }
  45. }
  46. func TestBuildSourceStdin(t *testing.T) {
  47. cfg := config.Default()
  48. cfg.Ingest.Kind = "stdin"
  49. src, ingress, err := BuildSource(cfg, Deps{Stdin: bytes.NewReader(nil)})
  50. if err != nil {
  51. t.Fatalf("build source: %v", err)
  52. }
  53. if src == nil {
  54. t.Fatalf("expected source")
  55. }
  56. if ingress != nil {
  57. t.Fatalf("expected no ingress for stdin")
  58. }
  59. if got := src.Descriptor().Kind; got != "stdin-pcm" {
  60. t.Fatalf("source kind=%s", got)
  61. }
  62. }
  63. func TestBuildSourceIcecastUsesDecoderPreference(t *testing.T) {
  64. cfg := config.Default()
  65. cfg.Ingest.Kind = "icecast"
  66. cfg.Ingest.Icecast.URL = "http://localhost:8000/stream"
  67. cfg.Ingest.Icecast.Decoder = "ffmpeg"
  68. src, ingress, err := BuildSource(cfg, Deps{})
  69. if err != nil {
  70. t.Fatalf("build source: %v", err)
  71. }
  72. if src == nil {
  73. t.Fatalf("expected source")
  74. }
  75. if ingress != nil {
  76. t.Fatalf("expected no ingress for icecast")
  77. }
  78. if got := src.Descriptor().Codec; got != "ffmpeg" {
  79. t.Fatalf("codec=%s want ffmpeg", got)
  80. }
  81. }
  82. func TestBuildSourceSRT(t *testing.T) {
  83. cfg := config.Default()
  84. cfg.Ingest.Kind = "srt"
  85. cfg.Ingest.SRT.URL = "srt://127.0.0.1:9000?mode=listener"
  86. cfg.Ingest.SRT.Mode = "listener"
  87. cfg.Ingest.SRT.SampleRateHz = 48000
  88. cfg.Ingest.SRT.Channels = 2
  89. src, ingress, err := BuildSource(cfg, Deps{})
  90. if err != nil {
  91. t.Fatalf("build source: %v", err)
  92. }
  93. if src == nil {
  94. t.Fatalf("expected source")
  95. }
  96. if ingress != nil {
  97. t.Fatalf("expected no ingress for srt")
  98. }
  99. if got := src.Descriptor().Kind; got != "srt" {
  100. t.Fatalf("source kind=%s", got)
  101. }
  102. }
  103. func TestBuildSourceUnsupportedKind(t *testing.T) {
  104. cfg := config.Default()
  105. cfg.Ingest.Kind = "nope"
  106. _, _, err := BuildSource(cfg, Deps{})
  107. if err == nil {
  108. t.Fatalf("expected error")
  109. }
  110. }
  111. func TestSampleRateForKind(t *testing.T) {
  112. cfg := config.Default()
  113. cfg.Ingest.Kind = "stdin"
  114. cfg.Ingest.Stdin.SampleRateHz = 48000
  115. if got := SampleRateForKind(cfg); got != 48000 {
  116. t.Fatalf("stdin sample rate=%d", got)
  117. }
  118. cfg.Ingest.Kind = "http-raw"
  119. cfg.Ingest.HTTPRaw.SampleRateHz = 32000
  120. if got := SampleRateForKind(cfg); got != 32000 {
  121. t.Fatalf("http-raw sample rate=%d", got)
  122. }
  123. cfg.Ingest.Kind = "icecast"
  124. if got := SampleRateForKind(cfg); got != 44100 {
  125. t.Fatalf("icecast sample rate=%d", got)
  126. }
  127. cfg.Ingest.Kind = "srt"
  128. cfg.Ingest.SRT.SampleRateHz = 48000
  129. if got := SampleRateForKind(cfg); got != 48000 {
  130. t.Fatalf("srt sample rate=%d", got)
  131. }
  132. }