<body>
<p>Test that MessagePort messages are delivered even if both ports are in transit (cloned).</p>
<p>Should say PASS twice.</p>
<pre id=log></pre>
<script>
function gc()
{
    if (window.GCController)
        return GCController.collect();

    for (var i = 0; i < 10000; i++) { // force garbage collection (FF requires about 9K allocations before a collect)
        var s = new String("abc");
    }
}

function log(message)
{
    document.getElementById("log").innerHTML += message + "<br>";
}

if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

function test1()
{
    var channel = new MessageChannel;
    var channel2 = new MessageChannel;
    channel.port1.postMessage("msg1");
    channel2.port1.postMessage("", [channel.port1]);
    channel2.port2.postMessage("", [channel.port2]);
    channel2.port2.onmessage = channel2.port1.onmessage = function(evt) {
        evt.ports[0].onmessage = function(event) {
            if (event.data == "msg1") {
                log("PASS: Received message to cloned port.");
                this.postMessage("msg2");
                gc();
            } else if (event.data == "msg2") {
                log("PASS: Received message from cloned port.");
                gc();
                if (window.testRunner)
                    testRunner.notifyDone();
            } else {
                log("FAIL: Received unknown message: " + event.data);
            }
        }
        evt.ports[0] = 0;
    }
    channel = 0;
    channel2 = 0;
    gc();
}

test1();

</script>
</body>
