Skip to content

Commit

Permalink
Compute earliest install time
Browse files Browse the repository at this point in the history
  • Loading branch information
rcaelers committed Sep 14, 2024
1 parent c2faebb commit 8582d7e
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/Checker.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public:
virtual std::shared_ptr<unfold::UpdateInfo> get_update_info() const = 0;
virtual std::shared_ptr<AppcastItem> get_selected_update() const = 0;
virtual std::chrono::seconds get_rollout_delay_for_priority(int priority) const = 0;
virtual std::chrono::system_clock::time_point get_earliest_rollout_time_for_priority(int priority) const = 0;
};

#endif // CHECKER_HH
18 changes: 17 additions & 1 deletion src/UpgradeChecker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
#include <boost/url/url.hpp>
#include <boost/process.hpp>

#include "UnfoldErrors.hh"
#include "utils/DateUtils.hh"

#include "UnfoldErrors.hh"
#include "Platform.hh"

UpgradeChecker::UpgradeChecker(std::shared_ptr<Platform> platform,
Expand Down Expand Up @@ -106,6 +107,20 @@ UpgradeChecker::get_rollout_delay_for_priority(int priority) const
return std::chrono::seconds(0);
}

std::chrono::system_clock::time_point
UpgradeChecker::get_earliest_rollout_time_for_priority(int priority) const
{
if (selected_item == nullptr)
{
return std::chrono::system_clock::from_time_t(0);
}

auto delay = get_rollout_delay_for_priority(priority);
auto pub_date = unfold::utils::DateUtils::parse_time_point(selected_item->publication_date);

return pub_date + delay;
}

