Skip to content

Commit

Permalink
Add .Net Standard 2.0. Add test and Travis CI.
Browse files Browse the repository at this point in the history
  • Loading branch information
stop-cran committed Dec 18, 2018
1 parent 62d79e7 commit 6848be4
Show file tree
Hide file tree
Showing 11 changed files with 386 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: csharp
mono: none
dotnet: 2.0.0
solution: ZabbixSender.Async.sln
script:
- dotnet test ZabbixSender.Async.Tests
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Overview
# Overview [![NuGet](https://img.shields.io/nuget/v/ZabbixSender.Async.svg)](https://www.nuget.org/packages/ZabbixSender.Async) [![Build Status](https://travis-ci.com/stop-cran/ZabbixSender.Async.svg?branch=master)](https://travis-ci.com/stop-cran/ZabbixSender.Async)

The package provides a tool to send data to Zabbix in the same way as [zabbix_sender](https://www.zabbix.com/documentation/4.0/ru/manual/concepts/sender) tool. It implements [Zabbix Sender Protocol 4.0](https://www.zabbix.org/wiki/Docs/protocols/zabbix_sender/4.0).

# Installation [![NuGet](https://img.shields.io/nuget/v/ZabbixSender.Async.svg)](https://www.nuget.org/packages/ZabbixSender.Async)
# Installation

NuGet package is available [here](https://www.nuget.org/packages/ZabbixSender.Async/).

Expand Down
132 changes: 132 additions & 0 deletions ZabbixSender.Async.Tests/FormatterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using Shouldly;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZabbixSender.Async.Tests
{
[TestFixture]
public class FormatterTests
{
private static readonly byte[] ZabbixHeader = new byte[] { 90, 66, 88, 68, 1 };

[Test]
public void RequestShouldStartWithZabbixHeader()
{
using (var stream = new MemoryStream())
{
new Formatter().WriteRequest(stream, new[] { new SendData() });
Encoding.ASCII.GetString(stream.ToArray()).ShouldStartWith("ZBXD");
}
}

[Test]
public async Task RequestShouldStartWithZabbixHeaderAsync()
{
using (var stream = new MemoryStream())
{
await new Formatter().WriteRequestAsync(stream, new[] { new SendData() });
Encoding.ASCII.GetString(stream.ToArray()).ShouldStartWith("ZBXD");
}
}

[Test]
public void RequestShouldEndWithJson()
{
using (var stream = new MemoryStream())
{
new Formatter().WriteRequest(stream, new[]
{
new SendData
{
Host = "host1",
Key = "key1"
},
new SendData
{
Host = "host2",
Key = "key2",
Value = "value2",
Clock = new System.DateTime(2018, 12, 18)
},
});

var result = Encoding.ASCII.GetString(stream.ToArray());

var json = JToken.Parse(result.Substring(result.IndexOf('{')));

json.ShouldNotBeNull();
json.ShouldBeOfType<JObject>();
}
}

[Test]
public async Task RequestShouldEndWithJsonAsync()
{
using (var stream = new MemoryStream())
{
await new Formatter().WriteRequestAsync(stream, new[]
{
new SendData
{
Host = "host1",
Key = "key1"
},
new SendData
{
Host = "host2",
Key = "key2",
Value = "value2",
Clock = new System.DateTime(2018, 12, 18)
},
});

var result = Encoding.ASCII.GetString(stream.ToArray());

var json = JToken.Parse(result.Substring(result.IndexOf('{')));

json.ShouldNotBeNull();
json.ShouldBeOfType<JObject>();
}
}

[Test]
public void ShouldCheckResponseHeader()
{
using (var stream = new MemoryStream())
Should.Throw<ProtocolException>(() => new Formatter().ReadResponse(stream));
}

[Test]
public void ShouldCheckResponseHeaderAsyncc()
{
using (var stream = new MemoryStream())
Should.Throw<ProtocolException>(async () => await new Formatter().ReadResponseAsync(stream));
}

[Test]
public void ShouldRethrowJsonError()
{
using (var stream = new MemoryStream(
ZabbixHeader
.Concat(new byte[8])
.Concat(Encoding.ASCII.GetBytes("[ \"invalid\" ]"))
.ToArray()))
Should.Throw<ProtocolException>(() => new Formatter().ReadResponse(stream));
}

[Test]
public void ShouldRethrowJsonErrorAsync()
{
using (var stream = new MemoryStream(
ZabbixHeader
.Concat(new byte[8])
.Concat(Encoding.ASCII.GetBytes("[ \"invalid\" ]"))
.ToArray()))
Should.Throw<ProtocolException>(async () => await new Formatter().ReadResponseAsync(stream));
}
}
}
20 changes: 20 additions & 0 deletions ZabbixSender.Async.Tests/ZabbixSender.Async.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.2" />
<PackageReference Include="Shouldly" Version="3.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ZabbixSender.Async\ZabbixSender.Async.csproj" />
</ItemGroup>

</Project>
8 changes: 7 additions & 1 deletion ZabbixSender.Async.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.136
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZabbixSender.Async", "ZabbixSender.Async\ZabbixSender.Async.csproj", "{4D75CEC8-46DC-4959-8F60-94AF4565B7A5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZabbixSender.Async", "ZabbixSender.Async\ZabbixSender.Async.csproj", "{4D75CEC8-46DC-4959-8F60-94AF4565B7A5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZabbixSender.Async.Tests", "ZabbixSender.Async.Tests\ZabbixSender.Async.Tests.csproj", "{1AA6B8F1-BDA0-4249-8616-356681ACA298}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{4D75CEC8-46DC-4959-8F60-94AF4565B7A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D75CEC8-46DC-4959-8F60-94AF4565B7A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D75CEC8-46DC-4959-8F60-94AF4565B7A5}.Release|Any CPU.Build.0 = Release|Any CPU
{1AA6B8F1-BDA0-4249-8616-356681ACA298}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1AA6B8F1-BDA0-4249-8616-356681ACA298}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AA6B8F1-BDA0-4249-8616-356681ACA298}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1AA6B8F1-BDA0-4249-8616-356681ACA298}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
10 changes: 5 additions & 5 deletions ZabbixSender.Async/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public Formatter(int bufferSize = 1024) :
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
Converters =
{
new UnixDateTimeConverter()
}
{
new UnixDateTimeConverter()
}
}, bufferSize)
{ }

