From ddea35e23596e32255222a001538571e335055af Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 25 Aug 2013 08:22:20 +0000 Subject: tests: use unix_client in tests to kill some redundant code This should make porting to different Unix socket types easier, too. Much of these tests were written before I decided to implement unix_client originally. --- test/player_integration.rb | 22 +++------ test/test_format_change.rb | 12 ++--- test/test_player_integration.rb | 95 ++++++++++++++------------------------- test/test_rg_integration.rb | 64 +++++++++----------------- test/test_sink_tee_integration.rb | 6 +-- 5 files changed, 66 insertions(+), 133 deletions(-) diff --git a/test/player_integration.rb b/test/player_integration.rb index 6580194..72f6039 100644 --- a/test/player_integration.rb +++ b/test/player_integration.rb @@ -4,6 +4,7 @@ require './test/helper' require 'dtas/player' require 'dtas/state_file' +require 'dtas/unix_client' require 'yaml' require 'tempfile' require 'shellwords' @@ -46,18 +47,8 @@ module PlayerIntegration end end - module PlayerClient - def preq(args) - args = Shellwords.join(args) if Array === args - send(args, Socket::MSG_EOR) - end - end - def client_socket - s = Socket.new(:AF_UNIX, :SOCK_SEQPACKET, 0) - s.connect(Socket.pack_sockaddr_un(@sock_path)) - s.extend(PlayerClient) - s + DTAS::UNIXClient.new(@sock_path) end def wait_pid_dead(pid, time = 5) @@ -79,8 +70,7 @@ module PlayerIntegration def default_sink_pid(s) default_pid = Tempfile.new(%w(dtas-test .pid)) pf = "echo $$ >> #{default_pid.path}; " - s.send("sink ed default command='#{pf}#@cmd'", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) + s.req_ok("sink ed default command='#{pf}#@cmd'") default_pid end @@ -108,13 +98,11 @@ module PlayerIntegration end def dethrottle_decoder(s) - s.send("sink ed default active=false", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) + s.req_ok("sink ed default active=false") end def stop_playback(pid_file, s) - s.send("skip", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) + s.req_ok("skip") pid = read_pid_file(pid_file) wait_pid_dead(pid) end diff --git a/test/test_format_change.rb b/test/test_format_change.rb index ed0e7a2..b6edd7e 100644 --- a/test/test_format_change.rb +++ b/test/test_format_change.rb @@ -13,15 +13,12 @@ class TestFormatChange < Minitest::Unit::TestCase d = "#{dir}/dump.$CHANNELS.$RATE" f44100 = File.open("#{dir}/dump.2.44100", IO::RDWR|IO::CREAT) f88200 = File.open("#{dir}/dump.2.88200", IO::RDWR|IO::CREAT) - s.preq("sink ed dump active=true command='cat > #{d}'") - assert_equal "OK", s.readpartial(666) + s.req_ok("sink ed dump active=true command='cat > #{d}'") noise, len = tmp_noise - s.preq(%W(enq #{noise.path})) - assert_equal "OK", s.readpartial(666) + s.req_ok(%W(enq #{noise.path})) wait_files_not_empty(default_pid, f44100) - s.preq("format rate=88200") - assert_equal "OK", s.readpartial(666) + s.req_ok("format rate=88200") wait_files_not_empty(f88200) @@ -29,8 +26,7 @@ class TestFormatChange < Minitest::Unit::TestCase Timeout.timeout(len) do begin - s.preq("current") - cur = YAML.load(s.readpartial(6666)) + cur = YAML.load(s.req("current")) end while cur["sinks"] && sleep(0.01) end diff --git a/test/test_player_integration.rb b/test/test_player_integration.rb index 4ce0752..8f44b49 100644 --- a/test/test_player_integration.rb +++ b/test/test_player_integration.rb @@ -22,9 +22,8 @@ class TestPlayerIntegration < Minitest::Unit::TestCase default_pid = default_sink_pid(s) Tempfile.open('junk') do |junk| pink = "sox -n $SOXFMT - synth 0.0001 pinknoise | tee -i #{junk.path}" - s.send("enq-cmd \"#{pink}\"", Socket::MSG_EOR) + s.req_ok("enq-cmd \"#{pink}\"") wait_files_not_empty(junk) - assert_equal "OK", s.readpartial(666) end wait_files_not_empty(default_pid) pid = read_pid_file(default_pid) @@ -36,8 +35,7 @@ class TestPlayerIntegration < Minitest::Unit::TestCase default_pid = default_sink_pid(s) cmd = Tempfile.new(%w(sox-cmd .pid)) pink = "echo $$ > #{cmd.path}; sox -n $SOXFMT - synth 100 pinknoise" - s.send("enq-cmd \"#{pink}\"", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) + s.req_ok("enq-cmd \"#{pink}\"") wait_files_not_empty(cmd, default_pid) pid = read_pid_file(default_pid) Process.kill(:KILL, pid) @@ -47,31 +45,28 @@ class TestPlayerIntegration < Minitest::Unit::TestCase def test_sink_activate s = client_socket - s.send("sink ls", Socket::MSG_EOR) - assert_equal "default", s.readpartial(666) + res = s.req("sink ls") + assert_equal "default", res # setup two outputs # make the default sink trickle default_pid = Tempfile.new(%w(dtas-test .pid)) pf = "echo $$ >> #{default_pid.path}; " - s.send("sink ed default command='#{pf}#@cmd'", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) + s.req_ok("sink ed default command='#{pf}#@cmd'") # make a sleepy sink trickle, too sleepy_pid = Tempfile.new(%w(dtas-test .pid)) pf = "echo $$ >> #{sleepy_pid.path};" - s.send("sink ed sleepy command='#{pf}#@cmd' active=true", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) + s.req_ok("sink ed sleepy command='#{pf}#@cmd' active=true") # ensure both sinks were created - s.send("sink ls", Socket::MSG_EOR) - assert_equal "default sleepy", s.readpartial(666) + res = s.req("sink ls") + assert_equal "default sleepy", res # generate pinknoise pinknoise = "sox -n -r 44100 -c 2 -t s32 - synth 0 pinknoise" - s.send("enq-cmd \"#{pinknoise}\"", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) + s.req_ok("enq-cmd \"#{pinknoise}\"") # wait for sinks to start wait_files_not_empty(sleepy_pid, default_pid) @@ -80,8 +75,7 @@ class TestPlayerIntegration < Minitest::Unit::TestCase sleepy = File.read(sleepy_pid).to_i assert_operator sleepy, :>, 0 Process.kill(0, sleepy) - s.send("sink ed sleepy active=false", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) + s.req_ok("sink ed sleepy active=false") wait_pid_dead(sleepy) # ensure default sink is still alive @@ -93,8 +87,7 @@ class TestPlayerIntegration < Minitest::Unit::TestCase sleepy_pid.sync = true sleepy_pid.seek(0) sleepy_pid.truncate(0) - s.send("sink ed sleepy active=true", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) + s.req_ok("sink ed sleepy active=true") # wait for sleepy sink wait_files_not_empty(sleepy_pid) @@ -105,8 +98,7 @@ class TestPlayerIntegration < Minitest::Unit::TestCase Process.kill(0, sleepy) # stop playing current track - s.send("skip", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) + s.req_ok("skip") wait_pid_dead(sleepy) wait_pid_dead(default) @@ -115,22 +107,17 @@ class TestPlayerIntegration < Minitest::Unit::TestCase def test_env_change s = client_socket tmp = Tempfile.new(%w(env .txt)) - s.preq("sink ed default active=true command='cat >/dev/null'") - assert_equal "OK", s.readpartial(666) + s.req_ok("sink ed default active=true command='cat >/dev/null'") - s.preq("env FOO=BAR") - assert_equal "OK", s.readpartial(666) - s.preq(["enq-cmd", "echo $FOO | tee #{tmp.path}"]) - assert_equal "OK", s.readpartial(666) + s.req_ok("env FOO=BAR") + s.req_ok(["enq-cmd", "echo $FOO | tee #{tmp.path}"]) wait_files_not_empty(tmp) assert_equal "BAR\n", tmp.read tmp.rewind tmp.truncate(0) - s.preq("env FOO#") - assert_equal "OK", s.readpartial(666) - s.preq(["enq-cmd", "echo -$FOO- | tee #{tmp.path}"]) - assert_equal "OK", s.readpartial(666) + s.req_ok("env FOO#") + s.req_ok(["enq-cmd", "echo -$FOO- | tee #{tmp.path}"]) wait_files_not_empty(tmp) assert_equal "--\n", tmp.read end @@ -138,31 +125,25 @@ class TestPlayerIntegration < Minitest::Unit::TestCase def test_sink_env s = client_socket tmp = Tempfile.new(%w(env .txt)) - s.preq("sink ed default active=true command='echo -$FOO- > #{tmp.path}'") - assert_equal "OK", s.readpartial(666) + s.req_ok("sink ed default active=true command='echo -$FOO- > #{tmp.path}'") - s.preq("sink ed default env.FOO=BAR") - assert_equal "OK", s.readpartial(666) - s.preq(["enq-cmd", "echo HI"]) - assert_equal "OK", s.readpartial(666) + s.req_ok("sink ed default env.FOO=BAR") + s.req_ok(["enq-cmd", "echo HI"]) wait_files_not_empty(tmp) assert_equal "-BAR-\n", tmp.read tmp.rewind tmp.truncate(0) - s.preq("sink ed default env#FOO") - assert_equal "OK", s.readpartial(666) + s.req_ok("sink ed default env#FOO") Timeout.timeout(5) do begin - s.preq("current") - yaml = s.readpartial(66666) + yaml = s.req("current") cur = YAML.load(yaml) end while cur["sinks"] && sleep(0.01) end - s.preq(["enq-cmd", "echo HI"]) - assert_equal "OK", s.readpartial(666) + s.req_ok(["enq-cmd", "echo HI"]) wait_files_not_empty(tmp) assert_equal "--\n", tmp.read end @@ -171,15 +152,11 @@ class TestPlayerIntegration < Minitest::Unit::TestCase s = client_socket default_sink_pid(s) dump = Tempfile.new(%W(d .sox)) - s.preq "sink ed dump active=true command='sox $SOXFMT - #{dump.path}'" - assert_equal "OK", s.readpartial(666) + s.req_ok "sink ed dump active=true command='sox $SOXFMT - #{dump.path}'" noise, len = tmp_noise - s.preq("enq-head #{noise.path}") - assert_equal "OK", s.readpartial(666) - s.preq("enq-head #{noise.path} 4") - assert_equal "OK", s.readpartial(666) - s.preq("enq-head #{noise.path} 3") - assert_equal "OK", s.readpartial(666) + s.req_ok("enq-head #{noise.path}") + s.req_ok("enq-head #{noise.path} 4") + s.req_ok("enq-head #{noise.path} 3") dethrottle_decoder(s) expect = Tempfile.new(%W(expect .sox)) @@ -188,8 +165,7 @@ class TestPlayerIntegration < Minitest::Unit::TestCase assert system(c) Timeout.timeout(len) do begin - s.preq("current") - yaml = s.readpartial(66666) + yaml = s.req("current") cur = YAML.load(yaml) end while cur["sinks"] && sleep(0.01) end @@ -201,20 +177,15 @@ class TestPlayerIntegration < Minitest::Unit::TestCase s = client_socket pwd = Dir.pwd - s.preq("pwd") - assert_equal pwd, s.readpartial(6666) + assert_equal pwd, s.req("pwd") - s.preq("cd /") - assert_equal "OK", s.readpartial(6) + s.req_ok("cd /") - s.preq("pwd") - assert_equal "/", s.readpartial(6) + assert_equal "/", s.req("pwd") - s.preq("cd /this-better-be-totally-non-existent-on-any-system-#{rand}") - err = s.readpartial(666) + err = s.req("cd /this-better-be-totally-non-existent-on-any-system-#{rand}") assert_match(%r{\AERR }, err, err) - s.preq("pwd") - assert_equal "/", s.readpartial(6) + assert_equal "/", s.req("pwd") end end diff --git a/test/test_rg_integration.rb b/test/test_rg_integration.rb index 551fd78..0d2ac09 100644 --- a/test/test_rg_integration.rb +++ b/test/test_rg_integration.rb @@ -28,19 +28,16 @@ class TestRgIntegration < Minitest::Unit::TestCase dump_pid = Tempfile.new(%w(dump .pid)) default_pid = default_sink_pid(s) dump_cmd = "echo $$ > #{dump_pid.path}; sox $SOXFMT - #{dumper.path}" - s.send("sink ed dump active=true command='#{dump_cmd}'", Socket::MSG_EOR) - assert_equal("OK", s.readpartial(666)) + s.req_ok("sink ed dump active=true command='#{dump_cmd}'") # start playback! - s.send("enq \"#{pluck.path}\"", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) + s.req_ok("enq \"#{pluck.path}\"") # wait for playback to start yaml = cur = nil Timeout.timeout(5) do begin - s.send("current", Socket::MSG_EOR) - cur = YAML.load(yaml = s.readpartial(1666)) + cur = YAML.load(yaml = s.req("current")) end while cur["current_offset"] == 0 && sleep(0.01) end @@ -52,12 +49,11 @@ class TestRgIntegration < Minitest::Unit::TestCase pid = read_pid_file(dump_pid) check_gain = proc do |expect, mode| - s.send("rg mode=#{mode}", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) + s.req_ok("rg mode=#{mode}") Timeout.timeout(5) do begin - s.send("current", Socket::MSG_EOR) - cur = YAML.load(yaml = s.readpartial(3666)) + yaml = s.req("current") + cur = YAML.load(yaml) end while cur["current"]["env"]["RGFX"] !~ expect && sleep(0.01) end assert_match expect, cur["current"]["env"]["RGFX"] @@ -68,40 +64,28 @@ class TestRgIntegration < Minitest::Unit::TestCase check_gain.call(%r{vol 1\.3}, "track_peak") check_gain.call(%r{vol 1\.0}, "album_peak") - s.send("rg preamp+=1", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) - s.send("rg", Socket::MSG_EOR) - rg = YAML.load(yaml = s.readpartial(3666)) + s.req_ok("rg preamp+=1") + rg = YAML.load(yaml = s.req("rg")) assert_equal 1, rg["preamp"] - s.send("rg preamp-=1", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) - s.send("rg", Socket::MSG_EOR) - rg = YAML.load(yaml = s.readpartial(3666)) + s.req_ok("rg preamp-=1") + rg = YAML.load(yaml = s.req("rg")) assert_nil rg["preamp"] - s.send("rg preamp=2", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) - s.send("rg", Socket::MSG_EOR) - rg = YAML.load(yaml = s.readpartial(3666)) + s.req_ok("rg preamp=2") + rg = YAML.load(yaml = s.req("rg")) assert_equal 2, rg["preamp"] - s.send("rg preamp-=0.3", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) - s.send("rg", Socket::MSG_EOR) - rg = YAML.load(yaml = s.readpartial(3666)) + s.req_ok("rg preamp-=0.3") + rg = YAML.load(yaml = s.req("rg")) assert_equal 1.7, rg["preamp"] - s.send("rg preamp-=-0.3", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) - s.send("rg", Socket::MSG_EOR) - rg = YAML.load(yaml = s.readpartial(3666)) + s.req_ok("rg preamp-=-0.3") + rg = YAML.load(yaml = s.req("rg")) assert_equal 2.0, rg["preamp"] - s.send("rg preamp-=+0.3", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) - s.send("rg", Socket::MSG_EOR) - rg = YAML.load(yaml = s.readpartial(3666)) + s.req_ok("rg preamp-=+0.3") + rg = YAML.load(yaml = s.req("rg")) assert_equal 1.7, rg["preamp"] dethrottle_decoder(s) @@ -117,17 +101,13 @@ class TestRgIntegration < Minitest::Unit::TestCase def test_rg_env_in_source s = client_socket - s.preq("rg mode=album_gain") - assert_equal "OK", s.readpartial(666) + s.req_ok("rg mode=album_gain") pluck, _ = tmp_pluck cmd = DTAS::Source::SOURCE_DEFAULTS["command"] fifo = tmpfifo - s.preq("source ed command='env > #{fifo}; #{cmd}'") - assert_equal "OK", s.readpartial(666) - s.preq("sink ed default command='cat >/dev/null' active=true") - assert_equal "OK", s.readpartial(666) - s.preq(%W(enq #{pluck.path})) - assert_equal "OK", s.readpartial(666) + s.req_ok("source ed command='env > #{fifo}; #{cmd}'") + s.req_ok("sink ed default command='cat >/dev/null' active=true") + s.req_ok(%W(enq #{pluck.path})) rg = {} File.readlines(fifo).each do |line| diff --git a/test/test_sink_tee_integration.rb b/test/test_sink_tee_integration.rb index 8c20adb..082c1d5 100644 --- a/test/test_sink_tee_integration.rb +++ b/test/test_sink_tee_integration.rb @@ -15,11 +15,9 @@ class TestSinkTeeIntegration < Minitest::Unit::TestCase cmd = "echo $$ > #{tee_pid.path}; " \ "cat /dev/fd/a > #{ajunk.path} & " \ "cat /dev/fd/b > #{bjunk.path}; wait" - s.send("sink ed split active=true command='#{cmd}'", Socket::MSG_EOR) - assert_equal("OK", s.readpartial(666)) + s.req_ok("sink ed split active=true command='#{cmd}'") pluck = "sox -n $SOXFMT - synth 3 pluck | tee #{orig.path}" - s.send("enq-cmd \"#{pluck}\"", Socket::MSG_EOR) - assert_equal "OK", s.readpartial(666) + s.req_ok("enq-cmd \"#{pluck}\"") wait_files_not_empty(tee_pid) pid = read_pid_file(tee_pid) -- cgit v1.2.3-24-ge0c7