From d3fdaadc6514fa25c7a5545c83648e20b092d787 Mon Sep 17 00:00:00 2001 From: Surgupta Date: Mon, 7 Nov 2022 11:20:36 -0800 Subject: [PATCH] Adding test to validate worker invocation metrics (#8882) * Adding test to validate invocation metrics --- .../Workers/Rpc/GrpcWorkerChannelTests.cs | 21 ++++++++++++++++++- .../Workers/Rpc/TestFunctionRpcService.cs | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/test/WebJobs.Script.Tests/Workers/Rpc/GrpcWorkerChannelTests.cs b/test/WebJobs.Script.Tests/Workers/Rpc/GrpcWorkerChannelTests.cs index eba505cb26..9ad6f5683f 100644 --- a/test/WebJobs.Script.Tests/Workers/Rpc/GrpcWorkerChannelTests.cs +++ b/test/WebJobs.Script.Tests/Workers/Rpc/GrpcWorkerChannelTests.cs @@ -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] @@ -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()); + 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() { diff --git a/test/WebJobs.Script.Tests/Workers/Rpc/TestFunctionRpcService.cs b/test/WebJobs.Script.Tests/Workers/Rpc/TestFunctionRpcService.cs index 080e7d7b8d..0817f3f143 100644 --- a/test/WebJobs.Script.Tests/Workers/Rpc/TestFunctionRpcService.cs +++ b/test/WebJobs.Script.Tests/Workers/Rpc/TestFunctionRpcService.cs @@ -262,7 +262,7 @@ public static FunctionEnvironmentReloadResponse GetTestFunctionEnvReloadResponse return relaodEnvResponse; } - public void PublishInvocationResponseEvent() + public void PublishInvocationResponseEvent(string invocationId = null) { StatusResult statusResult = new StatusResult() { @@ -270,7 +270,7 @@ public void PublishInvocationResponseEvent() }; InvocationResponse invocationResponse = new InvocationResponse() { - InvocationId = "TestInvocationId", + InvocationId = invocationId == null ? "TestInvocationId" : invocationId, Result = statusResult }; StreamingMessage responseMessage = new StreamingMessage()