Skip to content

Commit 646032b

Browse files
committed
Fix unit test failures
1 parent 7584ee7 commit 646032b

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

lib/optimizely/config/datafile_project_config.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,12 +644,16 @@ def get_holdouts_for_flag(flag_id)
644644

645645
return [] if @holdouts.nil? || @holdouts.empty?
646646

647+
# Check cache first (before validation, so we cache the validation result too)
648+
return @flag_holdouts_map[flag_id] if @flag_holdouts_map.key?(flag_id)
649+
647650
# Validate that the flag exists in the datafile
648651
flag_exists = @feature_flags.any? { |flag| flag['id'] == flag_id }
649-
return [] unless flag_exists
650-
651-
# Check cache and return persistent holdouts
652-
return @flag_holdouts_map[flag_id] if @flag_holdouts_map.key?(flag_id)
652+
unless flag_exists
653+
# Cache the empty result for non-existent flags
654+
@flag_holdouts_map[flag_id] = []
655+
return []
656+
end
653657

654658
# Prioritize global holdouts first
655659
excluded = @excluded_holdouts[flag_id] || []

spec/config/datafile_project_config_spec.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@
12811281
end
12821282

12831283
it 'should return only global holdouts for flags not specifically targeted' do
1284-
string_feature_id = '594060'
1284+
string_feature_id = '155557'
12851285
holdouts = config_with_holdouts.get_holdouts_for_flag(string_feature_id)
12861286

12871287
# Should only include global holdout (not excluded and no specific targeting)
@@ -1366,8 +1366,8 @@
13661366
# Use the correct feature flag IDs from the debug output
13671367
boolean_feature_id = '155554'
13681368
multi_variate_feature_id = '155559'
1369-
empty_feature_id = '594032'
1370-
string_feature_id = '594060'
1369+
empty_feature_id = '155564'
1370+
string_feature_id = '155557'
13711371

13721372
config_body_with_holdouts['holdouts'] = [
13731373
{
@@ -1403,8 +1403,8 @@
14031403
# Use the correct feature flag IDs
14041404
boolean_feature_id = '155554'
14051405
multi_variate_feature_id = '155559'
1406-
empty_feature_id = '594032'
1407-
string_feature_id = '594060'
1406+
empty_feature_id = '155564'
1407+
string_feature_id = '155557'
14081408

14091409
expect(config_with_complex_holdouts.included_holdouts[multi_variate_feature_id]).not_to be_nil
14101410
expect(config_with_complex_holdouts.included_holdouts[multi_variate_feature_id]).not_to be_empty
@@ -1474,7 +1474,7 @@
14741474
feature_flag = config_with_holdouts.feature_flag_key_map['boolean_feature']
14751475
expect(feature_flag).not_to be_nil
14761476

1477-
holdouts_for_flag = config_with_holdouts.get_holdouts_for_flag('boolean_feature')
1477+
holdouts_for_flag = config_with_holdouts.get_holdouts_for_flag(feature_flag['id'])
14781478
expect(holdouts_for_flag).to be_an(Array)
14791479
end
14801480
end
@@ -1485,7 +1485,7 @@
14851485
feature_flag = config_with_holdouts.feature_flag_key_map['boolean_feature']
14861486

14871487
if feature_flag
1488-
holdouts_for_flag = config_with_holdouts.get_holdouts_for_flag('boolean_feature')
1488+
holdouts_for_flag = config_with_holdouts.get_holdouts_for_flag(feature_flag['id'])
14891489

14901490
# Should not include holdouts that exclude this flag
14911491
excluded_holdout = holdouts_for_flag.find { |h| h['key'] == 'excluded_holdout' }
@@ -1497,7 +1497,7 @@
14971497
feature_flag = config_with_holdouts.feature_flag_key_map['boolean_feature']
14981498
expect(feature_flag).not_to be_nil
14991499

1500-
holdouts_for_flag = config_with_holdouts.get_holdouts_for_flag('boolean_feature')
1500+
holdouts_for_flag = config_with_holdouts.get_holdouts_for_flag(feature_flag['id'])
15011501
expect(holdouts_for_flag).to be_an(Array)
15021502
end
15031503
end
@@ -1523,8 +1523,9 @@
15231523
end
15241524

15251525
it 'should properly cache holdout lookups' do
1526-
holdouts_1 = config_with_holdouts.get_holdouts_for_flag('boolean_feature')
1527-
holdouts_2 = config_with_holdouts.get_holdouts_for_flag('boolean_feature')
1526+
feature_flag = config_with_holdouts.feature_flag_key_map['boolean_feature']
1527+
holdouts_1 = config_with_holdouts.get_holdouts_for_flag(feature_flag['id'])
1528+
holdouts_2 = config_with_holdouts.get_holdouts_for_flag(feature_flag['id'])
15281529

15291530
expect(holdouts_1).to equal(holdouts_2)
15301531
end
@@ -1703,7 +1704,7 @@
17031704
feature_flag = config_with_holdouts.feature_flag_key_map['boolean_feature']
17041705
expect(feature_flag).not_to be_nil
17051706

1706-
holdouts_for_flag = config_with_holdouts.get_holdouts_for_flag('boolean_feature')
1707+
holdouts_for_flag = config_with_holdouts.get_holdouts_for_flag(feature_flag['id'])
17071708

17081709
holdouts_for_flag.each do |holdout|
17091710
# Each holdout should have necessary info for decision reasoning
@@ -1758,7 +1759,8 @@
17581759
error_handler
17591760
)
17601761

1761-
holdouts_for_flag = config_without_holdouts.get_holdouts_for_flag('boolean_feature')
1762+
feature_flag = config_without_holdouts.feature_flag_key_map['boolean_feature']
1763+
holdouts_for_flag = config_without_holdouts.get_holdouts_for_flag(feature_flag['id'])
17621764
expect(holdouts_for_flag).to eq([])
17631765
end
17641766

0 commit comments

Comments
 (0)