Skip to content

Commit ec6e02f

Browse files
committed
CSHARP-1994: Added tests.
1 parent 2497f98 commit ec6e02f

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

src/MongoDB.Driver.Core/Core/Connections/KeepAliveValues.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ internal struct KeepAliveValues
2525

2626
public byte[] ToBytes()
2727
{
28+
// set the tcp_keepalive struct at the following page for documentation of the buffer layout
29+
/// https://msdn.microsoft.com/en-us/library/windows/desktop/dd877220(v=vs.85).aspx
2830
var bytes = new byte[24];
2931
Array.Copy(BitConverter.GetBytes(OnOff), 0, bytes, 0, 8);
3032
Array.Copy(BitConverter.GetBytes(KeepAliveTime), 0, bytes, 8, 8);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* Copyright 2018-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Text;
20+
using System.Threading.Tasks;
21+
using FluentAssertions;
22+
using Xunit;
23+
24+
namespace MongoDB.Driver.Core.Connections
25+
{
26+
public class KeepAliveValuesTests
27+
{
28+
[Theory]
29+
[InlineData(0)]
30+
[InlineData(1)]
31+
[InlineData(ulong.MaxValue)]
32+
public void OnOff_get_and_set_work(ulong value)
33+
{
34+
var subject = new KeepAliveValues();
35+
36+
subject.OnOff = value;
37+
var result = subject.OnOff;
38+
39+
result.Should().Be(value);
40+
}
41+
42+
[Theory]
43+
[InlineData(0)]
44+
[InlineData(1)]
45+
[InlineData(ulong.MaxValue)]
46+
public void KeepAliveTime_get_and_set_work(ulong value)
47+
{
48+
var subject = new KeepAliveValues();
49+
50+
subject.KeepAliveTime = value;
51+
var result = subject.KeepAliveTime;
52+
53+
result.Should().Be(value);
54+
}
55+
56+
[Theory]
57+
[InlineData(0)]
58+
[InlineData(1)]
59+
[InlineData(ulong.MaxValue)]
60+
public void KeepAliveInterval_get_and_set_work(ulong value)
61+
{
62+
var subject = new KeepAliveValues();
63+
64+
subject.KeepAliveInterval = value;
65+
var result = subject.KeepAliveInterval;
66+
67+
result.Should().Be(value);
68+
}
69+
70+
[Theory]
71+
[InlineData(0x0102030405060708, 0x0203040506070809, 0x030405060708090a, new byte[] { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03 })]
72+
[InlineData(0xf1f2f3f4f5f6f7f8, 0xf2f3f4f5f6f7f8f9, 0xf3f4f5f6f7f8f9fa, new byte[] { 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3 })]
73+
public void ToBytes_should_return_expected_result(ulong onOff, ulong keepAliveTime, ulong keepAliveInterval, byte[] expectedResult)
74+
{
75+
var subject = new KeepAliveValues
76+
{
77+
OnOff = onOff,
78+
KeepAliveTime = keepAliveTime,
79+
KeepAliveInterval = keepAliveInterval
80+
};
81+
82+
var result = subject.ToBytes();
83+
84+
result.Should().Equal(expectedResult);
85+
}
86+
}
87+
}

tests/MongoDB.Driver.Core.Tests/MongoDB.Driver.Core.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<Compile Include="Core\Connections\BinaryConnection_CommandEventTests.cs" />
9999
<Compile Include="Core\Connections\ClientDocumentHelperTests.cs" />
100100
<Compile Include="Core\Connections\IsMasterHelperTests.cs" />
101+
<Compile Include="Core\Connections\KeepAliveValuesTests.cs" />
101102
<Compile Include="Core\Events\EventAggregatorTests.cs" />
102103
<Compile Include="Core\Events\ReflectionEventSubscriberTests.cs" />
103104
<Compile Include="Core\Events\TraceSourceSdamEventSubscriberTests.cs" />

0 commit comments

Comments
 (0)