[meta]
    title = "test workflow for broadcast functionality"
    description = """
        The first task broadcasts an environment variable "BCAST" to various
        cycles and namespaces. Then each task writes its point, name, and value
        of BCAST to a log for comparison with the expected result.
    """

[scheduler]
    cycle point format = %Y%m%dT%H
    allow implicit tasks = True

[scheduling]
    initial cycle point = 20100808T00
    final cycle point   = 20100809T00
    # Ensure the first cycle finishes before the second (last) one, so that the
    # broadcast to foo in the first cycle expires before shutdown:
    runahead limit = P0
    [[graph]]
        R1 = "prep => check & foo"
        T00 = """
            foo => bar & baz & qux & wibble => ENS?
            ENS:finish-all => wobble
        """

[runtime]
    [[root]]
        pre-script = "echo $CYLC_TASK_CYCLE_POINT $CYLC_TASK_NAME BCAST is $BCAST | tee -a $BCASTLOG"
        script = "true" # no sleep
        [[[environment]]]
            BCAST = ${BCAST:-(not set)}
            BCASTLOG = ${CYLC_WORKFLOW_SHARE_DIR}/broadcast.log
            PREPLOG = ${CYLC_WORKFLOW_SHARE_DIR}/prep
    [[prep]]
        pre-script = "rm -f $BCASTLOG $PREPLOG"
        script = """
            set +x
            {
                # broadcast to all cycles and namespaces:
                    cylc broadcast -s "[environment]BCAST = ROOT" $CYLC_WORKFLOW_ID
                # broadcast to 20100808T00/foo:
                    cylc broadcast -p 20100808T00 -n foo -s "[environment]BCAST = FOO" $CYLC_WORKFLOW_ID
                # broadcast to bar at all cycles:
                    cylc broadcast -n bar -s "[environment]BCAST = BAR" $CYLC_WORKFLOW_ID
                # broadcast to baz at 20100809T00:
                    cylc broadcast -n baz -p 20100809T00 -s "[environment]BCAST = BAZ" $CYLC_WORKFLOW_ID
                # broadcast to qux at 20100809T00, then cancel it:
                    cylc broadcast -n qux -p 20100809T00 -s "[environment]BCAST = QUX" $CYLC_WORKFLOW_ID
                    cylc broadcast -n qux -p 20100809T00 --cancel "[environment]BCAST" $CYLC_WORKFLOW_ID
                # broadcast to wibble at all cycles, then clear it:
                    cylc broadcast -n wibble -s "[environment]BCAST = WIBBLE" $CYLC_WORKFLOW_ID
                    cylc broadcast -n wibble --clear $CYLC_WORKFLOW_ID
                # broadcast to all members of ENS, all cycles:
                    cylc broadcast -n ENS -s "[environment]BCAST = ENS" $CYLC_WORKFLOW_ID
                # broadcast to all members of ENS1, all cycles:
                    cylc broadcast -n ENS1 -s "[environment]BCAST = ENS1" $CYLC_WORKFLOW_ID
                # broadcast to a single member m2 of ENS1, in 20100809T00:
                    cylc broadcast -n m2 -p 20100809T00 -s "[environment]BCAST = M2" $CYLC_WORKFLOW_ID
                # cancel broadcast to m4 of ENS1, in 20100809T00 (will not work):
                    ! cylc broadcast -n m4 -p 20100809T00 --cancel "[environment]BCAST" $CYLC_WORKFLOW_ID
                # cancel broadcast to m5 of ENS1 at all cycles (will not work):
                    ! cylc broadcast -n m5 --cancel "[environment]BCAST" $CYLC_WORKFLOW_ID
                # clear broadcast to m6 of ENS1 at all cycles (will not work):
                    ! cylc broadcast -n m6 --clear $CYLC_WORKFLOW_ID
                # clear, then reset, broadcast to m7 of ENS1 at all cycles:
                    ! cylc broadcast -n m7 --clear $CYLC_WORKFLOW_ID
                    cylc broadcast -n m7 -s "[environment]BCAST = M7" $CYLC_WORKFLOW_ID
                # reset broadcast to m8 of ENS1 at 20100809T00
                    cylc broadcast -n m8 -s "[environment]BCAST = M8" $CYLC_WORKFLOW_ID
                # reset broadcast to m9 of ENS1 at all cycles
                    cylc broadcast -n m9 -s "[environment]BCAST = M9" $CYLC_WORKFLOW_ID
                # clear broadcast for ENS3 (will not work):
                    ! cylc broadcast -n ENS3 --clear $CYLC_WORKFLOW_ID
            } 1>${PREPLOG}.out 2>${PREPLOG}.err
        """
    [[check]]
        # Check that the broadcasts performed by the previous task were
        # recorded properly by the scheduler (doing this in another task
        # gives time for the datastore to update broadcast data).
        script = """
           # list the result to prep task stdout:
           cylc broadcast --display $CYLC_WORKFLOW_ID \
             1>>${PREPLOG}.out 2>>${PREPLOG}.err
           set -x
           sed -i '/DEBUG -/d' ${PREPLOG}.out
           sed -i '/\(DEBUG\|WARNING\|ERROR\) -/d' ${PREPLOG}.err

           # workaround for platforms affected by https://github.com/cylc/cylc-flow/issues/3585
           sed -i '/BASH_XTRACEFD/d' ${PREPLOG}.err

           diff -u "${CYLC_WORKFLOW_RUN_DIR}/expected-prep.out" ${PREPLOG}.out
           diff -u "${CYLC_WORKFLOW_RUN_DIR}/expected-prep.err" ${PREPLOG}.err
        """
    [[ENS]]
    [[ENS1]]
        inherit = ENS
    [[m1,m2,m3,m4,m5,m6,m7,m8,m9]]
        inherit = ENS1
    [[ENS2]]
        inherit = ENS
    [[n1]]
        inherit = ENS2
    [[ENS3]]
        inherit = ENS
    [[o1]]
        inherit = ENS3
    [[wobble]]
        script = """
            if [[ "${CYLC_TASK_CYCLE_POINT}" == "20100809T00" ]]; then
                    sleep 5
            fi
        """