boost::asio::awaitable<outcome::std_result<bool>>
UpgradeChecker::check_for_update()
{
Expand Down Expand Up @@ -239,6 +254,7 @@ UpgradeChecker::build_update_info(std::shared_ptr<Appcast> appcast)
for (auto x: items)
{
spdlog::info("applicable {}", x->version);

auto r = unfold::UpdateReleaseNotes{x->version, x->publication_date, x->description};
update_info->release_notes.push_back(r);
}
Expand Down
1 change: 1 addition & 0 deletions src/UpgradeChecker.hh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public:
std::shared_ptr<unfold::UpdateInfo> get_update_info() const override;
std::shared_ptr<AppcastItem> get_selected_update() const override;
std::chrono::seconds get_rollout_delay_for_priority(int priority) const override;
std::chrono::system_clock::time_point get_earliest_rollout_time_for_priority(int priority) const override;

private:
boost::asio::awaitable<outcome::std_result<std::string>> download_appcast();
Expand Down
12 changes: 6 additions & 6 deletions test/AppCastTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ BOOST_AUTO_TEST_CASE(appcast_load_from_string)
" <sparkle:channel>release</sparkle:channel>\n"
" <sparkle:version>1.0</sparkle:version>\n"
" <sparkle:releaseNotesLink>https://workrave.org/v1.html</sparkle:releaseNotesLink>\n"
" <pubDate>Sun Apr 17 19:30:14 CEST 2022</pubDate>\n"
" <pubDate>Sun, 17 Apr 2022 19:30:14 +0200</pubDate>\n"
" <enclosure url=\"http://localhost:1337/v2.zip\" sparkle:edSignature=\"xx\" length=\"1234\" type=\"application/octet-stream\" />\n"
" </item>\n"
" </channel>\n"
Expand All @@ -67,7 +67,7 @@ BOOST_AUTO_TEST_CASE(appcast_load_from_string)
BOOST_CHECK_EQUAL(appcast->items[0]->short_version, "");
BOOST_CHECK_EQUAL(appcast->items[0]->description, "");
BOOST_CHECK_EQUAL(appcast->items[0]->release_notes_link, "https://workrave.org/v1.html");
BOOST_CHECK_EQUAL(appcast->items[0]->publication_date, "Sun Apr 17 19:30:14 CEST 2022");
BOOST_CHECK_EQUAL(appcast->items[0]->publication_date, "Sun, 17 Apr 2022 19:30:14 +0200");
BOOST_CHECK_EQUAL(appcast->items[0]->minimum_system_version, "");
BOOST_CHECK_EQUAL(appcast->items[0]->minimum_auto_update_version, "");
BOOST_CHECK_EQUAL(appcast->items[0]->ignore_skipped_upgrades_below_version, "");
Expand Down Expand Up @@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(appcast_load_from_file)
BOOST_CHECK_EQUAL(appcast->items[0]->short_version, "");
BOOST_CHECK_EQUAL(appcast->items[0]->description, "");
BOOST_CHECK_EQUAL(appcast->items[0]->release_notes_link, "https://workrave.org/v1.html");
BOOST_CHECK_EQUAL(appcast->items[0]->publication_date, "Sun Apr 17 19:30:14 CEST 2022");
BOOST_CHECK_EQUAL(appcast->items[0]->publication_date, "Sun, 17 Apr 2022 19:30:14 +0200");
BOOST_CHECK_EQUAL(appcast->items[0]->minimum_system_version, "");
BOOST_CHECK_EQUAL(appcast->items[0]->minimum_auto_update_version, "");
BOOST_CHECK_EQUAL(appcast->items[0]->ignore_skipped_upgrades_below_version, "");
Expand All @@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE(appcast_load_from_file)
BOOST_CHECK_EQUAL(appcast->items[1]->short_version, "");
BOOST_CHECK_EQUAL(appcast->items[1]->description, "Version 2 update");
BOOST_CHECK_EQUAL(appcast->items[1]->release_notes_link, "");
BOOST_CHECK_EQUAL(appcast->items[1]->publication_date, "Sun Apr 17 19:30:14 CEST 2022");
BOOST_CHECK_EQUAL(appcast->items[1]->publication_date, "Sun, 17 Apr 2022 19:30:14 +0200");
BOOST_CHECK_EQUAL(appcast->items[1]->minimum_system_version, "");
BOOST_CHECK_EQUAL(appcast->items[1]->minimum_auto_update_version, "");
BOOST_CHECK_EQUAL(appcast->items[1]->ignore_skipped_upgrades_below_version, "");
Expand Down Expand Up @@ -158,7 +158,7 @@ BOOST_AUTO_TEST_CASE(appcast_canary)
BOOST_CHECK_EQUAL(appcast->items[0]->short_version, "");
BOOST_CHECK_EQUAL(appcast->items[0]->description, "");
BOOST_CHECK_EQUAL(appcast->items[0]->release_notes_link, "https://workrave.org/v1.html");
BOOST_CHECK_EQUAL(appcast->items[0]->publication_date, "Sun Apr 17 19:30:14 CEST 2022");
BOOST_CHECK_EQUAL(appcast->items[0]->publication_date, "Sun, 17 Apr 2022 19:30:14 +0200");
BOOST_CHECK_EQUAL(appcast->items[0]->minimum_system_version, "");
BOOST_CHECK_EQUAL(appcast->items[0]->minimum_auto_update_version, "");
BOOST_CHECK_EQUAL(appcast->items[0]->ignore_skipped_upgrades_below_version, "");
Expand All @@ -183,7 +183,7 @@ BOOST_AUTO_TEST_CASE(appcast_canary)
BOOST_CHECK_EQUAL(appcast->items[1]->short_version, "");
BOOST_CHECK_EQUAL(appcast->items[1]->description, "Version 2 update");
BOOST_CHECK_EQUAL(appcast->items[1]->release_notes_link, "");
BOOST_CHECK_EQUAL(appcast->items[1]->publication_date, "Sun Apr 17 19:30:14 CEST 2022");
BOOST_CHECK_EQUAL(appcast->items[1]->publication_date, "Sun, 17 Apr 2022 19:30:14 +0200");
BOOST_CHECK_EQUAL(appcast->items[1]->minimum_system_version, "");
BOOST_CHECK_EQUAL(appcast->items[1]->minimum_auto_update_version, "");
BOOST_CHECK_EQUAL(appcast->items[1]->ignore_skipped_upgrades_below_version, "");
Expand Down
1 change: 1 addition & 0 deletions test/CheckerMock.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public:
MOCK_METHOD(std::shared_ptr<unfold::UpdateInfo>, get_update_info, (), (const, override));
MOCK_METHOD(std::shared_ptr<AppcastItem>, get_selected_update, (), (const, override));
MOCK_METHOD(std::chrono::seconds, get_rollout_delay_for_priority, (int priority), (const, override));
MOCK_METHOD(std::chrono::system_clock::time_point, get_earliest_rollout_time_for_priority, (int priority), (const, override));
};

#endif // CHECKER_MOCK_HH
81 changes: 81 additions & 0 deletions test/CheckerTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "unfold/UnfoldErrors.hh"
#include "http/HttpServer.hh"
#include "utils/DateUtils.hh"

#include "TestPlatform.hh"
#include "TestBase.hh"
Expand Down Expand Up @@ -542,6 +543,86 @@ BOOST_AUTO_TEST_CASE(checker_delay)
server.stop();
}

