简体   繁体   中英

Embedded Erlang in Tsung.xml Dynamic Variable fails to parse for If expressions

I've been trying to remove some complications in a build process by bringing some external Erlang code into my Tsung config file (tsung.xml) and setting some dynamic variables with it, but have found that a few expressions just don't seem to work properly.

When trying to use If statements, cases or even function definitions (funs or normal), either Tsung complains with an "unexpected_char" message (but no mention of what possible char), or for the latter, Erlang returns a "syntax error before '[]'" parse error.

<setdynvars sourcetype="eval"
               code="fun({Pid,DynVars}) ->
                        {Var_Scale, ScaleUp} = {3, 100},
                        Random=random:uniform() * 100 * ScaleUp,
                        Compare=round(Var_Scale * ScaleUp),
                        if
                                 Random =< Compare ->
                                     Return = 1;
                                 true ->
                                     Return = 0
                        end,
                        Return.
                        ">
            <var name="someVariable" />

The same result occurs if the whole of the function expression is just the if expression:

<setdynvars sourcetype="eval"
               code="fun({Pid,DynVars}) ->
                        if
                            1 < 2 -> Return = 2;
                            true -> Return = 3
                        end.
                        ">
            <var name="anotherVariable" />
        </setdynvars>

And for an inlined or anonymous function

<setdynvars ...
  Compare=round(Var_Scale * ScaleUp),
  Fn = fun() -> a
  end.
">

which results in the aforementioned parse error.

I'm kind of new to Erlang and Tsung, so perhaps I've misinterpreted how these commands need to be structured, or what Tsung's eval in ts_utils needs to be fed. It seems to use the standard Erlang scan, parse and eval methods, so I'd imagine that it most certainly supports branching operations and functions.

So my question itself boils down to: Is this code (syntactically) correct and does Tsung simply not support this, and secondly, is there a better or more idiomatically correct way of doing this?

I'd guess it's unhappy about < and > characters being embedded in XML attributes. Try entering them as &lt; and &gt; . Also, unlike named functions a fun needs a matching end token:

<setdynvars sourcetype="eval"
               code="fun({Pid,DynVars}) -&gt;
                        if
                            1 &lt; 2 -&gt; Return = 2;
                            true -&gt; Return = 3
                        end
                     end.
                        ">
            <var name="anotherVariable" />
        </setdynvars>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM