PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/Configuration/ConnectionSettingsTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 243 lines | 185 code | 44 blank | 14 comment | 0 complexity | 5b8cb970f0d814ff9dabebb26dcaf803 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 FluentAssertions;
  17. using MongoDB.Bson.TestHelpers.XunitExtensions;
  18. using MongoDB.Driver.Core.Authentication;
  19. using MongoDB.Driver.Core.Compression;
  20. using Xunit;
  21. namespace MongoDB.Driver.Core.Configuration
  22. {
  23. public class ConnectionSettingsTests
  24. {
  25. private static readonly ConnectionSettings __defaults = new ConnectionSettings();
  26. [Fact]
  27. public void constructor_should_initialize_instance()
  28. {
  29. var subject = new ConnectionSettings();
  30. subject.ApplicationName.Should().BeNull();
  31. subject.Authenticators.Should().BeEmpty();
  32. subject.Compressors.Should().BeEmpty();
  33. subject.MaxIdleTime.Should().Be(TimeSpan.FromMinutes(10));
  34. subject.MaxLifeTime.Should().Be(TimeSpan.FromMinutes(30));
  35. }
  36. [Fact]
  37. public void constructor_should_throw_when_applicationName_is_too_long()
  38. {
  39. var applicationName = new string('x', 129);
  40. var exception = Record.Exception(() => new ConnectionSettings(applicationName: applicationName));
  41. var argumentException = exception.Should().BeOfType<ArgumentException>().Subject;
  42. argumentException.ParamName.Should().Be("applicationName");
  43. }
  44. [Fact]
  45. public void constructor_should_throw_when_authenticators_is_null()
  46. {
  47. Action action = () => new ConnectionSettings(authenticators: null);
  48. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("authenticators");
  49. }
  50. [Fact]
  51. public void constructor_should_throw_when_compressors_is_null()
  52. {
  53. var exception = Record.Exception(() => new ConnectionSettings(compressors: null));
  54. var e = exception.Should().BeOfType<ArgumentNullException>().Subject;
  55. e.ParamName.Should().Be("compressors");
  56. }
  57. [Theory]
  58. [ParameterAttributeData]
  59. public void constructor_should_throw_when_maxIdleTime_is_negative_or_zero(
  60. [Values(-1, 0)]
  61. int maxIdleTime)
  62. {
  63. Action action = () => new ConnectionSettings(maxIdleTime: TimeSpan.FromSeconds(maxIdleTime));
  64. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("maxIdleTime");
  65. }
  66. [Theory]
  67. [ParameterAttributeData]
  68. public void constructor_should_throw_when_maxLifeTime_is_negative_or_zero(
  69. [Values(-1, 0)]
  70. int maxLifeTime)
  71. {
  72. Action action = () => new ConnectionSettings(maxLifeTime: TimeSpan.FromSeconds(maxLifeTime));
  73. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("maxLifeTime");
  74. }
  75. [Fact]
  76. public void constructor_with_applicationName_should_initialize_instance()
  77. {
  78. var subject = new ConnectionSettings(applicationName: "app");
  79. subject.ApplicationName.Should().Be("app");
  80. subject.Authenticators.Should().Equal(__defaults.Authenticators);
  81. subject.Compressors.Should().Equal(__defaults.Compressors);
  82. subject.MaxIdleTime.Should().Be(__defaults.MaxIdleTime);
  83. subject.MaxLifeTime.Should().Be(__defaults.MaxLifeTime);
  84. }
  85. [Fact]
  86. public void constructor_with_authenticators_should_initialize_instance()
  87. {
  88. #pragma warning disable 618
  89. var authenticators = new[] { new MongoDBCRAuthenticator(new UsernamePasswordCredential("source", "username", "password")) };
  90. #pragma warning restore 618
  91. var subject = new ConnectionSettings(authenticators: authenticators);
  92. subject.ApplicationName.Should().BeNull();
  93. subject.Authenticators.Should().Equal(authenticators);
  94. subject.Compressors.Should().BeEquivalentTo(__defaults.Compressors);
  95. subject.MaxIdleTime.Should().Be(__defaults.MaxIdleTime);
  96. subject.MaxLifeTime.Should().Be(__defaults.MaxLifeTime);
  97. }
  98. [Fact]
  99. public void constructor_with_compressors_should_initialize_instance()
  100. {
  101. var compressors = new[] { new CompressorConfiguration(CompressorType.Zlib) };
  102. var subject = new ConnectionSettings(compressors: compressors);
  103. subject.ApplicationName.Should().BeNull();
  104. subject.Authenticators.Should().Equal(__defaults.Authenticators);
  105. subject.Compressors.Should().Equal(compressors);
  106. subject.MaxIdleTime.Should().Be(__defaults.MaxIdleTime);
  107. subject.MaxLifeTime.Should().Be(__defaults.MaxLifeTime);
  108. }
  109. [Fact]
  110. public void constructor_with_maxIdleTime_should_initialize_instance()
  111. {
  112. var maxIdleTime = TimeSpan.FromSeconds(123);
  113. var subject = new ConnectionSettings(maxIdleTime: maxIdleTime);
  114. subject.ApplicationName.Should().BeNull();
  115. subject.Authenticators.Should().Equal(__defaults.Authenticators);
  116. subject.Compressors.Should().Equal(__defaults.Compressors);
  117. subject.MaxIdleTime.Should().Be(maxIdleTime);
  118. subject.MaxLifeTime.Should().Be(__defaults.MaxLifeTime);
  119. }
  120. [Fact]
  121. public void constructor_with_maxLifeTime_should_initialize_instance()
  122. {
  123. var maxLifeTime = TimeSpan.FromSeconds(123);
  124. var subject = new ConnectionSettings(maxLifeTime: maxLifeTime);
  125. subject.ApplicationName.Should().BeNull();
  126. subject.Authenticators.Should().Equal(__defaults.Authenticators);
  127. subject.Compressors.Should().Equal(__defaults.Compressors);
  128. subject.MaxIdleTime.Should().Be(__defaults.MaxIdleTime);
  129. subject.MaxLifeTime.Should().Be(maxLifeTime);
  130. }
  131. [Fact]
  132. public void With_applicationName_should_return_expected_result()
  133. {
  134. var oldApplicationName = "app1";
  135. var newApplicationName = "app2";
  136. var subject = new ConnectionSettings(applicationName: oldApplicationName);
  137. var result = subject.With(applicationName: newApplicationName);
  138. result.ApplicationName.Should().Be(newApplicationName);
  139. result.Authenticators.Should().Equal(subject.Authenticators);
  140. subject.Compressors.Should().Equal(__defaults.Compressors);
  141. result.MaxIdleTime.Should().Be(subject.MaxIdleTime);
  142. result.MaxLifeTime.Should().Be(subject.MaxLifeTime);
  143. }
  144. [Fact]
  145. public void With_authenticators_should_return_expected_result()
  146. {
  147. #pragma warning disable 618
  148. var oldAuthenticators = new[] { new MongoDBCRAuthenticator(new UsernamePasswordCredential("source", "username1", "password1")) };
  149. var newAuthenticators = new[] { new MongoDBCRAuthenticator(new UsernamePasswordCredential("source", "username2", "password2")) };
  150. #pragma warning restore 618
  151. var subject = new ConnectionSettings(authenticators: oldAuthenticators);
  152. var result = subject.With(authenticators: newAuthenticators);
  153. result.ApplicationName.Should().Be(subject.ApplicationName);
  154. result.Authenticators.Should().Equal(newAuthenticators);
  155. subject.Compressors.Should().Equal(subject.Compressors);
  156. result.MaxIdleTime.Should().Be(subject.MaxIdleTime);
  157. result.MaxLifeTime.Should().Be(subject.MaxLifeTime);
  158. }
  159. [Fact]
  160. public void With_compressors_should_return_expected_result()
  161. {
  162. var oldCompressors = new[] { new CompressorConfiguration(CompressorType.Zlib) };
  163. var newCompressors = new[] { new CompressorConfiguration(CompressorType.Snappy) };
  164. var subject = new ConnectionSettings(compressors: oldCompressors);
  165. var result = subject.With(compressors: newCompressors);
  166. result.ApplicationName.Should().Be(subject.ApplicationName);
  167. result.Authenticators.Should().Equal(subject.Authenticators);
  168. result.Compressors.Should().Equal(newCompressors);
  169. result.MaxIdleTime.Should().Be(subject.MaxIdleTime);
  170. result.MaxLifeTime.Should().Be(subject.MaxLifeTime);
  171. }
  172. [Fact]
  173. public void With_maxIdleTime_should_return_expected_result()
  174. {
  175. var oldMaxIdleTime = TimeSpan.FromSeconds(1);
  176. var newMaxIdleTime = TimeSpan.FromSeconds(2);
  177. var subject = new ConnectionSettings(maxIdleTime: oldMaxIdleTime);
  178. var result = subject.With(maxIdleTime: newMaxIdleTime);
  179. result.ApplicationName.Should().Be(subject.ApplicationName);
  180. result.Authenticators.Should().Equal(subject.Authenticators);
  181. result.Compressors.Should().Equal(subject.Compressors);
  182. result.MaxIdleTime.Should().Be(newMaxIdleTime);
  183. result.MaxLifeTime.Should().Be(subject.MaxLifeTime);
  184. }
  185. [Fact]
  186. public void With_maxLifeTime_should_return_expected_result()
  187. {
  188. var oldMaxLifeTime = TimeSpan.FromSeconds(1);
  189. var newMaxLifeTime = TimeSpan.FromSeconds(2);
  190. var subject = new ConnectionSettings(maxLifeTime: oldMaxLifeTime);
  191. var result = subject.With(maxLifeTime: newMaxLifeTime);
  192. result.ApplicationName.Should().Be(subject.ApplicationName);
  193. result.Authenticators.Should().Equal(subject.Authenticators);
  194. result.Compressors.Should().Equal(subject.Compressors);
  195. result.MaxIdleTime.Should().Be(subject.MaxIdleTime);
  196. result.MaxLifeTime.Should().Be(newMaxLifeTime);
  197. }
  198. }
  199. }