Skip to content

Commit

Permalink
Adding test to validate worker invocation metrics (#8882)
Browse files Browse the repository at this point in the history
* Adding test to validate invocation metrics
  • Loading branch information
surgupta-msft authored Nov 7, 2022
1 parent e57dc4c commit d3fdaad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
21 changes: 20 additions & 1 deletion test/WebJobs.Script.Tests/Workers/Rpc/GrpcWorkerChannelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,10 @@ public async Task SendInvocationRequest_PublishesOutboundEvent()
ScriptInvocationContext scriptInvocationContext = GetTestScriptInvocationContext(Guid.NewGuid(), null);
await _workerChannel.SendInvocationRequest(scriptInvocationContext);
await Task.Delay(500);
string testWorkerId = _workerId.ToLowerInvariant();
var traces = _logger.GetLogMessages();
Assert.True(traces.Any(m => string.Equals(m.FormattedMessage, _expectedLogMsg)));
Assert.False(string.IsNullOrEmpty(_metricsLogger.LoggedEvents.FirstOrDefault(e => e.Contains("testworkeridworker.invoke_js1"))));
Assert.Equal(1, _metricsLogger.LoggedEvents.Count(e => e.Contains($"{string.Format(MetricEventNames.WorkerInvoked, testWorkerId)}_{scriptInvocationContext.FunctionMetadata.Name}")));
}

[Fact]
Expand Down Expand Up @@ -688,6 +689,24 @@ public void SendFunctionEnvironmentReloadRequest_WithDirectory()
Assert.True(envReloadRequest.FunctionAppDirectory == _scriptRootPath);
}

[Fact]
public async Task SendInvocationRequest_PublishesOutboundEvent_ReceivesInvocationResponse()
{
await CreateDefaultWorkerChannel();
_metricsLogger.ClearCollections();
var invocationid = Guid.NewGuid();
ScriptInvocationContext scriptInvocationContext = GetTestScriptInvocationContext(invocationid, new TaskCompletionSource<ScriptInvocationResult>());
await _workerChannel.SendInvocationRequest(scriptInvocationContext);
_testFunctionRpcService.PublishInvocationResponseEvent(invocationid.ToString());
await Task.Delay(500);
var testWorkerId = _workerId.ToLowerInvariant();
var traces = _logger.GetLogMessages();
Assert.True(traces.Any(m => string.Equals(m.FormattedMessage, $"InvocationResponse received for invocation id: '{invocationid}'")));
Assert.Equal(1, _metricsLogger.LoggedEvents.Count(e => e.Contains($"{string.Format(MetricEventNames.WorkerInvoked, testWorkerId)}_{scriptInvocationContext.FunctionMetadata.Name}")));
Assert.Equal(1, _metricsLogger.LoggedEvents.Count(e => e.Contains(string.Format(MetricEventNames.WorkerInvokeSucceeded, testWorkerId))));
Assert.Equal(0, _metricsLogger.LoggedEvents.Count(e => e.Contains(string.Format(MetricEventNames.WorkerInvokeFailed, testWorkerId))));
}

[Fact]
public async Task ReceivesInboundEvent_InvocationResponse()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,15 @@ public static FunctionEnvironmentReloadResponse GetTestFunctionEnvReloadResponse
return relaodEnvResponse;
}

public void PublishInvocationResponseEvent()
public void PublishInvocationResponseEvent(string invocationId = null)
{
StatusResult statusResult = new StatusResult()
{
Status = StatusResult.Types.Status.Success
};
InvocationResponse invocationResponse = new InvocationResponse()
{
InvocationId = "TestInvocationId",
InvocationId = invocationId == null ? "TestInvocationId" : invocationId,
Result = statusResult
};
StreamingMessage responseMessage = new StreamingMessage()
Expand Down

0 comments on commit d3fdaad

Please sign in to comment.