Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ public async Task ProcessMapResultWithTargetInvalidAsync()
.SendEventTo(new ProcessFunctionTargetBuilder(mapStep));

// CountStep is not part of the map operation, rather it has been defined on the "outer" process.
CommonSteps.CountStep.Index = 0; // Reset static state (test hack)
ProcessStepBuilder countStep = process.AddStepFromType<CommonSteps.CountStep>();
ProcessStepBuilder countStep = process.AddStepFromType<CommonSteps.CountStep>(name: nameof(ProcessMapResultWithTargetInvalidAsync));
mapStep.MapOperation
.OnEvent(ComputeStep.SquareEventId)
.SendEventTo(new ProcessFunctionTargetBuilder(countStep));
Expand All @@ -287,7 +286,6 @@ public async Task ProcessMapResultWithTargetInvalidAsync()
public async Task ProcessMapResultWithTargetExtraAsync()
{
// Arrange
CommonSteps.CountStep.Index = 0;
ProcessBuilder process = new(nameof(ProcessMapResultProcessOperationAsync));

ProcessBuilder mapProcess = new("MapOperation");
Expand All @@ -296,7 +294,8 @@ public async Task ProcessMapResultWithTargetExtraAsync()
.OnInputEvent("Anything")
.SendEventTo(new ProcessFunctionTargetBuilder(computeStep));

ProcessStepBuilder countStep = mapProcess.AddStepFromType<CommonSteps.CountStep>();
const string CounterName = nameof(ProcessMapResultWithTargetExtraAsync);
ProcessStepBuilder countStep = mapProcess.AddStepFromType<CommonSteps.CountStep>(name: CounterName);
computeStep
.OnEvent(ComputeStep.SquareEventId)
.SendEventTo(new ProcessFunctionTargetBuilder(countStep));
Expand All @@ -320,7 +319,7 @@ public async Task ProcessMapResultWithTargetExtraAsync()
// Assert
UnionState unionState = await GetUnionStateAsync(processContext);
Assert.Equal(55L, unionState.SquareResult);
Assert.Equal(5, CommonSteps.CountStep.Index);
Assert.Equal(5, CommonSteps.CountStep.GetCount(CounterName));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task ProcessWithProxyWithSingleTopicCalledTwiceAsync()
var mockProxyClient = new MockCloudEventClient();
ProcessBuilder process = new(nameof(ProcessWithProxyWithSingleTopicCalledTwiceAsync));

var counterStep = process.AddStepFromType<CommonSteps.CountStep>();
var counterStep = process.AddStepFromType<CommonSteps.CountStep>(name: nameof(ProcessWithProxyWithSingleTopicCalledTwiceAsync));
var proxyStep = process.AddProxyStep([this._topic1, this._topic2]);

process.OnInputEvent(this._startProcessEvent).SendEventTo(new(counterStep));
Expand Down Expand Up @@ -75,7 +75,7 @@ public void ProcessWithProxyFailsToCreateDueMissingTopicRegistration()
var mockProxyClient = new MockCloudEventClient();
ProcessBuilder process = new(nameof(ProcessWithProxyFailsToCreateDueMissingTopicRegistration));

var counterStep = process.AddStepFromType<CommonSteps.CountStep>();
var counterStep = process.AddStepFromType<CommonSteps.CountStep>(name: nameof(ProcessWithProxyFailsToCreateDueMissingTopicRegistration));
var proxyStep = process.AddProxyStep([this._topic1]);

process.OnInputEvent(this._startProcessEvent).SendEventTo(new(counterStep));
Expand All @@ -92,9 +92,8 @@ public void ProcessWithProxyFailsToCreateDueMissingTopicRegistration()
public async Task ProcessWithCyclesAndProxyWithTwoTopicsAsync()
{
// Arrange
CommonSteps.CountStep.Index = 0;
var mockProxyClient = new MockCloudEventClient();
ProcessBuilder process = this.GetSampleProcessWithProxyEmittingTwoTopics(nameof(ProcessWithCyclesAndProxyWithTwoTopicsAsync));
ProcessBuilder process = this.GetSampleProcessWithProxyEmittingTwoTopics(nameof(ProcessWithCyclesAndProxyWithTwoTopicsAsync), counterName: nameof(ProcessWithCyclesAndProxyWithTwoTopicsAsync));
KernelProcess processInstance = process.Build();
Kernel kernel = new();

Expand Down Expand Up @@ -129,10 +128,10 @@ public async Task ProcessWithCyclesAndProxyWithTwoTopicsAsync()
public async Task ProcessWithProxyIn2LevelsNestedProcessEmitsTwoTopicsAsync()
{
// Arrange
CommonSteps.CountStep.Index = 0;
var mockProxyClient = new MockCloudEventClient();
ProcessBuilder process = new(nameof(ProcessWithProxyIn2LevelsNestedProcessEmitsTwoTopicsAsync));
var innerProcess = process.AddStepFromProcess(this.GetSampleProcessWithProxyEmittingTwoTopics($"Inner-{nameof(ProcessWithProxyIn2LevelsNestedProcessEmitsTwoTopicsAsync)}"));
var innerProcess = process.AddStepFromProcess(this.GetSampleProcessWithProxyEmittingTwoTopics(
$"Inner-{nameof(ProcessWithProxyIn2LevelsNestedProcessEmitsTwoTopicsAsync)}", counterName: nameof(ProcessWithProxyIn2LevelsNestedProcessEmitsTwoTopicsAsync)));

process
.OnInputEvent(this._startProcessEvent)
Expand Down Expand Up @@ -172,14 +171,16 @@ public async Task ProcessWithProxyIn2LevelsNestedProcessEmitsTwoTopicsAsync()
public async Task ProcessWithProxyIn4LevelsNestedProcessEmitsTwoTopicsAsync()
{
// Arrange
CommonSteps.CountStep.Index = 0;
var mockProxyClient = new MockCloudEventClient();
ProcessBuilder process = new(nameof(ProcessWithProxyIn4LevelsNestedProcessEmitsTwoTopicsAsync));
var innerProcess = process.AddStepFromProcess(
this.GetNestedProcess(
processName: $"Inner1-{nameof(ProcessWithProxyIn4LevelsNestedProcessEmitsTwoTopicsAsync)}",
internalProcess: this.GetSampleProcessWithProxyEmittingTwoTopics($"Inner2-{nameof(ProcessWithProxyIn4LevelsNestedProcessEmitsTwoTopicsAsync)}"),
inputEventName: this._startProcessEvent));
internalProcess: this.GetSampleProcessWithProxyEmittingTwoTopics(
$"Inner2-{nameof(ProcessWithProxyIn4LevelsNestedProcessEmitsTwoTopicsAsync)}",
$"Inner2_{nameof(ProcessWithProxyIn4LevelsNestedProcessEmitsTwoTopicsAsync)}"),
inputEventName: this._startProcessEvent,
counterName: $"Inner1_{nameof(ProcessWithProxyIn4LevelsNestedProcessEmitsTwoTopicsAsync)}"));

process
.OnInputEvent(this._startProcessEvent)
Expand Down Expand Up @@ -211,10 +212,10 @@ public async Task ProcessWithProxyIn4LevelsNestedProcessEmitsTwoTopicsAsync()
Assert.Equal(1, mockProxyClient.UninitializationCounter);
}

private ProcessBuilder GetNestedProcess(string processName, ProcessBuilder internalProcess, string inputEventName)
private ProcessBuilder GetNestedProcess(string processName, ProcessBuilder internalProcess, string inputEventName, string counterName)
{
ProcessBuilder process = new(processName);
var innerProcess = process.AddStepFromProcess(this.GetSampleProcessWithProxyEmittingTwoTopics($"Inner-{processName}"));
var innerProcess = process.AddStepFromProcess(this.GetSampleProcessWithProxyEmittingTwoTopics($"Inner-{processName}", counterName));

process
.OnInputEvent(inputEventName)
Expand All @@ -223,11 +224,11 @@ private ProcessBuilder GetNestedProcess(string processName, ProcessBuilder inter
return process;
}

private ProcessBuilder GetSampleProcessWithProxyEmittingTwoTopics(string processName)
private ProcessBuilder GetSampleProcessWithProxyEmittingTwoTopics(string processName, string counterName)
{
ProcessBuilder process = new(processName);

var counterStep = process.AddStepFromType<CommonSteps.CountStep>();
var counterStep = process.AddStepFromType<CommonSteps.CountStep>(name: counterName);
var evenNumberStep = process.AddStepFromType<CommonSteps.EvenNumberDetectorStep>();
var proxyStep = process.AddProxyStep([this._topic1, this._topic2]);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Threading;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;

Expand All @@ -20,15 +20,39 @@ public sealed class CountStep : KernelProcessStep
{
public const string CountFunction = nameof(Count);

#pragma warning disable CA2211
// workaround for unit testing evaluation purposes
public static int Index = 0;
#pragma warning restore CA2211
private string _name = null!;
private static readonly Dictionary<string, long> s_counters = new();

public override ValueTask ActivateAsync(KernelProcessStepState state)
{
this._name = state.Name;
return default;
}

[KernelFunction]
public string Count()
{
Interlocked.Increment(ref Index);
return Index.ToString();
lock (s_counters)
{
if (s_counters.TryGetValue(this._name, out var counter))
{
s_counters[this._name] = counter + 1;
}
else
{
s_counters[this._name] = 1;
}

return s_counters[this._name].ToString();
}
}

public static long GetCount(string name)
{
lock (s_counters)
{
return s_counters[name];
}
}
}

Expand Down
Loading