|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | # |
4 | | -# Copyright 2017, Optimizely and contributors |
| 4 | +# Copyright 2017-2018, Optimizely and contributors |
5 | 5 | # |
6 | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
7 | 7 | # you may not use this file except in compliance with the License. |
|
487 | 487 | feature_flag = config.feature_flag_key_map['boolean_single_variable_feature'] |
488 | 488 | rollout_experiment = config.rollout_id_map[feature_flag['rolloutId']]['experiments'][0] |
489 | 489 | variation = rollout_experiment['variations'][0] |
490 | | - audience_id = rollout_experiment['audienceIds'][0] |
491 | | - audience_name = config.get_audience_from_id(audience_id)['name'] |
492 | 490 | expected_decision = Optimizely::DecisionService::Decision.new(rollout_experiment, variation, Optimizely::DecisionService::DECISION_SOURCE_ROLLOUT) |
493 | 491 | allow(Optimizely::Audience).to receive(:user_in_experiment?).and_return(true) |
494 | 492 | allow(decision_service.bucketer).to receive(:bucket) |
495 | 493 | .with(rollout_experiment, user_id, user_id) |
496 | 494 | .and_return(variation) |
497 | 495 | expect(decision_service.get_variation_for_feature_rollout(feature_flag, user_id, user_attributes)).to eq(expected_decision) |
498 | | - expect(spy_logger).to have_received(:log).once |
499 | | - .with(Logger::DEBUG, "Attempting to bucket user '#{user_id}' into rollout rule "\ |
500 | | - "for audience '#{audience_name}'.") |
501 | 496 | end |
502 | 497 | end |
503 | 498 |
|
|
523 | 518 | .with(config, rollout['experiments'][0], user_attributes) |
524 | 519 | expect(Optimizely::Audience).not_to have_received(:user_in_experiment?) |
525 | 520 | .with(config, rollout['experiments'][1], user_attributes) |
526 | | - |
527 | | - # verify log messages |
528 | | - experiment = rollout['experiments'][0] |
529 | | - audience_id = experiment['audienceIds'][0] |
530 | | - audience_name = config.get_audience_from_id(audience_id)['name'] |
531 | | - |
532 | | - expect(spy_logger).to have_received(:log).once |
533 | | - .with(Logger::DEBUG, "Attempting to bucket user '#{user_id}' into rollout rule "\ |
534 | | - "for audience '#{audience_name}'.") |
535 | | - expect(spy_logger).to have_received(:log).once |
536 | | - .with(Logger::DEBUG, "User '#{user_id}' was excluded due to traffic allocation. Checking 'Everyone Else' rule now.") |
537 | | - expect(spy_logger).to have_received(:log).once |
538 | | - .with(Logger::DEBUG, "User '#{user_id}' was excluded from the 'Everyone Else' rule for feature flag") |
539 | 521 | end |
540 | 522 | end |
541 | 523 |
|
|
561 | 543 | .with(config, rollout['experiments'][0], user_attributes) |
562 | 544 | expect(Optimizely::Audience).not_to have_received(:user_in_experiment?) |
563 | 545 | .with(config, rollout['experiments'][1], user_attributes) |
564 | | - |
565 | | - # verify log messages |
566 | | - experiment = rollout['experiments'][0] |
567 | | - audience_id = experiment['audienceIds'][0] |
568 | | - audience_name = config.get_audience_from_id(audience_id)['name'] |
569 | | - |
570 | | - expect(spy_logger).to have_received(:log).once |
571 | | - .with(Logger::DEBUG, "Attempting to bucket user '#{user_id}' into rollout rule for audience '#{audience_name}'.") |
572 | | - expect(spy_logger).to have_received(:log).once |
573 | | - .with(Logger::DEBUG, "User '#{user_id}' was excluded due to traffic allocation. Checking 'Everyone Else' rule now.") |
574 | 546 | end |
575 | 547 | end |
576 | 548 | end |
|
584 | 556 | variation = everyone_else_experiment['variations'][0] |
585 | 557 | expected_decision = Optimizely::DecisionService::Decision.new(everyone_else_experiment, variation, Optimizely::DecisionService::DECISION_SOURCE_ROLLOUT) |
586 | 558 | allow(Optimizely::Audience).to receive(:user_in_experiment?).and_return(false) |
| 559 | + |
| 560 | + allow(Optimizely::Audience).to receive(:user_in_experiment?) |
| 561 | + .with(config, everyone_else_experiment, user_attributes) |
| 562 | + .and_return(true) |
587 | 563 | allow(decision_service.bucketer).to receive(:bucket) |
588 | 564 | .with(everyone_else_experiment, user_id, user_id) |
589 | 565 | .and_return(variation) |
590 | 566 |
|
591 | 567 | expect(decision_service.get_variation_for_feature_rollout(feature_flag, user_id, user_attributes)).to eq(expected_decision) |
592 | 568 |
|
593 | | - # verify we tried to bucket in all targeting rules except for the everyone else rule |
| 569 | + # verify we tried to bucket in all targeting rules and the everyone else rule |
594 | 570 | expect(Optimizely::Audience).to have_received(:user_in_experiment?).once |
595 | 571 | .with(config, rollout['experiments'][0], user_attributes) |
596 | 572 | expect(Optimizely::Audience).to have_received(:user_in_experiment?) |
597 | 573 | .with(config, rollout['experiments'][1], user_attributes) |
598 | | - expect(Optimizely::Audience).not_to have_received(:user_in_experiment?) |
| 574 | + expect(Optimizely::Audience).to have_received(:user_in_experiment?) |
599 | 575 | .with(config, rollout['experiments'][2], user_attributes) |
600 | 576 |
|
601 | 577 | # verify log messages |
|
611 | 587 | expect(spy_logger).to have_received(:log).once |
612 | 588 | .with(Logger::DEBUG, "User '#{user_id}' does not meet the conditions to be in rollout rule for audience '#{audience_name}'.") |
613 | 589 | end |
| 590 | + |
| 591 | + it 'should not bucket the user into the "Everyone Else" rule when audience mismatch' do |
| 592 | + feature_flag = config.feature_flag_key_map['boolean_single_variable_feature'] |
| 593 | + rollout = config.rollout_id_map[feature_flag['rolloutId']] |
| 594 | + everyone_else_experiment = rollout['experiments'][2] |
| 595 | + everyone_else_experiment['audienceIds'] = ['11155'] |
| 596 | + allow(Optimizely::Audience).to receive(:user_in_experiment?).and_return(false) |
| 597 | + |
| 598 | + expect(decision_service.bucketer).not_to receive(:bucket) |
| 599 | + .with(everyone_else_experiment, user_id, user_id) |
| 600 | + |
| 601 | + expect(decision_service.get_variation_for_feature_rollout(feature_flag, user_id, user_attributes)).to eq(nil) |
| 602 | + |
| 603 | + # verify we tried to bucket in all targeting rules and the everyone else rule |
| 604 | + expect(Optimizely::Audience).to have_received(:user_in_experiment?).once |
| 605 | + .with(config, rollout['experiments'][0], user_attributes) |
| 606 | + expect(Optimizely::Audience).to have_received(:user_in_experiment?) |
| 607 | + .with(config, rollout['experiments'][1], user_attributes) |
| 608 | + expect(Optimizely::Audience).to have_received(:user_in_experiment?) |
| 609 | + .with(config, rollout['experiments'][2], user_attributes) |
| 610 | + |
| 611 | + # verify log messages |
| 612 | + experiment = rollout['experiments'][0] |
| 613 | + audience_id = experiment['audienceIds'][0] |
| 614 | + audience_name = config.get_audience_from_id(audience_id)['name'] |
| 615 | + expect(spy_logger).to have_received(:log).once |
| 616 | + .with(Logger::DEBUG, "User '#{user_id}' does not meet the conditions to be in rollout rule for audience '#{audience_name}'.") |
| 617 | + |
| 618 | + experiment = rollout['experiments'][1] |
| 619 | + audience_id = experiment['audienceIds'][0] |
| 620 | + audience_name = config.get_audience_from_id(audience_id)['name'] |
| 621 | + expect(spy_logger).to have_received(:log).twice |
| 622 | + .with(Logger::DEBUG, "User '#{user_id}' does not meet the conditions to be in rollout rule for audience '#{audience_name}'.") |
| 623 | + end |
614 | 624 | end |
615 | 625 | end |
616 | 626 |
|
|
0 commit comments