Skip to content

Commit 8e8ea26

Browse files
committed
Fix event grid publishing empty content (#6)
1 parent c2bbd0e commit 8e8ea26

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/AzureFunctionExtensions/EventGrid/EventGridOutputAsyncCollector.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public EventGridOutputAsyncCollector(EventGridOutputConfiguration config, EventG
5656

5757
public async Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken))
5858
{
59+
// Do nothing if no events were created
60+
if (this.eventGridEvents == null || this.eventGridEvents.Count == 0)
61+
return;
62+
5963
var sasKey = Utils.MergeValueForProperty(attr.SasKey, config.SasKey);
6064
if (string.IsNullOrEmpty(sasKey)) throw new ArgumentException("Sas Key is missing", nameof(sasKey));
6165

test/AzureFunctionExtensions.Test/EventGridOutputAsyncCollectorTest.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,44 @@ public async Task SendsMessage_If_All_IsCorrect()
200200
Assert.Equal("true", requestJson[0]["data"]["test"].ToString().ToLowerInvariant());
201201

202202
}
203+
204+
205+
[Fact]
206+
public async Task Do_Nothing_If_No_Event_Was_Created()
207+
{
208+
var config = new EventGridOutputConfiguration()
209+
{
210+
SasKey = "1234594949=",
211+
TopicEndpoint = "https://xxxx.westeurope-1.eventgrid.azure.net/api/events",
212+
EventType = "My.Test",
213+
Subject = "Test/1"
214+
};
215+
216+
var attr = new EventGridOutputAttribute()
217+
{
218+
};
219+
220+
HttpRequestMessage message = null;
221+
var httpMessageHandler = new HttpMessageHandlerMock()
222+
.SetupHandler(req =>
223+
{
224+
message = req;
225+
return new HttpResponseMessage(HttpStatusCode.OK);
226+
});
227+
228+
var httpClient = new HttpClient(httpMessageHandler);
229+
230+
var httpClientFactory = new Mock<IHttpClientFactory>();
231+
httpClientFactory.Setup(x => x.Create()).Returns(httpClient);
232+
233+
var target = new EventGridOutputAsyncCollector(config, attr, httpClientFactory.Object);
234+
235+
await target.FlushAsync();
236+
237+
238+
//httpClientFactory.VerifyAll();
239+
240+
Assert.Null(message);
241+
}
203242
}
204243
}

0 commit comments

Comments
 (0)