PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/Bindings/ServerChannelSourceTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 142 lines | 103 code | 25 blank | 14 comment | 2 complexity | 60f032ef86f4fd0f5e8e5f034c788a7e MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-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. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using FluentAssertions;
  22. using MongoDB.Driver.Core.Bindings;
  23. using MongoDB.Driver.Core.Clusters;
  24. using MongoDB.Driver.Core.Servers;
  25. using MongoDB.Driver.Core.Helpers;
  26. using Moq;
  27. using Xunit;
  28. using MongoDB.Bson.TestHelpers.XunitExtensions;
  29. namespace MongoDB.Driver.Core.Bindings
  30. {
  31. public class ServerChannelSourceTests
  32. {
  33. private Mock<IServer> _mockServer;
  34. public ServerChannelSourceTests()
  35. {
  36. _mockServer = new Mock<IServer>();
  37. }
  38. [Fact]
  39. public void Constructor_should_throw_when_server_is_null()
  40. {
  41. var session = new Mock<ICoreSessionHandle>().Object;
  42. Action act = () => new ServerChannelSource(null, session);
  43. act.ShouldThrow<ArgumentNullException>();
  44. }
  45. [Fact]
  46. public void Constructor_should_throw_when_session_is_null()
  47. {
  48. Action act = () => new ServerChannelSource(_mockServer.Object, null);
  49. act.ShouldThrow<ArgumentNullException>();
  50. }
  51. [Fact]
  52. public void ServerDescription_should_return_description_of_server()
  53. {
  54. var session = new Mock<ICoreSessionHandle>().Object;
  55. var subject = new ServerChannelSource(_mockServer.Object, session);
  56. var desc = ServerDescriptionHelper.Disconnected(new ClusterId());
  57. _mockServer.SetupGet(s => s.Description).Returns(desc);
  58. var result = subject.ServerDescription;
  59. result.Should().BeSameAs(desc);
  60. }
  61. [Fact]
  62. public void Session_should_return_expected_result()
  63. {
  64. var session = new Mock<ICoreSessionHandle>().Object;
  65. var subject = new ServerChannelSource(_mockServer.Object, session);
  66. var result = subject.Session;
  67. result.Should().BeSameAs(session);
  68. }
  69. [Theory]
  70. [ParameterAttributeData]
  71. public void GetChannel_should_throw_if_disposed(
  72. [Values(false, true)]
  73. bool async)
  74. {
  75. var session = new Mock<ICoreSessionHandle>().Object;
  76. var subject = new ServerChannelSource(_mockServer.Object, session);
  77. subject.Dispose();
  78. Action act;
  79. if (async)
  80. {
  81. act = () => subject.GetChannelAsync(CancellationToken.None).GetAwaiter().GetResult();
  82. }
  83. else
  84. {
  85. act = () => subject.GetChannel(CancellationToken.None);
  86. }
  87. act.ShouldThrow<ObjectDisposedException>();
  88. }
  89. [Theory]
  90. [ParameterAttributeData]
  91. public void GetChannel_should_get_connection_from_server(
  92. [Values(false, true)]
  93. bool async)
  94. {
  95. var session = new Mock<ICoreSessionHandle>().Object;
  96. var subject = new ServerChannelSource(_mockServer.Object, session);
  97. if (async)
  98. {
  99. subject.GetChannelAsync(CancellationToken.None).GetAwaiter().GetResult();
  100. _mockServer.Verify(s => s.GetChannelAsync(CancellationToken.None), Times.Once);
  101. }
  102. else
  103. {
  104. subject.GetChannel(CancellationToken.None);
  105. _mockServer.Verify(s => s.GetChannel(CancellationToken.None), Times.Once);
  106. }
  107. }
  108. [Fact]
  109. public void Dispose_should_dispose_session()
  110. {
  111. var mockSession = new Mock<ICoreSessionHandle>();
  112. var subject = new ServerChannelSource(_mockServer.Object, mockSession.Object);
  113. subject.Dispose();
  114. mockSession.Verify(m => m.Dispose(), Times.Once);
  115. }
  116. }
  117. }