DisposableConcurrentDictionaryTests.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Collections.Generic;
  2. using Masuit.Tools.Systems;
  3. using Xunit;
  4. namespace Masuit.Tools.Abstractions.Test.Systems;
  5. public class DisposableConcurrentDictionaryTests
  6. {
  7. [Fact]
  8. public void Constructor_Default_ShouldInitialize()
  9. {
  10. var dictionary = new DisposableConcurrentDictionary<string, DisposableValue>();
  11. Assert.NotNull(dictionary);
  12. }
  13. [Fact]
  14. public void Constructor_WithFallbackValue_ShouldInitialize()
  15. {
  16. var fallbackValue = new DisposableValue();
  17. var dictionary = new DisposableConcurrentDictionary<string, DisposableValue>(fallbackValue);
  18. Assert.NotNull(dictionary);
  19. }
  20. [Fact]
  21. public void Constructor_WithConcurrencyLevelAndCapacity_ShouldInitialize()
  22. {
  23. var dictionary = new DisposableConcurrentDictionary<string, DisposableValue>(4, 100);
  24. Assert.NotNull(dictionary);
  25. }
  26. [Fact]
  27. public void Constructor_WithComparer_ShouldInitialize()
  28. {
  29. var comparer = EqualityComparer<NullObject<string>>.Default;
  30. var dictionary = new DisposableConcurrentDictionary<string, DisposableValue>(comparer);
  31. Assert.NotNull(dictionary);
  32. }
  33. [Fact]
  34. public void Dispose_ShouldDisposeAllValues()
  35. {
  36. var value1 = new DisposableValue();
  37. var value2 = new DisposableValue();
  38. var dictionary = new DisposableConcurrentDictionary<string, DisposableValue>
  39. {
  40. ["key1"] = value1,
  41. ["key2"] = value2
  42. };
  43. dictionary.Dispose();
  44. Assert.True(value1.IsDisposed);
  45. Assert.True(value2.IsDisposed);
  46. }
  47. [Fact]
  48. public void Dispose_MultipleTimes_ShouldNotThrow()
  49. {
  50. var dictionary = new DisposableConcurrentDictionary<string, DisposableValue>();
  51. dictionary.Dispose();
  52. var exception = Record.Exception(() => dictionary.Dispose());
  53. Assert.Null(exception);
  54. }
  55. }
  56. public class DisposableValue : Disposable
  57. {
  58. public bool IsDisposed { get; private set; }
  59. /// <summary>
  60. /// 释放
  61. /// </summary>
  62. /// <param name="disposing"></param>
  63. public override void Dispose(bool disposing)
  64. {
  65. IsDisposed = true;
  66. }
  67. }