fix: fetch subscribed bundles on focus instead of every minute (#17939)

* fix: fetch subscribed bundles on focus instead of every minute

The preset sync loop asked Orca Cloud for the subscribed bundles every
60 s in every signed-in window, even in the background, though most users
have none. The preset push and deletes keep their 60 s cadence; the
subscriptions are now fetched:

- on the sync thread's first pass (startup, sign-in, Sync Presets);
- when the main window gains focus, the cloud pane closes, or the user
  leaves the Home tab with the cloud pane open, at most once a minute
  (a request inside the minute is kept until the minute is up);
- every 30 minutes for users who have subscribed bundles.

The schedule is OrcaCloudServiceAgent::subscriptions_fetch_due, covered
by new tests in test_orca_cloud_agent.cpp.

* fix: fetch subscribed bundles right after an unsubscribe

The Preset Bundle dialog's Unsubscribe only tells the server. The local
copy is removed by the sync loop's next subscriptions fetch, and while
the modal dialog is open nothing else asks for one, so the bundle stayed
listed. Ask for a fetch after a successful unsubscribe, so it goes within
a minute, as before.

* fix: poll subscriptions for all users with bounded retry traffic

---------

Co-authored-by: Ian Chua <iancrb00@gmail.com>
This commit is contained in:
weng haishiandIan Chua authored and GitHub committed 2026-12-01 21:16:30 +08:00
1 parent eb790b0a09
commit d64ebfc0ef
6 files changed
+163 -10

No files matched your search

@@ -24,6 +24,7 @@
#include <istream>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <string_view>
#include <thread>
@@ -901,3 +902,82 @@ TEST_CASE_METHOD(HealthCheckFixture, "A pull answered 304 keeps the sync state i
CHECK(agent->get_sync_state().last_sync_timestamp == 0);
CHECK_FALSE(fs::exists(dir.path() / "user" / "user-a" / "sync_state"));
}
namespace {
const std::chrono::steady_clock::time_point SYNC_T0 = std::chrono::steady_clock::time_point{} + 1000h;
using SubscriptionFetchSchedule = OrcaCloudServiceAgent::SubscriptionFetchSchedule;
} // namespace
TEST_CASE("The preset sync's first pass fetches the subscribed bundles", "[OrcaCloudServiceAgent][Subscriptions]")
{
// Startup, sign-in and Sync Presets all start a new sync thread, which has not fetched yet.
SubscriptionFetchSchedule schedule;
CHECK(schedule.due(SYNC_T0));
}
TEST_CASE("A signed-in user's subscriptions are polled every 30 minutes even with no bundles", "[OrcaCloudServiceAgent][Subscriptions]")
{
// A subscription made in the cloud pane or on another device is picked up by this poll, whatever the current
// bundle list holds.
SubscriptionFetchSchedule schedule;
schedule.note_attempt(SYNC_T0);
CHECK_FALSE(schedule.due(SYNC_T0 + 29min));
CHECK(schedule.due(SYNC_T0 + 30min));
}
TEST_CASE("A request within a minute of the last poll waits for the minute to pass", "[OrcaCloudServiceAgent][Subscriptions]")
{
SubscriptionFetchSchedule schedule;
schedule.note_attempt(SYNC_T0);
schedule.request();
// Asking whether a fetch is due does not consume the request, so repeated ticks keep it until the minute is up.
CHECK_FALSE(schedule.due(SYNC_T0 + 59s));
CHECK_FALSE(schedule.due(SYNC_T0 + 59s));
CHECK(schedule.due(SYNC_T0 + 1min));
}
TEST_CASE("A request made while a fetch is running is served by the next pass and then cleared", "[OrcaCloudServiceAgent][Subscriptions]")
{
SubscriptionFetchSchedule schedule;
schedule.note_attempt(SYNC_T0); // a fetch starts
schedule.request(); // the window gains focus while it runs
// Not dropped, but still throttled to once a minute.
CHECK_FALSE(schedule.due(SYNC_T0 + 59s));
CHECK(schedule.due(SYNC_T0 + 1min));
// The pass the request asked for runs; any further request would have to be made again.
schedule.note_attempt(SYNC_T0 + 1min);
CHECK_FALSE(schedule.due(SYNC_T0 + 1min + 1s));
CHECK(schedule.due(SYNC_T0 + 31min));
}
TEST_CASE("A pass that cannot fetch records no attempt", "[OrcaCloudServiceAgent][Subscriptions]")
{
// Signed out or without a preset bundle, the loop evaluates the schedule but skips the fetch, so it never records
// an attempt: the request keeps its place and is served by the first pass that can fetch.
SubscriptionFetchSchedule schedule;
schedule.note_attempt(SYNC_T0);
schedule.request();
CHECK_FALSE(schedule.due(SYNC_T0 + 30s));
REQUIRE(schedule.last_attempt == SYNC_T0);
// The request survives the skipped pass and is due once the minute since the real attempt is up.
CHECK(schedule.due(SYNC_T0 + 1min));
}
TEST_CASE("A failed fetch waits for the next slot instead of retrying on every tick", "[OrcaCloudServiceAgent][Subscriptions]")
{
// The attempt is recorded as it starts, so a failing server is polled again only after the fallback, not on each
// half-second tick.
SubscriptionFetchSchedule schedule;
schedule.note_attempt(SYNC_T0);
CHECK_FALSE(schedule.due(SYNC_T0 + 1s));
CHECK_FALSE(schedule.due(SYNC_T0 + 59s));
CHECK(schedule.due(SYNC_T0 + 30min));
}