BOOST_AUTO_TEST_CASE(checker_earliest_rollout)
{
unfold::http::HttpServer server;
server.add_file("/appcast.xml", "appcast-canary.xml");
server.run();

auto http = std::make_shared<unfold::http::HttpClient>();
auto &options = http->options();
options.add_ca_cert(cert);

auto hooks = std::make_shared<Hooks>();

UpgradeChecker checker(std::make_shared<TestPlatform>(), http, hooks);

auto rc = checker.set_appcast("https://127.0.0.1:1337/appcast.xml");
BOOST_CHECK_EQUAL(rc.has_error(), false);

rc = checker.set_current_version("1.0.0");
BOOST_CHECK_EQUAL(rc.has_error(), false);

boost::asio::io_context ioc;
boost::asio::co_spawn(
ioc,
[&]() -> boost::asio::awaitable<void> {
try
{
auto pub_date = unfold::utils::DateUtils::parse_time_point("Sun, 17 Apr 2022 19:30:14 +0200");

auto rollout_time = checker.get_earliest_rollout_time_for_priority(0);
BOOST_CHECK_EQUAL(rollout_time, std::chrono::system_clock::from_time_t(0));

auto check_result = co_await checker.check_for_update();
BOOST_CHECK_EQUAL(check_result.has_error(), false);
BOOST_CHECK_EQUAL(check_result.value(), true);

rollout_time = checker.get_earliest_rollout_time_for_priority(0);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(0));
rollout_time = checker.get_earliest_rollout_time_for_priority(1);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(0));
rollout_time = checker.get_earliest_rollout_time_for_priority(9);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(0));
rollout_time = checker.get_earliest_rollout_time_for_priority(10);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(0));
rollout_time = checker.get_earliest_rollout_time_for_priority(11);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(2 * 24 * 60 * 60));
rollout_time = checker.get_earliest_rollout_time_for_priority(12);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(2 * 24 * 60 * 60));
rollout_time = checker.get_earliest_rollout_time_for_priority(24);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(2 * 24 * 60 * 60));
rollout_time = checker.get_earliest_rollout_time_for_priority(25);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(2 * 24 * 60 * 60));
rollout_time = checker.get_earliest_rollout_time_for_priority(26);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(5 * 24 * 60 * 60));
rollout_time = checker.get_earliest_rollout_time_for_priority(27);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(5 * 24 * 60 * 60));
rollout_time = checker.get_earliest_rollout_time_for_priority(54);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(5 * 24 * 60 * 60));
rollout_time = checker.get_earliest_rollout_time_for_priority(55);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(5 * 24 * 60 * 60));
rollout_time = checker.get_earliest_rollout_time_for_priority(56);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(10 * 24 * 60 * 60));
rollout_time = checker.get_earliest_rollout_time_for_priority(57);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(10 * 24 * 60 * 60));
rollout_time = checker.get_earliest_rollout_time_for_priority(99);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(10 * 24 * 60 * 60));
rollout_time = checker.get_earliest_rollout_time_for_priority(100);
BOOST_CHECK_EQUAL(rollout_time, pub_date + std::chrono::seconds(10 * 24 * 60 * 60));
}
catch (std::exception &e)
{
spdlog::info("Exception {}", e.what());
BOOST_CHECK(false);
}
},
boost::asio::detached);
ioc.run();

server.stop();
}

