diff --git a/lib/tzinfo.rb b/lib/tzinfo.rb index df447a9d..b1b53449 100644 --- a/lib/tzinfo.rb +++ b/lib/tzinfo.rb @@ -3,6 +3,18 @@ # The top level module for TZInfo. module TZInfo + class << self + # Instructs the current {DataSource} to load all timezone and country data + # into memory (initializing the {DataSource} first if not previously + # accessed or set). + # + # This may be desirable in production environments to improve copy-on-write + # performance and to avoid flushing the constant cache every time a new + # timezone or country is loaded from {DataSources::RubyDataSource}. + def eager_load! + DataSource.get.eager_load! + end + end end # Object#untaint is a deprecated no-op in Ruby >= 2.7 and will be removed in diff --git a/lib/tzinfo/data_source.rb b/lib/tzinfo/data_source.rb index 3dbac86b..952b435a 100644 --- a/lib/tzinfo/data_source.rb +++ b/lib/tzinfo/data_source.rb @@ -247,11 +247,12 @@ def country_codes raise_invalid_data_source('country_codes') end - # Loads all timezone and country data into memory. This may be desirable in - # production environments to improve copy-on-write performance and to - # avoid flushing the constant cache every time a new timezone or country - # is loaded from {DataSources::RubyDataSource}. - def preload! + # Loads all timezone and country data into memory. + # + # This may be desirable in production environments to improve copy-on-write + # performance and to avoid flushing the constant cache every time a new + # timezone or country is loaded from {DataSources::RubyDataSource}. + def eager_load! timezone_identifiers.each {|identifier| load_timezone_info(identifier) } country_codes.each {|code| load_country_info(code) } nil diff --git a/test/tc_data_source.rb b/test/tc_data_source.rb index 28103e12..1bdbe8a9 100644 --- a/test/tc_data_source.rb +++ b/test/tc_data_source.rb @@ -87,7 +87,7 @@ def call_lookup_country_info(hash, code, encoding = Encoding::UTF_8) end end - class PreloadTestDataSource < GetTimezoneIdentifiersTestDataSource + class EagerLoadTestDataSource < GetTimezoneIdentifiersTestDataSource attr_reader :country_codes_called attr_reader :loaded_timezones attr_reader :loaded_countries @@ -581,13 +581,13 @@ def test_lookup_country_info_case end end - def test_preload + def test_eager_load data_timezone_identifiers = ['Data/Zone1', 'Data/Zone2'] linked_timezone_identifiers = ['Linked/Zone1', 'Linked/Zone2'] all_timezone_identifiers = data_timezone_identifiers + linked_timezone_identifiers country_codes = ['AA', 'BB', 'CC'] - ds = PreloadTestDataSource.new(data_timezone_identifiers, linked_timezone_identifiers, country_codes) - assert_nil(ds.preload!) + ds = EagerLoadTestDataSource.new(data_timezone_identifiers, linked_timezone_identifiers, country_codes) + assert_nil(ds.eager_load!) assert_equal(1, ds.data_timezone_identifiers_called) assert_equal(1, ds.linked_timezone_identifiers_called) assert_equal(all_timezone_identifiers, ds.loaded_timezones) diff --git a/test/tc_tzinfo.rb b/test/tc_tzinfo.rb new file mode 100644 index 00000000..c24e226b --- /dev/null +++ b/test/tc_tzinfo.rb @@ -0,0 +1,21 @@ +# encoding: UTF-8 +# frozen_string_literal: true + +require_relative 'test_utils' + +class TCTZInfo < Minitest::Test + def test_eager_load + test_data_source = Minitest::Mock.new + test_data_source.expect(:kind_of?, true, [DataSource]) + test_data_source.expect(:eager_load!, nil) + + orig_data_source = DataSource.get + DataSource.set(test_data_source) + begin + assert_nil(TZInfo.eager_load!) + test_data_source.verify + ensure + DataSource.set(orig_data_source) + end + end +end