Skip to main content

Selection functions

stateMachine

Commentary

added in 0.0.5

stateful function

Generates data according to a set of states and transitions.

For many use cases, it's common that each successive state needs to merge its previously generated event. Top-level state machines may be configured with "merge": { "previous": true } to enable this behavior. See the examples below.


Examples

Basic state machine

{
"_gen": "stateMachine",
"initial": "a",
"transitions": {
"a": "b",
"b": "c",
"c": "a"
},
"states": {
"a": 1,
"b": 2,
"c": 3
}
}
[
1,
2,
3,
1,
2
]

Top-level state machine

An optional top-level state machine may be defined for the generator. When present, the value of it's state is merged into the entire generator output. This is useful when modeling change that affects many attributes, even including the output collection.

{
"generators": [
{
"topic": "customers",
"vars": {
"id": {
"_gen": "uniformDistribution",
"bounds": [
1,
10
]
}
},
"key": {
"_gen": "var",
"var": "id"
},
"value": {},
"stateMachine": {
"_gen": "stateMachine",
"initial": "a",
"transitions": {
"a": "b",
"b": "c",
"c": "a"
},
"states": {
"a": {
"value": {
"action": "start"
}
},
"b": {
"value": {
"action": "running"
}
},
"c": {
"value": {
"action": "stopped"
}
}
}
}
}
],
"connections": {
"kafka": {
"kind": "kafka",
"producerConfigs": {
"bootstrap.servers": "localhost:9092",
"key.serializer": "io.shadowtraffic.kafka.serdes.JsonSerializer",
"value.serializer": "io.shadowtraffic.kafka.serdes.JsonSerializer"
}
}
}
}
[
{
"topic": "customers",
"key": 8.344376981444391,
"value": {
"action": "start"
}
},
{
"topic": "customers",
"key": 4.554659728849531,
"value": {
"action": "running"
}
},
{
"topic": "customers",
"key": 9.253903352598465,
"value": {
"action": "stopped"
}
},
{
"topic": "customers",
"key": 7.6556433452045285,
"value": {
"action": "start"
}
},
{
"topic": "customers",
"key": 8.637752167404386,
"value": {
"action": "running"
}
}
]

Merging previous events

Top-level state machines can force their previously generated events into their body.

{
"generators": [
{
"topic": "sandbox",
"value": {
"a": 1
},
"stateMachine": {
"_gen": "stateMachine",
"initial": "s1",
"merge": {
"previous": true
},
"transitions": {
"s1": "s2",
"s2": "s3"
},
"states": {
"s1": {
"value": {
"b": 2
}
},
"s2": {
"value": {
"c": 3
}
},
"s3": {
"value": {
"d": 4
}
}
}
}
}
],
"connections": {
"kafka": {
"kind": "kafka",
"producerConfigs": {
"bootstrap.servers": "localhost:9092",
"key.serializer": "io.shadowtraffic.kafka.serdes.JsonSerializer",
"value.serializer": "io.shadowtraffic.kafka.serdes.JsonSerializer"
}
}
}
}
[
{
"topic": "sandbox",
"key": null,
"value": {
"a": 1,
"b": 2
}
},
{
"topic": "sandbox",
"key": null,
"value": {
"a": 1,
"b": 2,
"c": 3
}
},
{
"topic": "sandbox",
"key": null,
"value": {
"a": 1,
"b": 2,
"c": 3,
"d": 4
}
}
]

Specification

JSON schema

{
"type": "object",
"properties": {
"initial": {
"type": "string"
},
"merge": {
"type": "object",
"properties": {
"previous": {
"type": "boolean"
}
},
"required": [
"previous"
]
},
"transitions": {
"type": "object"
},
"states": {
"type": "object"
}
},
"required": [
"initial",
"transitions",
"states"
]
}