BOOST_AUTO_TEST_CASE(checker_channels_allowed_none)
{
unfold::http::HttpServer server;
Expand Down
4 changes: 2 additions & 2 deletions test/data/appcast-canary-error.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link>https://workrave.org</link>
<sparkle:version>1.0</sparkle:version>
<sparkle:releaseNotesLink>https://workrave.org/v1.html</sparkle:releaseNotesLink>
<pubDate>Sun Apr 17 19:30:14 CEST 2022</pubDate>
<pubDate>Sun, 17 Apr 2022 19:30:14 +0200</pubDate>
<enclosure url="http://localhost:1337/v2.zip" sparkle:edSignature="xx" length="1234" type="application/octet-stream" />
<sparkle:phasedRolloutInterval>2</sparkle:phasedRolloutInterval>
<unfold:canary>
Expand All @@ -34,7 +34,7 @@
<item>
<title>Version 2.0</title>
<description>Version 2 update</description>
<pubDate>Sun Apr 17 19:30:14 CEST 2022</pubDate>
<pubDate>Sun, 17 Apr 2022 19:30:14 +0200</pubDate>
<enclosure url="http://localhost:1337/v1.zip" sparkle:version="2.0" />
<sparkle:criticalUpdate sparkle:version="1.5" />
</item>
Expand Down
4 changes: 2 additions & 2 deletions test/data/appcast-canary-sparkle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
<link>https://workrave.org</link>
<sparkle:version>1.0</sparkle:version>
<sparkle:releaseNotesLink>https://workrave.org/v1.html</sparkle:releaseNotesLink>
<pubDate>Sun Apr 17 19:30:14 CEST 2022</pubDate>
<pubDate>Sun, 17 Apr 2022 19:30:14 +0200</pubDate>
<enclosure url="http://localhost:1337/v2.zip" sparkle:edSignature="xx" length="1234" type="application/octet-stream" />
<sparkle:phasedRolloutInterval>172800</sparkle:phasedRolloutInterval>
</item>

<item>
<title>Version 2.0</title>
<description>Version 2 update</description>
<pubDate>Sun Apr 17 19:30:14 CEST 2022</pubDate>
<pubDate>Sun, 17 Apr 2022 19:30:14 +0200</pubDate>
<enclosure url="http://localhost:1337/v1.zip" sparkle:version="2.0" />
<sparkle:criticalUpdate sparkle:version="1.5" />
</item>
Expand Down
4 changes: 2 additions & 2 deletions test/data/appcast-canary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link>https://workrave.org</link>
<sparkle:version>1.0.0</sparkle:version>
<sparkle:releaseNotesLink>https://workrave.org/v1.html</sparkle:releaseNotesLink>
<pubDate>Sun Apr 17 19:30:14 CEST 2022</pubDate>
<pubDate>Sun, 17 Apr 2022 19:30:14 +0200</pubDate>
<enclosure os="windows-x64" url="http://localhost:1337/v2.zip" sparkle:edSignature="xx" length="1234" type="application/octet-stream" />
<unfold:canary>
<interval>
Expand All @@ -38,7 +38,7 @@
<item>
<title>Version 2.0</title>
<description>Version 2 update</description>
<pubDate>Sun Apr 17 19:30:14 CEST 2022</pubDate>
<pubDate>Sun, 17 Apr 2022 19:30:14 +0200</pubDate>
<sparkle:version>2.0.0</sparkle:version>
<enclosure os="windows-x64" url="http://localhost:1337/v1.zip" sparkle:version="2.0" />
<sparkle:criticalUpdate sparkle:version="1.5" />
Expand Down
4 changes: 2 additions & 2 deletions test/data/okappcast.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
<link>https://workrave.org</link>
<sparkle:version>1.0</sparkle:version>
<sparkle:releaseNotesLink>https://workrave.org/v1.html</sparkle:releaseNotesLink>
<pubDate>Sun Apr 17 19:30:14 CEST 2022</pubDate>
<pubDate>Sun, 17 Apr 2022 19:30:14 +0200</pubDate>
<enclosure url="http://localhost:1337/v2.zip" sparkle:edSignature="xx" length="1234" type="application/octet-stream" />
</item>

<item>
<title>Version 2.0</title>
<description>Version 2 update</description>
<pubDate>Sun Apr 17 19:30:14 CEST 2022</pubDate>
<pubDate>Sun, 17 Apr 2022 19:30:14 +0200</pubDate>
<enclosure url="http://localhost:1337/v1.zip" sparkle:version="2.0" />
<sparkle:criticalUpdate sparkle:version="1.5" />
</item>
Expand Down
4 changes: 2 additions & 2 deletions test/data/testappcast.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
<link>https://workrave.org</link>
<sparkle:version>1.0.0</sparkle:version>
<sparkle:releaseNotesLink>https://workrave.org/v1.html</sparkle:releaseNotesLink>
<pubDate>Sun Apr 17 19:30:14 CEST 2022</pubDate>
<pubDate>Sun, 17 Apr 2022 19:30:14 +0200</pubDate>
<enclosure url="http://localhost:1337/v2.zip" sparkle:edSignature="xx" length="1234" type="application/octet-stream" />
</item>

<item>
<title>Version 2.0</title>
<description>Version 2 update</description>
<pubDate>Sun Apr 17 19:30:14 CEST 2022</pubDate>
<pubDate>Sun, 17 Apr 2022 19:30:14 +0200</pubDate>
<enclosure url="http://localhost:1337/v1.zip" sparkle:version="2.0" />
<sparkle:criticalUpdate sparkle:version="1.5" />
</item>
Expand Down

0 comments on commit 8582d7e

Please sign in to comment.