Expand Down Expand Up @@ -76,7 +76,7 @@ public void WriteRequest(Stream stream, IEnumerable<SendData> data)
}

public async Task WriteRequestAsync(Stream stream, IEnumerable<SendData> data,
CancellationToken cancellationToken)
CancellationToken cancellationToken = default)
{
using (var ms = new MemoryStream())
{
Expand Down Expand Up @@ -124,7 +124,7 @@ public SenderResponse ReadResponse(Stream stream)
}
}

public async Task<SenderResponse> ReadResponseAsync(Stream stream, CancellationToken cancellationToken)
public async Task<SenderResponse> ReadResponseAsync(Stream stream, CancellationToken cancellationToken = default)
{
try
{
Expand Down
4 changes: 2 additions & 2 deletions ZabbixSender.Async/IFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace ZabbixSender.Async
public interface IFormatter
{
SenderResponse ReadResponse(Stream stream);
Task<SenderResponse> ReadResponseAsync(Stream stream, CancellationToken cancellationToken);
Task<SenderResponse> ReadResponseAsync(Stream stream, CancellationToken cancellationToken = default);
void WriteRequest(Stream stream, IEnumerable<SendData> data);
Task WriteRequestAsync(Stream stream, IEnumerable<SendData> data, CancellationToken cancellationToken);
Task WriteRequestAsync(Stream stream, IEnumerable<SendData> data, CancellationToken cancellationToken = default);
}
}
8 changes: 2 additions & 6 deletions ZabbixSender.Async/ISender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ namespace ZabbixSender.Async
{
public interface ISender
{
Task<SenderResponse> Send(IEnumerable<SendData> data);

Task<SenderResponse> Send(IEnumerable<SendData> data, CancellationToken cancellationToken);

Task<SenderResponse> Send(params SendData[] data);

Task<SenderResponse> Send(string host, string key, string value);
Task<SenderResponse> Send(IEnumerable<SendData> data, CancellationToken cancellationToken = default);

Task<SenderResponse> Send(string host, string key, string value, CancellationToken cancellationToken);
Task<SenderResponse> Send(string host, string key, string value, CancellationToken cancellationToken = default);
}
}
14 changes: 4 additions & 10 deletions ZabbixSender.Async/SenderSkeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public SenderSkeleton(
this.formatterFactory = formatterFactory;
}

public Task<SenderResponse> Send(string host, string key, string value) =>
Send(host, key, value, CancellationToken.None);
public Task<SenderResponse> Send(params SendData[] data) =>
Send(data, CancellationToken.None);

public Task<SenderResponse> Send(string host, string key, string value, CancellationToken cancellationToken) =>
public Task<SenderResponse> Send(string host, string key, string value, CancellationToken cancellationToken = default) =>
Send(new[]
{
new SendData
Expand All @@ -41,13 +41,7 @@ public Task<SenderResponse> Send(string host, string key, string value, Cancella
}
}, cancellationToken);

public Task<SenderResponse> Send(params SendData[] data) =>
Send(data, CancellationToken.None);

public Task<SenderResponse> Send(IEnumerable<SendData> data) =>
Send(data, CancellationToken.None);

public async Task<SenderResponse> Send(IEnumerable<SendData> data, CancellationToken cancellationToken)
public async Task<SenderResponse> Send(IEnumerable<SendData> data, CancellationToken cancellationToken = default)
{
using (var tcpClient = await tcpClientFactory(cancellationToken))
using (var networkStream = tcpClient.GetStream())
Expand Down
9 changes: 5 additions & 4 deletions ZabbixSender.Async/ZabbixSender.Async.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>
<Version>1.0.3</Version>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<Version>1.0.4</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Apache License 2.0 (stop-cran, 2018)</Copyright>
<Authors>stop-cran &lt;[email protected]&gt;</Authors>
<Company>stop-cran &lt;[email protected]&gt;</Company>
<RepositoryUrl>https://github.com/stop-cran/ZabbixSender.Async</RepositoryUrl>
<PackageReleaseNotes>Added documentation XML.</PackageReleaseNotes>
<Description>The package provides a tool to send data to Zabbix in the same way as zabbix_sender tool. It implements Zabbix Sender Protocol 4.0.</Description>
<PackageReleaseNotes>Added .Net Standard 2.0.</PackageReleaseNotes>
<Description>A tool to send data to Zabbix in the same way as zabbix_sender tool. It implements Zabbix Sender Protocol 4.0.</Description>
<DocumentationFile>ZabbixSender.Async.xml</DocumentationFile>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 6848be4

Please sign in to comment.