PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/Misc/ReferenceCountedTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 78 lines | 51 code | 13 blank | 14 comment | 0 complexity | 3de7a5928182ef5b77e5cc784f5c50b5 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.Misc;
  23. using MongoDB.Driver.Core.Servers;
  24. using Moq;
  25. using Xunit;
  26. namespace MongoDB.Driver.Core.Misc
  27. {
  28. public class ReferenceCountedTests
  29. {
  30. private Mock<IDisposable> _mockDisposable;
  31. public ReferenceCountedTests()
  32. {
  33. _mockDisposable = new Mock<IDisposable>();
  34. }
  35. [Fact]
  36. public void Constructor_should_throw_if_instance_is_null()
  37. {
  38. Action act = () => new ReferenceCounted<IDisposable>(null);
  39. act.ShouldThrow<ArgumentNullException>();
  40. }
  41. [Fact]
  42. public void Initial_reference_count_should_be_one()
  43. {
  44. var subject = new ReferenceCounted<IDisposable>(_mockDisposable.Object);
  45. subject.ReferenceCount.Should().Be(1);
  46. }
  47. [Fact]
  48. public void Decrement_should_not_call_dispose_when_reference_count_is_greater_than_zero()
  49. {
  50. var subject = new ReferenceCounted<IDisposable>(_mockDisposable.Object);
  51. subject.IncrementReferenceCount();
  52. subject.DecrementReferenceCount();
  53. subject.ReferenceCount.Should().Be(1);
  54. _mockDisposable.Verify(d => d.Dispose(), Times.Never);
  55. }
  56. [Fact]
  57. public void Decrement_should_call_dispose_when_reference_count_is_zero()
  58. {
  59. var subject = new ReferenceCounted<IDisposable>(_mockDisposable.Object);
  60. subject.DecrementReferenceCount();
  61. subject.ReferenceCount.Should().Be(0);
  62. _mockDisposable.Verify(d => d.Dispose(), Times.Once);
  63. }
  64. }
  65. }