public async Task TestMultipleCloudEventsWithTracingMultiDispatchError() { // individual elements var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance); using var host = TestHelpers.NewHost <MyProg3>(ext); await host.StartAsync(); // add listener using var testListener = new ClientDiagnosticListener("Azure.Messaging.EventGrid"); const string functionName = "EventGridThrowsExceptionMultiple"; const string traceparent1 = "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01"; const string traceparent2 = "00-1123456789abcdef0123456789abcdef-1123456789abcdef-01"; var request = CreateDispatchRequest(functionName, JObject.Parse($"{{'subject':'one','data':{{'prop':'alpha'}},'traceparent':'{traceparent1}'}}"), JObject.Parse($"{{'subject':'two','data':{{'prop':'alpha'}},'traceparent':'{traceparent2}'}}")); var response = await ext.ConvertAsync(request, CancellationToken.None); Assert.AreEqual(1, testListener.Scopes.Count); var executionScope = testListener.AssertScope("EventGrid.Process", new KeyValuePair <string, string>("az.namespace", "Microsoft.EventGrid")); var expectedLinks = new[] { new ClientDiagnosticListener.ProducedLink(traceparent1, null), new ClientDiagnosticListener.ProducedLink(traceparent2, null) }; Assert.That(executionScope.Links, Is.EquivalentTo(expectedLinks)); Assert.NotNull(executionScope.Exception); Assert.True(_log.TryGetValue(executionScope.Activity.Id, out var activityName)); Assert.AreEqual("EventGrid.Process", activityName); }
public async Task TestEventGridEventWithTracingSingleDispatch(string functionName) { // individual elements var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance); using var host = TestHelpers.NewHost <MyProg1>(ext); await host.StartAsync(); // add listener using var testListener = new ClientDiagnosticListener("Azure.Messaging.EventGrid"); var request = CreateDispatchRequest(functionName, JObject.Parse(@"{'subject':'one','eventType':'1','id':'1','dataVersion':'0','data':{'prop':'alpha'}}"), JObject.Parse(@"{'subject':'one','eventType':'2','id':'1','dataVersion':'0','data':{'prop':'alpha'}}")); var response = await ext.ConvertAsync(request, CancellationToken.None); Assert.AreEqual(2, testListener.Scopes.Count); // one of executions will fail because of dup subject Assert.AreEqual(1, testListener.Scopes.Count(s => s.Exception != null)); for (int i = 0; i < 2; i++) { var executionScope = testListener.AssertAndRemoveScope("EventGrid.Process", new KeyValuePair <string, string>("az.namespace", "Microsoft.EventGrid")); Assert.IsEmpty(executionScope.Links); Assert.True(_log.TryGetValue(executionScope.Activity.Id, out var activityName)); Assert.AreEqual("EventGrid.Process", activityName); } }
public async Task TestUnsubscribe() { var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance); var host = TestHelpers.NewHost <MyProg1>(ext); await host.StartAsync(); // add listener var request = CreateUnsubscribeRequest("TestEventGrid"); var response = await ext.ConvertAsync(request, CancellationToken.None); Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode); }
public async Task TestMultipleCloudEventsWithTracingSingleDispatch() { // individual elements var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance); using var host = TestHelpers.NewHost <MyProg1>(ext); await host.StartAsync(); // add listener using var testListener = new ClientDiagnosticListener("Azure.Messaging.EventGrid"); const string functionName = "TestCloudEvent"; const string traceparent1 = "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01"; const string tracestate1 = "foo1=bar1"; const string traceparent2 = "00-1123456789abcdef0123456789abcdef-1123456789abcdef-01"; var request = CreateDispatchRequest(functionName, JObject.Parse($"{{{CloudEventRequiredFields},'data':{{'prop':'1'}},'traceparent':'{traceparent1}','tracestate':'{tracestate1}'}}"), JObject.Parse($"{{{CloudEventRequiredFields},'data':{{'prop':'2'}},'traceparent':'{traceparent2}'}}")); var response = await ext.ConvertAsync(request, CancellationToken.None); Assert.AreEqual(2, testListener.Scopes.Count); // one of executions will fail because of dup subject Assert.AreEqual(1, testListener.Scopes.Count(s => s.Exception != null)); bool fullFound = false, parentOnlyFound = false; for (int i = 0; i < 2; i++) { var executionScope = testListener.AssertAndRemoveScope("EventGrid.Process", new KeyValuePair <string, string>("az.namespace", "Microsoft.EventGrid")); Assert.AreEqual(1, executionScope.Links.Count); Assert.True(_log.TryGetValue(executionScope.Activity.Id, out var activityName)); Assert.AreEqual("EventGrid.Process", activityName); var link = executionScope.Links.Single(); if (link.Traceparent == traceparent1) { Assert.AreEqual(tracestate1, link.Tracestate); Assert.IsFalse(fullFound); fullFound = true; } else { Assert.IsNull(link.Tracestate); Assert.IsFalse(parentOnlyFound); parentOnlyFound = true; } } Assert.IsTrue(fullFound); Assert.IsTrue(parentOnlyFound); }
public async Task TestSubscribeOptions(string functionName) { var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance); using var host = TestHelpers.NewHost <MyProg1>(ext); await host.StartAsync(); // add listener var request = new HttpRequestMessage(HttpMethod.Options, $"http://localhost/?functionName={functionName}"); var response = await ext.ConvertAsync(request, CancellationToken.None); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.AreEqual("eventgrid.azure.net", response.Headers.GetValues("Webhook-Allowed-Origin").First()); }
public async Task TestSubscribe(string evt) { var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance); var host = TestHelpers.NewHost <MyProg1>(ext); await host.StartAsync(); // add listener var request = CreateEventSubscribeRequest("TestEventGrid", evt); var response = await ext.ConvertAsync(request, CancellationToken.None); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); var content = await response.Content.ReadAsStringAsync(); Assert.AreEqual("validation-code", JObject.Parse(content)["validationResponse"].ToString()); }
public async Task ExecutionFailureMultipleEventsTest() { var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance); var host = TestHelpers.NewHost <MyProg3>(ext); await host.StartAsync(); // add listener JObject dummyPayload = JObject.Parse("{}"); var request = CreateDispatchRequest("EventGridThrowsExceptionMultiple", dummyPayload); var response = await ext.ConvertAsync(request, CancellationToken.None); string responseContent = await response.Content.ReadAsStringAsync(); Assert.AreEqual("Exception while executing function: EventGridThrowsExceptionMultiple", responseContent); Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode); }
public async Task WrongFunctionNameTest() { var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance); var host = TestHelpers.NewHost <MyProg2>(ext); await host.StartAsync(); // add listener JObject dummyPayload = JObject.Parse("{}"); var request = CreateDispatchRequest("RandomFunctionName", dummyPayload); var response = await ext.ConvertAsync(request, CancellationToken.None); string responseContent = await response.Content.ReadAsStringAsync(); Assert.AreEqual("cannot find function: 'RandomFunctionName'", responseContent); Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); }
public async Task TestCloudEvent() { // individual elements var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance); var host = TestHelpers.NewHost <MyProg1>(ext); await host.StartAsync(); // add listener var request = CreateSingleRequest("TestEventGrid", JObject.Parse(@"{'subject':'one','data':{'prop':'alpha'}}")); var response = await ext.ConvertAsync(request, CancellationToken.None); // verifies each instance gets its own proper binding data (from FakePayload.Prop) _log.TryGetValue("one", out string alpha); Assert.AreEqual("alpha", alpha); Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode); }
public async Task TestDispatch() { var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance); var host = TestHelpers.NewHost <MyProg1>(ext); await host.StartAsync(); // add listener var request = CreateDispatchRequest("TestEventGrid", JObject.Parse(@"{'subject':'one','data':{'prop':'alpha'}}"), JObject.Parse(@"{'subject':'two','data':{'prop':'beta'}}")); var response = await ext.ConvertAsync(request, CancellationToken.None); // Verify that the user function was dispatched twice, NOT necessarily in order // Also verifies each instance gets its own proper binding data (from FakePayload.Prop) _log.TryGetValue("one", out string alpha); _log.TryGetValue("two", out string beta); Assert.AreEqual("alpha", alpha); Assert.AreEqual("beta", beta); // TODO - Verify that we return from webhook before the dispatch is finished // https://github.com/Azure/azure-functions-eventgrid-extension/issues/10 Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode); }