Skip to content

Commit

Permalink
fix: prevent durable sink from posting empty batches
Browse files Browse the repository at this point in the history
Prevent durable HTTP sink from posting HTTP messages without any log events.

Closes #32
  • Loading branch information
FantasticFiasco authored Oct 11, 2017
1 parent 20139ce commit 959d918
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ This project adheres to [Semantic Versioning](http://semver.org/) and is followi

## Unreleased

### Fixed

- [#32](https://github.com/FantasticFiasco/serilog-sinks-http/issues/32) Prevent durable HTTP sink from posting HTTP messages without any log events

## [4.2.0] - 2017-08-20

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ private async Task OnTick()
nextRequiredLevelCheckUtc = DateTime.UtcNow.Add(RequiredLevelCheckInterval);
}

if (string.IsNullOrEmpty(payload))
continue;;

var content = new StringContent(payload, Encoding.UTF8, ContentType);

var result = await client.PostAsync(requestUri, content).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Moq;
using Serilog.LogServer;
using Serilog.Sinks.Http.BatchFormatters;
using Serilog.Sinks.Http.TextFormatters;
Expand Down Expand Up @@ -137,5 +140,32 @@ public void InvalidBufferFileSizeLimitBytes(int? bufferFileSizeLimitBytes)
// Assert
provider.ShouldThrow<ArgumentOutOfRangeException>();
}

[Fact]
public async Task NoNetworkTrafficWithoutLogEvents()
{
// Arrange
var httpClient = new Mock<IHttpClient>();

// ReSharper disable once UnusedVariable
var httpSink = new DurableHttpSink(
"api/events",
"Buffer-{Date}.json",
null,
null,
1000,
TimeSpan.FromSeconds(2),
new NormalRenderedTextFormatter(),
new DefaultBatchFormatter(),
httpClient.Object);

// Act
await Task.Delay(TimeSpan.FromMinutes(3));

// Assert
httpClient.Verify(
mock => mock.PostAsync(It.IsAny<string>(), It.IsAny<HttpContent>()),
Times.Never);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Moq;
using Serilog.Sinks.Http.BatchFormatters;
using Serilog.Sinks.Http.TextFormatters;
using Xunit;

namespace Serilog.Sinks.Http.Private.Sinks
{
public class HttpSinkTest
{
[Fact]
public async Task NoNetworkTrafficWithoutLogEvents()
{
// Arrange
var httpClient = new Mock<IHttpClient>();

// ReSharper disable once UnusedVariable
var httpSink = new HttpSink(
"api/events",
1000,
TimeSpan.FromSeconds(2),
new NormalRenderedTextFormatter(),
new DefaultBatchFormatter(),
httpClient.Object);

// Act
await Task.Delay(TimeSpan.FromMinutes(3));

// Assert
httpClient.Verify(
mock => mock.PostAsync(It.IsAny<string>(), It.IsAny<HttpContent>()),
Times.Never);
}
}
}

0 comments on commit 959d918

Please sign in to comment.