-
-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to prioritize downloads by host
- Loading branch information
Showing
10 changed files
with
182 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
|
||
namespace CKAN | ||
{ | ||
public class PreferredHostUriComparer : IComparer<Uri> | ||
{ | ||
public PreferredHostUriComparer(IEnumerable<string> hosts) | ||
{ | ||
this.hosts = (hosts ?? Enumerable.Empty<string>()).ToList(); | ||
// null represents the position in the list for all other hosts | ||
defaultPriority = this.hosts.IndexOf(null); | ||
} | ||
|
||
public int Compare(Uri a, Uri b) | ||
=> GetPriority(a).CompareTo(GetPriority(b)); | ||
|
||
private int GetPriority(Uri u) | ||
{ | ||
var index = hosts.IndexOf(u.Host); | ||
return index == -1 ? defaultPriority | ||
: index; | ||
} | ||
|
||
private readonly List<string> hosts; | ||
private readonly int defaultPriority; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
using NUnit.Framework; | ||
|
||
using CKAN; | ||
|
||
namespace Tests.Core.Net | ||
{ | ||
[TestFixture] | ||
public sealed class PreferredHostUriComparerTests | ||
{ | ||
private static readonly Uri[] uris = new Uri[] | ||
{ | ||
new Uri("https://taniwha.org/"), | ||
new Uri("https://spacedock.info/"), | ||
new Uri("https://github.com/"), | ||
new Uri("https://archive.org/"), | ||
}; | ||
|
||
// Reminder: null means "all other hosts" | ||
|
||
[Test, | ||
// Null settings | ||
TestCase(null, | ||
new string[] | ||
{ | ||
"https://taniwha.org/", | ||
"https://spacedock.info/", | ||
"https://github.com/", | ||
"https://archive.org/", | ||
}), | ||
// Empty settings | ||
TestCase(new string[] { }, | ||
new string[] | ||
{ | ||
"https://taniwha.org/", | ||
"https://spacedock.info/", | ||
"https://github.com/", | ||
"https://archive.org/", | ||
}), | ||
// Irrelevant settings | ||
TestCase(new string[] { "api.github.com", "curseforge.com", null, "www.dropbox.com", "drive.google.com" }, | ||
new string[] | ||
{ | ||
"https://taniwha.org/", | ||
"https://spacedock.info/", | ||
"https://github.com/", | ||
"https://archive.org/", | ||
}), | ||
// Prioritize one | ||
TestCase(new string[] { "github.com", null }, | ||
new string[] | ||
{ | ||
"https://github.com/", | ||
"https://taniwha.org/", | ||
"https://spacedock.info/", | ||
"https://archive.org/", | ||
}), | ||
// De-prioritize one | ||
TestCase(new string[] { null, "spacedock.info" }, | ||
new string[] | ||
{ | ||
"https://taniwha.org/", | ||
"https://github.com/", | ||
"https://archive.org/", | ||
"https://spacedock.info/", | ||
}), | ||
// Prioritize one, de-prioritize another | ||
TestCase(new string[] { "github.com", null, "spacedock.info" }, | ||
new string[] | ||
{ | ||
"https://github.com/", | ||
"https://taniwha.org/", | ||
"https://archive.org/", | ||
"https://spacedock.info/", | ||
}), | ||
] | ||
public void OrderBy_WithPreferences_SortsCorrectly(string[] preferredHosts, | ||
string[] correctAnswer) | ||
{ | ||
// Arrange | ||
var comparer = new PreferredHostUriComparer(preferredHosts); | ||
|
||
// Act | ||
var result = uris.OrderBy(u => u, comparer) | ||
.Select(u => u.ToString()) | ||
.ToArray(); | ||
|
||
// Assert | ||
Assert.AreEqual(correctAnswer, result); | ||
} | ||
} | ||
} |