Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to variable sequences and file based configuration #32

Merged
merged 7 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/IotTelemetrySimulator/TelemetryValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public Dictionary<string, object> NextValues(Dictionary<string, object> previous
// sequence vars might reference non-sequence vars.
if (hasSequenceVars)
fbeltrao marked this conversation as resolved.
Show resolved Hide resolved
{
var notUsedSequenceVariables = this.Variables
.Where(x => x.Sequence)
.SelectMany(x => x.GetReferenceVariableNames())
.ToHashSet();

foreach (var seqVar in this.Variables.Where(x => x.Sequence))
{
var value = seqVar.Values[iterationNumber % (ulong)seqVar.Values.Length];
Expand All @@ -106,14 +111,16 @@ public Dictionary<string, object> NextValues(Dictionary<string, object> previous
{
next[seqVar.Name] = value;
}

notUsedSequenceVariables.Remove(usedVariable);
}
else
{
next[seqVar.Name] = value;
}

ResetNonUsedReferencedVariables(previous, next, seqVar, usedVariable);
}

ResetNotUsedReferencedVariables(previous, next, notUsedSequenceVariables);
}

return next;
Expand All @@ -123,26 +130,21 @@ public Dictionary<string, object> NextValues(Dictionary<string, object> previous
/// Removes non-used variables in a sequence.
/// This way we can keep the a counter variable incrementally correctly if the sequence did not use it in current iteration.
/// </summary>
private static void ResetNonUsedReferencedVariables(
private static void ResetNotUsedReferencedVariables(
Dictionary<string, object> previous,
Dictionary<string, object> next,
TelemetryVariable sequenceVariable,
string usedVariable)
IEnumerable<string> notUserVariables)
fbeltrao marked this conversation as resolved.
Show resolved Hide resolved
{
var referencedVariables = sequenceVariable.GetReferenceVariableNames();
foreach (var referencedVariable in referencedVariables)
foreach (var notUsedVariable in notUserVariables)
{
if (referencedVariable != usedVariable)
// Restore it from the previous value.
if (previous != null && previous.TryGetValue(notUsedVariable, out var previousValue))
fbeltrao marked this conversation as resolved.
Show resolved Hide resolved
{
// Restore it from the previous value.
if (previous != null && previous.TryGetValue(referencedVariable, out var previousValue))
{
next[referencedVariable] = previousValue;
}
else
{
next.Remove(referencedVariable);
}
next[notUsedVariable] = previousValue;
}
else
{
next.Remove(notUsedVariable);
fbeltrao marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
131 changes: 131 additions & 0 deletions test/IotTelemetrySimulator.Test/PayloadGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,136 @@ public void Sequence_Generator()
Assert.Equal(expectedValue, Encoding.UTF8.GetString(result));
}
}

[Fact]
public void Sequence_With_Exchanging_Counters_Generator()
{
var telemetryTemplate = new TelemetryTemplate("{\"val1\":\"$.Value1\",\"val2\":\"$.Value2\"}", new[] { "Value1", "Value2", "Counter1", "Counter2" });
var telemetryVariables = new[]
{
new TelemetryVariable
{
Name = "Value1",
Sequence = true,
Values = new object[] { "$.Counter1", "$.Counter2" },
},

new TelemetryVariable
{
Name = "Value2",
Sequence = true,
Values = new object[] { "$.Counter2", "$.Counter1" },
},

new TelemetryVariable
{
Name = "Counter1",
Step = 1,
Min = 1
},

new TelemetryVariable
{
Name = "Counter2",
Step = 1,
Min = 1_001
}
};
var telemetryValues = new TelemetryValues(telemetryVariables);

var payload = new TemplatedPayload(100, telemetryTemplate, telemetryValues);

var target = new PayloadGenerator(new[] { payload }, new DefaultRandomizer());

var expectedValues = new[]
{
"{\"val1\":\"1\",\"val2\":\"1001\"}",
"{\"val1\":\"1002\",\"val2\":\"2\"}",
"{\"val1\":\"3\",\"val2\":\"1003\"}",
"{\"val1\":\"1004\",\"val2\":\"4\"}",
};

var variables = new Dictionary<string, object>
{
{ Constants.DeviceIdValueName, "mydevice" },
};

byte[] result;
foreach (var expectedValue in expectedValues)
{
(result, variables) = target.Generate(null, variables);
Assert.NotEmpty(variables);
Assert.Equal(expectedValue, Encoding.UTF8.GetString(result));
}
}

[Fact]
public void Sequence_With_Mixed_Counters_Generator()
{
var telemetryTemplate = new TelemetryTemplate("{\"val1\":\"$.Value1\",\"val2\":\"$.Value2\",\"counter_3\":\"$.Counter3\"}", new[] { "Value1", "Value2", "Counter1", "Counter2", "Counter3" });
var telemetryVariables = new[]
{
new TelemetryVariable
{
Name = "Value1",
Sequence = true,
Values = new object[] { "$.Counter1", "$.Counter2" },
},

new TelemetryVariable
{
Name = "Value2",
Sequence = true,
Values = new object[] { "$.Counter2", "$.Counter1" },
},

new TelemetryVariable
{
Name = "Counter1",
Step = 1,
Min = 1
},

new TelemetryVariable
{
Name = "Counter2",
Step = 1,
Min = 1_001
},

new TelemetryVariable
{
Name = "Counter3",
Step = 1,
Min = 1_000_001
}
};
var telemetryValues = new TelemetryValues(telemetryVariables);

var payload = new TemplatedPayload(100, telemetryTemplate, telemetryValues);

var target = new PayloadGenerator(new[] { payload }, new DefaultRandomizer());

var expectedValues = new[]
{
"{\"val1\":\"1\",\"val2\":\"1001\",\"counter_3\":\"1000001\"}",
"{\"val1\":\"1002\",\"val2\":\"2\",\"counter_3\":\"1000002\"}",
"{\"val1\":\"3\",\"val2\":\"1003\",\"counter_3\":\"1000003\"}",
"{\"val1\":\"1004\",\"val2\":\"4\",\"counter_3\":\"1000004\"}",
};

var variables = new Dictionary<string, object>
{
{ Constants.DeviceIdValueName, "mydevice" },
};

byte[] result;
foreach (var expectedValue in expectedValues)
{
(result, variables) = target.Generate(null, variables);
Assert.NotEmpty(variables);
Assert.Equal(expectedValue, Encoding.UTF8.GetString(result));
}
}
}
}