From f28a8c87abe2def002987e5b170f83d8603e0708 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 30 Nov 2023 09:51:18 +1100 Subject: [PATCH] Activate background report processing by default Many report specs are still testing the old behaviour. We need to migrate them to background reports. Some tests may be better as unit tests instead of system tests. --- ...222807_enable_feature_backround_reports.rb | 7 +++++ lib/open_food_network/feature_toggle.rb | 26 +++++++++++++++++- spec/base_spec_helper.rb | 27 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20231129222807_enable_feature_backround_reports.rb diff --git a/db/migrate/20231129222807_enable_feature_backround_reports.rb b/db/migrate/20231129222807_enable_feature_backround_reports.rb new file mode 100644 index 0000000000..a0688cb66c --- /dev/null +++ b/db/migrate/20231129222807_enable_feature_backround_reports.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class EnableFeatureBackroundReports < ActiveRecord::Migration[7.0] + def up + Flipper.enable("background_reports") + end +end diff --git a/lib/open_food_network/feature_toggle.rb b/lib/open_food_network/feature_toggle.rb index ccced3415c..f1a075ef8f 100644 --- a/lib/open_food_network/feature_toggle.rb +++ b/lib/open_food_network/feature_toggle.rb @@ -10,6 +10,17 @@ module OpenFoodNetwork # Please add your new feature here to appear in the Flipper UI. # We way move this to a YAML file when it becomes too awkward. # **WARNING:** Features not in this list will be removed. + # + # Once the feature is ready for general production use, + # copy the feature declaration to ACTIVE_BY_DEFAULT below and + # activate it for all instances with a migration: + # + # ./bin/rails generate migration EnableFeatureDragonMode + # + # Replace the `change` method with an `up` method and add this line: + # + # Flipper.enable("dragon_mode") + # CURRENT_FEATURES = { "admin_style_v3" => <<~DESC, Test the work-in-progress design updates. @@ -38,10 +49,23 @@ module OpenFoodNetwork DESC }.freeze + # Features you would like to be enabled to start with. + # + # Copy features here that were activated in a migration so that new + # instances, development and test environments have the feature active. + ACTIVE_BY_DEFAULT = { + "background_reports" => <<~DESC, + Generate reports in a background process to limit memory consumption. + DESC + }.freeze + def self.setup! CURRENT_FEATURES.each_key do |name| feature = Flipper.feature(name) - feature.add unless feature.exist? + unless feature.exist? + feature.add + feature.enable if ACTIVE_BY_DEFAULT[name] + end end Flipper.features.each do |feature| diff --git a/spec/base_spec_helper.rb b/spec/base_spec_helper.rb index ad026026ed..36895456dd 100644 --- a/spec/base_spec_helper.rb +++ b/spec/base_spec_helper.rb @@ -88,6 +88,33 @@ RSpec.configure do |config| expectations.syntax = :expect end + # Reset all feature toggles to prevent leaking. + config.before(:suite) do + Flipper.features.each(&:remove) + OpenFoodNetwork::FeatureToggle.setup! + end + + config.before(:each) do |example| + Flipper.disable(:background_reports) if example.file_path.in?( + [ + # rubocop:disable Layout/LineLength + "./spec/controllers/admin/reports_controller_spec.rb", + "./spec/system/admin/reports/enterprise_fee_summaries_spec.rb", + "./spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order_spec.rb", + "./spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_producer_spec.rb", + "./spec/system/admin/reports/orders_and_fulfillment_spec.rb", + "./spec/system/admin/reports/packing_report_spec.rb", + "./spec/system/admin/reports/payments_report_spec.rb", + "./spec/system/admin/reports/revenues_by_hub_spec.rb", + "./spec/system/admin/reports/sales_tax/sales_tax_totals_by_order_spec.rb", + "./spec/system/admin/reports/sales_tax/sales_tax_totals_by_producer_spec.rb", + "./spec/system/admin/reports/users_and_enterprises_spec.rb", + "./spec/system/admin/reports_spec.rb", + # rubocop:enable Layout/LineLength + ] + ) + end + config.before(:each, :feature) do |example| feature = example.metadata[:feature].to_s