PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 144 lines | 102 code | 28 blank | 14 comment | 0 complexity | 23f7b72f73b2df98948cabb99bf67724 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-2014 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.Driver.Core.Authentication;
  18. using NUnit.Framework;
  19. namespace MongoDB.Driver.Core.Configuration
  20. {
  21. [TestFixture]
  22. public class ConnectionSettingsTests
  23. {
  24. private static readonly ConnectionSettings __defaults = new ConnectionSettings();
  25. [Test]
  26. public void constructor_should_initialize_instance()
  27. {
  28. var subject = new ConnectionSettings();
  29. subject.Authenticators.Should().BeEmpty();
  30. subject.MaxIdleTime.Should().Be(TimeSpan.FromMinutes(10));
  31. subject.MaxLifeTime.Should().Be(TimeSpan.FromMinutes(30));
  32. }
  33. [Test]
  34. public void constructor_should_throw_when_authenticators_is_null()
  35. {
  36. Action action = () => new ConnectionSettings(authenticators: null);
  37. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("authenticators");
  38. }
  39. [Test]
  40. public void constructor_should_throw_when_maxIdleTime_is_negative_or_zero(
  41. [Values(-1, 0)]
  42. int maxIdleTime)
  43. {
  44. Action action = () => new ConnectionSettings(maxIdleTime: TimeSpan.FromSeconds(maxIdleTime));
  45. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("maxIdleTime");
  46. }
  47. [Test]
  48. public void constructor_should_throw_when_maxLifeTime_is_negative_or_zero(
  49. [Values(-1, 0)]
  50. int maxLifeTime)
  51. {
  52. Action action = () => new ConnectionSettings(maxLifeTime: TimeSpan.FromSeconds(maxLifeTime));
  53. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("maxLifeTime");
  54. }
  55. [Test]
  56. public void constructor_with_authenticators_should_initialize_instance()
  57. {
  58. var authenticators = new[] { new MongoDBCRAuthenticator(new UsernamePasswordCredential("source", "username", "password")) };
  59. var subject = new ConnectionSettings(authenticators: authenticators);
  60. subject.Authenticators.Should().Equal(authenticators);
  61. subject.MaxIdleTime.Should().Be(__defaults.MaxIdleTime);
  62. subject.MaxLifeTime.Should().Be(__defaults.MaxLifeTime);
  63. }
  64. [Test]
  65. public void constructor_with_maxIdleTime_should_initialize_instance()
  66. {
  67. var maxIdleTime = TimeSpan.FromSeconds(123);
  68. var subject = new ConnectionSettings(maxIdleTime: maxIdleTime);
  69. subject.Authenticators.Should().Equal(__defaults.Authenticators);
  70. subject.MaxIdleTime.Should().Be(maxIdleTime);
  71. subject.MaxLifeTime.Should().Be(__defaults.MaxLifeTime);
  72. }
  73. [Test]
  74. public void constructor_with_maxLifeTime_should_initialize_instance()
  75. {
  76. var maxLifeTime = TimeSpan.FromSeconds(123);
  77. var subject = new ConnectionSettings(maxLifeTime: maxLifeTime);
  78. subject.Authenticators.Should().Equal(__defaults.Authenticators);
  79. subject.MaxIdleTime.Should().Be(subject.MaxIdleTime);
  80. subject.MaxLifeTime.Should().Be(maxLifeTime);
  81. }
  82. [Test]
  83. public void With_authenticators_should_return_expected_result()
  84. {
  85. var oldAuthenticators = new[] { new MongoDBCRAuthenticator(new UsernamePasswordCredential("source", "username1", "password1")) };
  86. var newAuthenticators = new[] { new MongoDBCRAuthenticator(new UsernamePasswordCredential("source", "username2", "password2")) };
  87. var subject = new ConnectionSettings(authenticators: oldAuthenticators);
  88. var result = subject.With(authenticators: newAuthenticators);
  89. result.Authenticators.Should().Equal(newAuthenticators);
  90. result.MaxIdleTime.Should().Be(subject.MaxIdleTime);
  91. result.MaxLifeTime.Should().Be(subject.MaxLifeTime);
  92. }
  93. [Test]
  94. public void With_maxIdleTime_should_return_expected_result()
  95. {
  96. var oldMaxIdleTime = TimeSpan.FromSeconds(1);
  97. var newMaxIdleTime = TimeSpan.FromSeconds(2);
  98. var subject = new ConnectionSettings(maxIdleTime: oldMaxIdleTime);
  99. var result = subject.With(maxIdleTime: newMaxIdleTime);
  100. result.Authenticators.Should().Equal(subject.Authenticators);
  101. result.MaxIdleTime.Should().Be(newMaxIdleTime);
  102. result.MaxLifeTime.Should().Be(subject.MaxLifeTime);
  103. }
  104. [Test]
  105. public void With_maxLifeTime_should_return_expected_result()
  106. {
  107. var oldMaxLifeTime = TimeSpan.FromSeconds(1);
  108. var newMaxLifeTime = TimeSpan.FromSeconds(2);
  109. var subject = new ConnectionSettings(maxLifeTime: oldMaxLifeTime);
  110. var result = subject.With(maxLifeTime: newMaxLifeTime);
  111. result.Authenticators.Should().Equal(subject.Authenticators);
  112. result.MaxIdleTime.Should().Be(subject.MaxIdleTime);
  113. result.MaxLifeTime.Should().Be(newMaxLifeTime);
  114. }
  115. }
  116. }