github.com/Comcast/plax@v0.8.32/demos/mother.yaml (about) 1 doc: | 2 You start with a single channel named "mother". Send a message to 3 this channel to make another channel. 4 5 There's a new channel type: 'cmd'. A 'cmd' channel is an interface 6 to a subprocess. You can 'recv' lines (hopefully JSON) from a 7 subprocess, so you can parse those lines to make bindings in the 8 usual way. (You can also 'pub' lines to the subprocess's stdin.) 9 10 So the old 'start', 'initially', and 'finally' clauses are no longer 11 needed. 12 13 Bindings now have some magic properties. If a binding variable 14 starts with '?*', then that binding is cleared before each 'recv'. 15 So if you have a '?*' pattern variable in a 'recv', the resulting 16 binding cannot conflict when used in a subsequent 'recv'. 17 18 Additionally, a binding for a variable that starts with '?!' also 19 has a special property. A 'recv' step can have a 'clearbindings' 20 property, which (when true) will remove all bindings except those 21 for variables that start with '?!'. In other words, '?!' bindings 22 survive 'clearbindings'. 23 24 No more parameters! Instead, we just use bindings. Consider using 25 bindings for variables starting with '?!' (to survive any 26 'clearbindings' in 'recv' steps). You can set bindings on the Plax 27 command line almost as you did for parameters. The typical 28 difference is that you don't want the braces. Example: 29 30 plax -test demos/mother.yaml -p '?!WANT=chips' 31 32 That invocation will establish the binding of '?!WANT', and that 33 binding will override the default value specified in the 'bindings' 34 section below. 35 36 bindings: 37 '?!WANT': tacos 38 spec: 39 initialphase: mock-demo 40 phases: 41 mock-demo: 42 doc: | 43 We ask Mother to make us a mock channel. Then we test that 44 channel. 45 steps: 46 - pub: 47 doc: Please make a mock channel. 48 payload: 49 make: 50 name: mock1 51 type: mock 52 - recv: 53 doc: Check that our request succeeded. 54 chan: mother 55 pattern: 56 success: true 57 - pub: 58 payload: 59 hello: world 60 - recv: 61 pattern: 62 hello: world 63 - goto: cmd-demo 64 cmd-demo: 65 doc: | 66 Start a subprocess and then listen to it. 67 68 This subprocess will emit some JSON, and that JSON will 69 include the binding for '?!WANT'. 70 steps: 71 - pub: 72 chan: mother 73 payload: 74 make: 75 name: cmd1 76 type: cmd 77 config: 78 command: bash 79 doc: This command just emits one line. 80 args: 81 - '-c' 82 - 'echo "{\"want\":\"{?!WANT}\"}"' 83 - recv: 84 doc: Check that our request succeeded. 85 chan: mother 86 pattern: 87 success: true 88 - recv: 89 doc: | 90 Get what we expected (more or less). 91 92 We'll use a pattern variable starting with '?*' so that 93 we won't get a subsequent bindings conflict if we do a 94 subsequent 'recv' that happens to use the same variable 95 incidentally. 96 chan: cmd1 97 pattern: 98 want: '?*wanted' 99 - goto: mqtt-demo 100 mqtt-demo: 101 doc: | 102 Create and test an MQTT client channel. 103 104 This channel expects a local MQTT broker that allows anonymous 105 access. 106 steps: 107 - pub: 108 chan: mother 109 payload: 110 make: 111 name: mqtt1 112 type: mqtt 113 config: 114 clientid: plax1 115 brokerurl: tcp://localhost:1883 116 - recv: 117 chan: mother 118 pattern: 119 success: true 120 - sub: 121 chan: mqtt1 122 topic: want 123 - pub: 124 doc: | 125 Publish a message about what we '?!WANT'. 126 chan: mqtt1 127 topic: want 128 payload: 129 what: '?!WANT' 130 when: now 131 - recv: 132 doc: | 133 Receive our message and bind some variables that we 134 might use (intentionally) in a subsequent 'recv'. 135 chan: mqtt1 136 pattern: 137 what: "?what" 138 when: "?when" 139 - goto: echo-demo 140 echo-demo: 141 doc: | 142 Create a cmd (subprocess) channel that echos what we 'pub' to 143 it. 144 steps: 145 - pub: 146 chan: mother 147 payload: 148 make: 149 name: echo 150 type: cmd 151 config: 152 command: bash 153 doc: Just echo stdin to stdout. 154 args: 155 - '-c' 156 - 'while true; do read line; echo $line; done' 157 - recv: 158 chan: mother 159 pattern: 160 success: true 161 - pub: 162 doc: Send a message to the subprocess. 163 chan: echo 164 payload: 165 please: work 166 - recv: 167 doc: | 168 Verify we hear what we want, and establish a binding. 169 chan: echo 170 pattern: 171 please: "?work" 172 - run: | 173 // Check that we really have a binding for '?work'. 174 return test.Bindings["?work"] ? true : Failure("no '?work'"); 175 - pub: 176 doc: | 177 For fun, we use the previous binding for '?when'. 178 chan: echo 179 payload: 180 please: 'work again' 181 when: "?when" 182 - recv: 183 doc: | 184 Verify that we hear what we want. 185 186 Note that we are clearing bindings so that we have an 187 unbound '?work' going into the pattern matching. If we 188 didn't 'clearbindings', then this step would fail. 189 chan: echo 190 clearbindings: true 191 pattern: 192 please: "?work" 193 when: now