From c407ba02618254358398369424dc967445389b51 Mon Sep 17 00:00:00 2001 From: Aleksey Vlasenko Date: Sun, 12 Apr 2020 15:38:41 -0700 Subject: [PATCH] - Fix for DownloadMode.UpdateSeedingDownloadingState logic. Currently it sends multiple TorrentStateChanged events during download --- .../DownloadModeTests.cs | 33 +++++++++++++++++++ .../MonoTorrent.Client/Modes/DownloadMode.cs | 4 +-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/MonoTorrent.Tests/MonoTorrent.Client.Modes/DownloadModeTests.cs b/src/MonoTorrent.Tests/MonoTorrent.Client.Modes/DownloadModeTests.cs index 470daa053..83dbaac39 100644 --- a/src/MonoTorrent.Tests/MonoTorrent.Client.Modes/DownloadModeTests.cs +++ b/src/MonoTorrent.Tests/MonoTorrent.Client.Modes/DownloadModeTests.cs @@ -396,6 +396,39 @@ public void PartialProgress_RelatedDownloaded2 () Assert.AreEqual (TorrentState.Downloading, Manager.State, "#2"); } + [Test] + public void PartialProgress_RelatedDownloaded_FileAdded () + { + Manager.OnPieceHashed (0, true); + + foreach (var file in Manager.Torrent.Files) + file.Priority = Priority.DoNotDownload; + Manager.Torrent.Files.First ().Priority = Priority.Normal; + + var mode = new DownloadMode (Manager, DiskManager, ConnectionManager, Settings); + Manager.Mode = mode; + mode.UpdateSeedingDownloadingState (); + Assert.AreEqual (TorrentState.Seeding, Manager.State, "#1"); + + Manager.Torrent.Files.Skip (1).First ().Priority = Priority.Normal; + TorrentState oldState = TorrentState.Error; + TorrentState newState = TorrentState.Error; + Manager.TorrentStateChanged += (object sender, TorrentStateChangedEventArgs e) => { + oldState = e.OldState; + newState = e.NewState; + }; + mode.UpdateSeedingDownloadingState (); + + Assert.That (Manager.Progress, Is.GreaterThan (0.0), "#3a"); + Assert.That (Manager.Progress, Is.LessThan (100.0), "#3b"); + + Assert.That (Manager.PartialProgress, Is.LessThan (100.0), "#4"); + Assert.AreEqual (TorrentState.Downloading, Manager.State, "#5"); + + Assert.AreEqual (TorrentState.Seeding, oldState, "#6"); + Assert.AreEqual (TorrentState.Downloading, newState, "#7"); + } + [Test] public void PartialProgress_UnrelatedDownloaded_AllDoNotDownload () { diff --git a/src/MonoTorrent/MonoTorrent.Client/Modes/DownloadMode.cs b/src/MonoTorrent/MonoTorrent.Client/Modes/DownloadMode.cs index cc1e06345..63d0548a4 100644 --- a/src/MonoTorrent/MonoTorrent.Client/Modes/DownloadMode.cs +++ b/src/MonoTorrent/MonoTorrent.Client/Modes/DownloadMode.cs @@ -87,8 +87,8 @@ internal void UpdateSeedingDownloadingState () state = TorrentState.Seeding; Manager.RaiseTorrentStateChanged (new TorrentStateChangedEventArgs (Manager, TorrentState.Downloading, TorrentState.Seeding)); } else if (Manager.Bitfield.CountTrue (Manager.PartialProgressSelector) < Manager.PartialProgressSelector.TrueCount && state == TorrentState.Seeding) { - state = TorrentState.Seeding; - Manager.RaiseTorrentStateChanged (new TorrentStateChangedEventArgs (Manager, TorrentState.Downloading, TorrentState.Seeding)); + state = TorrentState.Downloading; + Manager.RaiseTorrentStateChanged (new TorrentStateChangedEventArgs (Manager, TorrentState.Seeding, TorrentState.Downloading)); } } }