 # Test handing off a populated rewind buffer and active source from one
 # master to another, as we would during a graceful restart. Test that
 # the new master is listening on the correct ports.
+
+debug = require("debug")("sm:tests:master_handoffs")
 
 MasterMode      = $src "modes/master"
 IcecastSource   = $src "util/icecast_source"

        m2config = null

        before (done) ->
            this.timeout 5000
            m2 = cp.fork "./index.js", ["--handoff",master_config...]

            m2rpc = new RPC m2, functions:


    describe "Initial Master", ->
        before (done) ->
            this.timeout 10000
            m1 = cp.fork "./index.js", master_config

            new RPC m1, (err,r) ->
                m1rpc = r

                debug "Starting loop for m1 OK"

                tries = 0

                okF = ->
                    tries += 1
                    m1rpc.request "OK", null, null, timeout:500, (err,msg) ->
                        if err
                            debug "m1 OK error. Tries: #{tries}"
                            okF() if tries < 20
                        else
                            debug "m1 OK success after #{tries} tries"
                            done()

                okF()

        it "is listening on a master port", (done) ->
            m1rpc.request "master_port", (err,port) ->
