From 0a70c836df765e8241e9335722557fcb9d567983 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 20 Nov 2011 20:43:16 -0800 Subject: [PATCH] Cleaned up progress notification. --- NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 1 + .../NotificationProviderFixture.cs | 71 +++++++++++++++++++ .../Notification/ProgressNotification.cs | 2 +- .../Providers/NotificationProvider.cs | 35 ++------- .../Controllers/NotificationController.cs | 12 ++-- 5 files changed, 86 insertions(+), 35 deletions(-) create mode 100644 NzbDrone.Core.Test/ProviderTests/NotificationProviderTests/NotificationProviderFixture.cs diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 734ca6950..e9739c412 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -84,6 +84,7 @@ + diff --git a/NzbDrone.Core.Test/ProviderTests/NotificationProviderTests/NotificationProviderFixture.cs b/NzbDrone.Core.Test/ProviderTests/NotificationProviderTests/NotificationProviderFixture.cs new file mode 100644 index 000000000..5f14a331c --- /dev/null +++ b/NzbDrone.Core.Test/ProviderTests/NotificationProviderTests/NotificationProviderFixture.cs @@ -0,0 +1,71 @@ +using System.Linq; +using System.Threading; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Model.Notification; +using NzbDrone.Core.Providers; + +namespace NzbDrone.Core.Test.ProviderTests.NotificationProviderTests +{ + [TestFixture] + public class NotificationProviderFixture + { + NotificationProvider _notificationProvider; + + [SetUp] + public void Setup() + { + _notificationProvider = new NotificationProvider(); + } + + [Test] + public void current_notification_should_return_null_at_start() + { + _notificationProvider.GetCurrent().Should().BeNull(); + } + + [Test] + public void should_return_current_on_active_notifications() + { + var fakeNotification = new ProgressNotification("Title"); + _notificationProvider.Register(fakeNotification); + + _notificationProvider.GetCurrent().Should().Be(fakeNotification); + } + + [Test] + public void should_return_last_if_recently_completed() + { + var fakeNotification = new ProgressNotification("Title"); + _notificationProvider.Register(fakeNotification); + fakeNotification.Dispose(); + + _notificationProvider.GetCurrent().Should().Be(fakeNotification); + } + + [Test] + public void should_return_null_if_completed_long_time_ago() + { + var fakeNotification = new ProgressNotification("Title"); + _notificationProvider.Register(fakeNotification); + fakeNotification.Dispose(); + + Thread.Sleep(4000); + + _notificationProvider.GetCurrent().Should().BeNull(); + } + + [Test] + public void new_notification_should_replace_old_one() + { + var oldNotification = new ProgressNotification("Title"); + _notificationProvider.Register(oldNotification); + + var newNotification = new ProgressNotification("Title"); + _notificationProvider.Register(newNotification); + + _notificationProvider.GetCurrent().Should().Be(newNotification); + } + + } +} diff --git a/NzbDrone.Core/Model/Notification/ProgressNotification.cs b/NzbDrone.Core/Model/Notification/ProgressNotification.cs index 80e8bc64a..8e6b84f28 100644 --- a/NzbDrone.Core/Model/Notification/ProgressNotification.cs +++ b/NzbDrone.Core/Model/Notification/ProgressNotification.cs @@ -79,7 +79,7 @@ namespace NzbDrone.Core.Model.Notification /// /// Gets the completed time. /// - public DateTime CompletedTime { get; private set; } + public Nullable CompletedTime { get; private set; } #region IDisposable Members diff --git a/NzbDrone.Core/Providers/NotificationProvider.cs b/NzbDrone.Core/Providers/NotificationProvider.cs index a76e7244f..8c3a072bf 100644 --- a/NzbDrone.Core/Providers/NotificationProvider.cs +++ b/NzbDrone.Core/Providers/NotificationProvider.cs @@ -7,42 +7,21 @@ namespace NzbDrone.Core.Providers { public class NotificationProvider { + private static ProgressNotification _currentNotification; - private static readonly Object _lock = new object(); - - private static readonly Dictionary _progressNotification = - new Dictionary(); - - public virtual List ProgressNotifications + public virtual ProgressNotification GetCurrent() { - get + if (_currentNotification == null || _currentNotification.CompletedTime < DateTime.Now.AddSeconds(-3)) { - lock (_lock) - { - var activeNotification = - _progressNotification.Values.Where(p => p.Status == ProgressNotificationStatus.InProgress). - ToList(); - - if (activeNotification.Count == 0) - { - //Get notifications that were recently done - activeNotification = - _progressNotification.Values.Where(p => p.CompletedTime >= DateTime.Now.AddSeconds(-3)). - OrderByDescending(c => c.CompletedTime).ToList(); - - } - - return activeNotification.ToList(); - } + return null; } + + return _currentNotification; } public virtual void Register(ProgressNotification notification) { - lock (_lock) - { - _progressNotification.Add(notification.Id, notification); - } + _currentNotification = notification; } } } \ No newline at end of file diff --git a/NzbDrone.Web/Controllers/NotificationController.cs b/NzbDrone.Web/Controllers/NotificationController.cs index 5477a5459..8f719ce54 100644 --- a/NzbDrone.Web/Controllers/NotificationController.cs +++ b/NzbDrone.Web/Controllers/NotificationController.cs @@ -9,13 +9,14 @@ namespace NzbDrone.Web.Controllers { public class NotificationController : Controller { - private readonly NotificationProvider _notifications; + private readonly NotificationProvider _notificationProvider; + // // GET: /Notification/ public NotificationController(NotificationProvider notificationProvider) { - _notifications = notificationProvider; + _notificationProvider = notificationProvider; } [HttpGet] @@ -36,11 +37,10 @@ namespace NzbDrone.Web.Controllers private string GetCurrentMessage() { - var notes = _notifications.ProgressNotifications; - - if (_notifications.ProgressNotifications.Count > 0) - return _notifications.ProgressNotifications[0].CurrentMessage; + var notification = _notificationProvider.GetCurrent(); + if (notification != null) + return notification.CurrentMessage; return string.Empty; }