PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/Events/EventAggregatorTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 77 lines | 52 code | 11 blank | 14 comment | 0 complexity | c5cbeb4c06c4f4516ea41574ee7f0626 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 Xunit;
  18. namespace MongoDB.Driver.Core.Events
  19. {
  20. public class EventAggregatorTests
  21. {
  22. [Fact]
  23. public void TryGetEventHandler_should_return_false_when_no_subscribers_exist()
  24. {
  25. var subject = new EventAggregator();
  26. Action<int> handler;
  27. subject.TryGetEventHandler(out handler).Should().BeFalse();
  28. }
  29. [Fact]
  30. public void TryGetEventHandler_should_return_true_when_one_subscriber_exists()
  31. {
  32. var subject = new EventAggregator();
  33. subject.Subscribe<int>(x => { });
  34. Action<int> handler;
  35. subject.TryGetEventHandler(out handler).Should().BeTrue();
  36. }
  37. [Fact]
  38. public void Handler_should_invoke_a_single_subscriber()
  39. {
  40. var subject = new EventAggregator();
  41. bool called = false;
  42. subject.Subscribe<int>(x => called = true);
  43. Action<int> handler;
  44. subject.TryGetEventHandler(out handler);
  45. handler(42);
  46. called.Should().BeTrue();
  47. }
  48. [Fact]
  49. public void Handler_should_invoke_multiple_subscribers()
  50. {
  51. var subject = new EventAggregator();
  52. bool called1 = false;
  53. bool called2 = false;
  54. bool called3 = false;
  55. subject.Subscribe<int>(x => called1 = true);
  56. subject.Subscribe<int>(x => called2 = true);
  57. subject.Subscribe<int>(x => called3 = true);
  58. Action<int> handler;
  59. subject.TryGetEventHandler(out handler);
  60. handler(42);
  61. called1.Should().BeTrue();
  62. called2.Should().BeTrue();
  63. called3.Should().BeTrue();
  64. }
  65. }
  66. }