/src/NzbDrone.Core.Test/Download/DownloadClientProviderFixture.cs

https://github.com/NzbDrone/NzbDrone · C# · 275 lines · 232 code · 43 blank · 0 comment · 0 complexity · 1b1f7d08c22993c36ec7ae579d585c34 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using FizzWare.NBuilder;
  6. using FluentAssertions;
  7. using Moq;
  8. using NUnit.Framework;
  9. using NzbDrone.Core.Download;
  10. using NzbDrone.Core.Download.Clients;
  11. using NzbDrone.Core.Indexers;
  12. using NzbDrone.Core.Test.Framework;
  13. namespace NzbDrone.Core.Test.Download
  14. {
  15. [TestFixture]
  16. public class DownloadClientProviderFixture : CoreTest<DownloadClientProvider>
  17. {
  18. private List<IDownloadClient> _downloadClients;
  19. private List<DownloadClientStatus> _blockedProviders;
  20. private int _nextId;
  21. [SetUp]
  22. public void SetUp()
  23. {
  24. _downloadClients = new List<IDownloadClient>();
  25. _blockedProviders = new List<DownloadClientStatus>();
  26. _nextId = 1;
  27. Mocker.GetMock<IDownloadClientFactory>()
  28. .Setup(v => v.GetAvailableProviders())
  29. .Returns(_downloadClients);
  30. Mocker.GetMock<IDownloadClientStatusService>()
  31. .Setup(v => v.GetBlockedProviders())
  32. .Returns(_blockedProviders);
  33. }
  34. private Mock<IDownloadClient> WithUsenetClient(int priority = 0)
  35. {
  36. var mock = new Mock<IDownloadClient>(MockBehavior.Default);
  37. mock.SetupGet(s => s.Definition)
  38. .Returns(Builder<DownloadClientDefinition>
  39. .CreateNew()
  40. .With(v => v.Id = _nextId++)
  41. .With(v => v.Priority = priority)
  42. .Build());
  43. _downloadClients.Add(mock.Object);
  44. mock.SetupGet(v => v.Protocol).Returns(DownloadProtocol.Usenet);
  45. return mock;
  46. }
  47. private Mock<IDownloadClient> WithTorrentClient(int priority = 0)
  48. {
  49. var mock = new Mock<IDownloadClient>(MockBehavior.Default);
  50. mock.SetupGet(s => s.Definition)
  51. .Returns(Builder<DownloadClientDefinition>
  52. .CreateNew()
  53. .With(v => v.Id = _nextId++)
  54. .With(v => v.Priority = priority)
  55. .Build());
  56. _downloadClients.Add(mock.Object);
  57. mock.SetupGet(v => v.Protocol).Returns(DownloadProtocol.Torrent);
  58. return mock;
  59. }
  60. private void WithTorrentIndexer(int downloadClientId)
  61. {
  62. Mocker.GetMock<IIndexerFactory>()
  63. .Setup(v => v.Find(It.IsAny<int>()))
  64. .Returns(Builder<IndexerDefinition>
  65. .CreateNew()
  66. .With(v => v.Id = _nextId++)
  67. .With(v => v.DownloadClientId = downloadClientId)
  68. .Build());
  69. }
  70. private void GivenBlockedClient(int id)
  71. {
  72. _blockedProviders.Add(new DownloadClientStatus
  73. {
  74. ProviderId = id,
  75. DisabledTill = DateTime.UtcNow.AddHours(3)
  76. });
  77. }
  78. [Test]
  79. public void should_roundrobin_over_usenet_client()
  80. {
  81. WithUsenetClient();
  82. WithUsenetClient();
  83. WithUsenetClient();
  84. WithTorrentClient();
  85. var client1 = Subject.GetDownloadClient(DownloadProtocol.Usenet);
  86. var client2 = Subject.GetDownloadClient(DownloadProtocol.Usenet);
  87. var client3 = Subject.GetDownloadClient(DownloadProtocol.Usenet);
  88. var client4 = Subject.GetDownloadClient(DownloadProtocol.Usenet);
  89. var client5 = Subject.GetDownloadClient(DownloadProtocol.Usenet);
  90. client1.Definition.Id.Should().Be(1);
  91. client2.Definition.Id.Should().Be(2);
  92. client3.Definition.Id.Should().Be(3);
  93. client4.Definition.Id.Should().Be(1);
  94. client5.Definition.Id.Should().Be(2);
  95. }
  96. [Test]
  97. public void should_roundrobin_over_torrent_client()
  98. {
  99. WithUsenetClient();
  100. WithTorrentClient();
  101. WithTorrentClient();
  102. WithTorrentClient();
  103. var client1 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  104. var client2 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  105. var client3 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  106. var client4 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  107. var client5 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  108. client1.Definition.Id.Should().Be(2);
  109. client2.Definition.Id.Should().Be(3);
  110. client3.Definition.Id.Should().Be(4);
  111. client4.Definition.Id.Should().Be(2);
  112. client5.Definition.Id.Should().Be(3);
  113. }
  114. [Test]
  115. public void should_roundrobin_over_protocol_separately()
  116. {
  117. WithUsenetClient();
  118. WithTorrentClient();
  119. WithTorrentClient();
  120. var client1 = Subject.GetDownloadClient(DownloadProtocol.Usenet);
  121. var client2 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  122. var client3 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  123. var client4 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  124. client1.Definition.Id.Should().Be(1);
  125. client2.Definition.Id.Should().Be(2);
  126. client3.Definition.Id.Should().Be(3);
  127. client4.Definition.Id.Should().Be(2);
  128. }
  129. [Test]
  130. public void should_skip_blocked_torrent_client()
  131. {
  132. WithUsenetClient();
  133. WithTorrentClient();
  134. WithTorrentClient();
  135. WithTorrentClient();
  136. GivenBlockedClient(3);
  137. var client1 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  138. var client2 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  139. var client3 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  140. var client4 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  141. var client5 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  142. client1.Definition.Id.Should().Be(2);
  143. client2.Definition.Id.Should().Be(4);
  144. client3.Definition.Id.Should().Be(2);
  145. client4.Definition.Id.Should().Be(4);
  146. }
  147. [Test]
  148. public void should_not_skip_blocked_torrent_client_if_all_blocked()
  149. {
  150. WithUsenetClient();
  151. WithTorrentClient();
  152. WithTorrentClient();
  153. WithTorrentClient();
  154. GivenBlockedClient(2);
  155. GivenBlockedClient(3);
  156. GivenBlockedClient(4);
  157. var client1 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  158. var client2 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  159. var client3 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  160. var client4 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  161. var client5 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  162. client1.Definition.Id.Should().Be(2);
  163. client2.Definition.Id.Should().Be(3);
  164. client3.Definition.Id.Should().Be(4);
  165. client4.Definition.Id.Should().Be(2);
  166. }
  167. [Test]
  168. public void should_skip_secondary_prio_torrent_client()
  169. {
  170. WithUsenetClient();
  171. WithTorrentClient(2);
  172. WithTorrentClient();
  173. WithTorrentClient();
  174. var client1 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  175. var client2 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  176. var client3 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  177. var client4 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  178. var client5 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  179. client1.Definition.Id.Should().Be(3);
  180. client2.Definition.Id.Should().Be(4);
  181. client3.Definition.Id.Should().Be(3);
  182. client4.Definition.Id.Should().Be(4);
  183. }
  184. [Test]
  185. public void should_not_skip_secondary_prio_torrent_client_if_primary_blocked()
  186. {
  187. WithUsenetClient();
  188. WithTorrentClient(2);
  189. WithTorrentClient(2);
  190. WithTorrentClient();
  191. GivenBlockedClient(4);
  192. var client1 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  193. var client2 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  194. var client3 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  195. var client4 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  196. var client5 = Subject.GetDownloadClient(DownloadProtocol.Torrent);
  197. client1.Definition.Id.Should().Be(2);
  198. client2.Definition.Id.Should().Be(3);
  199. client3.Definition.Id.Should().Be(2);
  200. client4.Definition.Id.Should().Be(3);
  201. }
  202. [Test]
  203. public void should_always_choose_indexer_client()
  204. {
  205. WithUsenetClient();
  206. WithTorrentClient();
  207. WithTorrentClient();
  208. WithTorrentClient();
  209. WithTorrentIndexer(3);
  210. var client1 = Subject.GetDownloadClient(DownloadProtocol.Torrent, 1);
  211. var client2 = Subject.GetDownloadClient(DownloadProtocol.Torrent, 1);
  212. var client3 = Subject.GetDownloadClient(DownloadProtocol.Torrent, 1);
  213. var client4 = Subject.GetDownloadClient(DownloadProtocol.Torrent, 1);
  214. var client5 = Subject.GetDownloadClient(DownloadProtocol.Torrent, 1);
  215. client1.Definition.Id.Should().Be(3);
  216. client2.Definition.Id.Should().Be(3);
  217. client3.Definition.Id.Should().Be(3);
  218. client4.Definition.Id.Should().Be(3);
  219. client5.Definition.Id.Should().Be(3);
  220. }
  221. [Test]
  222. public void should_fail_to_choose_client_when_indexer_reference_does_not_exist()
  223. {
  224. WithUsenetClient();
  225. WithTorrentClient();
  226. WithTorrentClient();
  227. WithTorrentClient();
  228. WithTorrentIndexer(5);
  229. Assert.Throws<DownloadClientUnavailableException>(() => Subject.GetDownloadClient(DownloadProtocol.Torrent, 1));
  230. }
  231. }
  232. }