1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
|
describe Scientist::Experiment do
class Fake
include Scientist::Experiment
# Undo auto-config magic / preserve default behavior of Scientist::Experiment.new
Scientist::Experiment.set_default(nil)
def initialize(*args)
end
def enabled?
true
end
attr_reader :published_result
def exceptions
@exceptions ||= []
end
def raised(op, exception)
exceptions << [op, exception]
end
def publish(result)
@published_result = result
end
end
before do
@ex = Fake.new
end
it "sets the default on inclusion" do
klass = Class.new do
include Scientist::Experiment
def initialize(name)
end
end
assert_kind_of klass, Scientist::Experiment.new("hello")
Scientist::Experiment.set_default(nil)
end
it "doesn't set the default on inclusion when it's a module" do
Module.new { include Scientist::Experiment }
assert_kind_of Scientist::Default, Scientist::Experiment.new("hello")
end
it "has a default implementation" do
ex = Scientist::Experiment.new("hello")
assert_kind_of Scientist::Default, ex
assert_equal "hello", ex.name
end
it "provides a static default name" do
assert_equal "experiment", Fake.new.name
end
it "requires includers to implement enabled?" do
obj = Object.new
obj.extend Scientist::Experiment
assert_raises NoMethodError do
obj.enabled?
end
end
it "requires includers to implement publish" do
obj = Object.new
obj.extend Scientist::Experiment
assert_raises NoMethodError do
obj.publish("result")
end
end
it "can't be run without a control behavior" do
e = assert_raises Scientist::BehaviorMissing do
@ex.run
end
assert_equal "control", e.name
end
it "is a straight pass-through with only a control behavior" do
@ex.use { "control" }
assert_equal "control", @ex.run
end
it "runs other behaviors but always returns the control" do
@ex.use { "control" }
@ex.try { "candidate" }
assert_equal "control", @ex.run
end
it "complains about duplicate behavior names" do
@ex.use { "control" }
e = assert_raises Scientist::BehaviorNotUnique do
@ex.use { "control-again" }
end
assert_equal @ex, e.experiment
assert_equal "control", e.name
end
it "swallows exceptions raised by candidate behaviors" do
@ex.use { "control" }
@ex.try { raise "candidate" }
assert_equal "control", @ex.run
end
it "passes through exceptions raised by the control behavior" do
@ex.use { raise "control" }
@ex.try { "candidate" }
exception = assert_raises RuntimeError do
@ex.run
end
assert_equal "control", exception.message
end
it "shuffles behaviors before running" do
last = nil
runs = []
@ex.use { last = "control" }
@ex.try { last = "candidate" }
10000.times do
@ex.run
runs << last
end
assert runs.uniq.size > 1
end
it "re-raises exceptions raised during publish by default" do
ex = Scientist::Experiment.new("hello")
assert_kind_of Scientist::Default, ex
def ex.enabled?
true
end
def ex.publish(result)
raise "boomtown"
end
ex.use { "control" }
ex.try { "candidate" }
exception = assert_raises RuntimeError do
ex.run
end
assert_equal "boomtown", exception.message
end
it "reports publishing errors" do
def @ex.publish(result)
raise "boomtown"
end
@ex.use { "control" }
@ex.try { "candidate" }
assert_equal "control", @ex.run
op, exception = @ex.exceptions.pop
assert_equal :publish, op
assert_equal "boomtown", exception.message
end
it "publishes results" do
@ex.use { 1 }
@ex.try { 1 }
assert_equal 1, @ex.run
assert @ex.published_result
end
it "does not publish results when there is only a control value" do
@ex.use { 1 }
assert_equal 1, @ex.run
assert_nil @ex.published_result
end
it "compares results with a comparator block if provided" do
@ex.compare { |a, b| a == b.to_s }
@ex.use { "1" }
@ex.try { 1 }
assert_equal "1", @ex.run
assert @ex.published_result.matched?
end
it "compares errors with an error comparator block if provided" do
@ex.compare_errors { |a, b| a.class == b.class }
@ex.use { raise "foo" }
@ex.try { raise "bar" }
resulting_error = assert_raises RuntimeError do
@ex.run
end
assert_equal "foo", resulting_error.message
assert @ex.published_result.matched?
end
it "knows how to compare two experiments" do
a = Scientist::Observation.new(@ex, "a") { 1 }
b = Scientist::Observation.new(@ex, "b") { 2 }
assert @ex.observations_are_equivalent?(a, a)
refute @ex.observations_are_equivalent?(a, b)
end
it "uses a compare block to determine if observations are equivalent" do
a = Scientist::Observation.new(@ex, "a") { "1" }
b = Scientist::Observation.new(@ex, "b") { 1 }
@ex.compare { |x, y| x == y.to_s }
assert @ex.observations_are_equivalent?(a, b)
end
it "reports errors in a compare block" do
@ex.compare { raise "boomtown" }
@ex.use { "control" }
@ex.try { "candidate" }
assert_equal "control", @ex.run
op, exception = @ex.exceptions.pop
assert_equal :compare, op
assert_equal "boomtown", exception.message
end
it "reports errors in the enabled? method" do
def @ex.enabled?
raise "kaboom"
end
@ex.use { "control" }
@ex.try { "candidate" }
assert_equal "control", @ex.run
op, exception = @ex.exceptions.pop
assert_equal :enabled, op
assert_equal "kaboom", exception.message
end
it "reports errors in a run_if block" do
@ex.run_if { raise "kaboom" }
@ex.use { "control" }
@ex.try { "candidate" }
assert_equal "control", @ex.run
op, exception = @ex.exceptions.pop
assert_equal :run_if, op
assert_equal "kaboom", exception.message
end
it "returns the given value when no clean block is configured" do
assert_equal 10, @ex.clean_value(10)
end
it "provides the clean block when asked for it, in case subclasses wish to override and provide defaults" do
assert_nil @ex.cleaner
cleaner = ->(value) { value.upcase }
@ex.clean(&cleaner)
assert_equal cleaner, @ex.cleaner
end
it "calls the configured clean block with a value when configured" do
@ex.clean do |value|
value.upcase
end
assert_equal "TEST", @ex.clean_value("test")
end
it "reports an error and returns the original value when an error is raised in a clean block" do
@ex.clean { |value| raise "kaboom" }
@ex.use { "control" }
@ex.try { "candidate" }
assert_equal "control", @ex.run
assert_equal "control", @ex.published_result.control.cleaned_value
op, exception = @ex.exceptions.pop
assert_equal :clean, op
assert_equal "kaboom", exception.message
end
describe "#raise_with" do
it "raises custom error if provided" do
CustomError = Class.new(Scientist::Experiment::MismatchError)
@ex.use { 1 }
@ex.try { 2 }
@ex.raise_with(CustomError)
@ex.raise_on_mismatches = true
assert_raises(CustomError) { @ex.run }
end
end
describe "#run_if" do
it "does not run the experiment if the given block returns false" do
candidate_ran = false
run_check_ran = false
@ex.use { 1 }
@ex.try { candidate_ran = true; 1 }
@ex.run_if { run_check_ran = true; false }
@ex.run
assert run_check_ran
refute candidate_ran
end
it "runs the experiment if the given block returns true" do
candidate_ran = false
run_check_ran = false
@ex.use { true }
@ex.try { candidate_ran = true }
@ex.run_if { run_check_ran = true }
@ex.run
assert run_check_ran
assert candidate_ran
end
end
describe "#ignore_mismatched_observation?" do
before do
@a = Scientist::Observation.new(@ex, "a") { 1 }
@b = Scientist::Observation.new(@ex, "b") { 2 }
end
it "does not ignore an observation if no ignores are configured" do
refute @ex.ignore_mismatched_observation?(@a, @b)
end
it "calls a configured ignore block with the given observed values" do
called = false
@ex.ignore do |a, b|
called = true
assert_equal @a.value, a
assert_equal @b.value, b
true
end
assert @ex.ignore_mismatched_observation?(@a, @b)
assert called
end
it "calls multiple ignore blocks to see if any match" do
called_one = called_two = called_three = false
@ex.ignore { |a, b| called_one = true; false }
@ex.ignore { |a, b| called_two = true; false }
@ex.ignore { |a, b| called_three = true; false }
refute @ex.ignore_mismatched_observation?(@a, @b)
assert called_one
assert called_two
assert called_three
end
it "only calls ignore blocks until one matches" do
called_one = called_two = called_three = false
@ex.ignore { |a, b| called_one = true; false }
@ex.ignore { |a, b| called_two = true; true }
@ex.ignore { |a, b| called_three = true; false }
assert @ex.ignore_mismatched_observation?(@a, @b)
assert called_one
assert called_two
refute called_three
end
it "reports exceptions raised in an ignore block and returns false" do
def @ex.exceptions
@exceptions ||= []
end
def @ex.raised(op, exception)
exceptions << [op, exception]
end
@ex.ignore { raise "kaboom" }
refute @ex.ignore_mismatched_observation?(@a, @b)
op, exception = @ex.exceptions.pop
assert_equal :ignore, op
assert_equal "kaboom", exception.message
end
it "skips ignore blocks that raise and tests any remaining blocks if an exception is swallowed" do
def @ex.exceptions
@exceptions ||= []
end
# this swallows the exception rather than re-raising
def @ex.raised(op, exception)
exceptions << [op, exception]
end
@ex.ignore { raise "kaboom" }
@ex.ignore { true }
assert @ex.ignore_mismatched_observation?(@a, @b)
assert_equal 1, @ex.exceptions.size
end
end
describe "raising on mismatches" do
before do
@old_raise_on_mismatches = Fake.raise_on_mismatches?
end
after do
Fake.raise_on_mismatches = @old_raise_on_mismatches
end
it "raises when there is a mismatch if raise on mismatches is enabled" do
Fake.raise_on_mismatches = true
@ex.use { "fine" }
@ex.try { "not fine" }
assert_raises(Scientist::Experiment::MismatchError) { @ex.run }
end
it "cleans values when raising on observation mismatch" do
Fake.raise_on_mismatches = true
@ex.use { "fine" }
@ex.try { "not fine" }
@ex.clean { "So Clean" }
err = assert_raises(Scientist::Experiment::MismatchError) { @ex.run }
assert_match /So Clean/, err.message
end
it "doesn't raise when there is a mismatch if raise on mismatches is disabled" do
Fake.raise_on_mismatches = false
@ex.use { "fine" }
@ex.try { "not fine" }
assert_equal "fine", @ex.run
end
it "raises a mismatch error if the control raises and candidate doesn't" do
Fake.raise_on_mismatches = true
@ex.use { raise "control" }
@ex.try { "candidate" }
assert_raises(Scientist::Experiment::MismatchError) { @ex.run }
end
it "raises a mismatch error if the candidate raises and the control doesn't" do
Fake.raise_on_mismatches = true
@ex.use { "control" }
@ex.try { raise "candidate" }
assert_raises(Scientist::Experiment::MismatchError) { @ex.run }
end
it "allows MismatchError to bubble up through bare rescues" do
Fake.raise_on_mismatches = true
@ex.use { "control" }
@ex.try { "candidate" }
runner = -> {
begin
@ex.run
rescue
# StandardError handled
end
}
assert_raises(Scientist::Experiment::MismatchError) { runner.call }
end
it "can be marshaled" do
Fake.raise_on_mismatches = true
@ex.before_run { "some block" }
@ex.clean { "some block" }
@ex.compare_errors { "some block" }
@ex.ignore { false }
@ex.run_if { "some block" }
@ex.try { "candidate" }
@ex.use { "control" }
@ex.compare { |control, candidate| control == candidate }
mismatch = nil
begin
@ex.run
rescue Scientist::Experiment::MismatchError => e
mismatch = e
end
assert_kind_of(String, Marshal.dump(mismatch))
end
it "can be marshal loaded" do
assert_kind_of(Fake, Marshal.load(Marshal.dump(@ex)))
end
describe "#raise_on_mismatches?" do
it "raises when there is a mismatch if the experiment instance's raise on mismatches is enabled" do
Fake.raise_on_mismatches = false
@ex.raise_on_mismatches = true
@ex.use { "fine" }
@ex.try { "not fine" }
assert_raises(Scientist::Experiment::MismatchError) { @ex.run }
end
it "doesn't raise when there is a mismatch if the experiment instance's raise on mismatches is disabled" do
Fake.raise_on_mismatches = true
@ex.raise_on_mismatches = false
@ex.use { "fine" }
@ex.try { "not fine" }
assert_equal "fine", @ex.run
end
it "respects the raise_on_mismatches class attribute by default" do
Fake.raise_on_mismatches = false
@ex.use { "fine" }
@ex.try { "not fine" }
assert_equal "fine", @ex.run
Fake.raise_on_mismatches = true
assert_raises(Scientist::Experiment::MismatchError) { @ex.run }
end
end
describe "MismatchError" do
before do
Fake.raise_on_mismatches = true
@ex.use { :foo }
@ex.try { :bar }
begin
@ex.run
rescue Scientist::Experiment::MismatchError => e
@mismatch = e
end
assert @mismatch
end
it "has the name of the experiment" do
assert_equal @ex.name, @mismatch.name
end
it "includes the experiments' results" do
assert_equal @ex.published_result, @mismatch.result
end
it "formats nicely as a string" do
assert_equal <<-STR, @mismatch.to_s
experiment 'experiment' observations mismatched:
control:
:foo
candidate:
:bar
STR
end
it "includes the backtrace when an observation raises" do
skip
mismatch = nil
ex = Fake.new
ex.use { "value" }
ex.try { raise "error" }
begin
ex.run
rescue Scientist::Experiment::MismatchError => e
mismatch = e
end
# Should look like this:
# experiment 'experiment' observations mismatched:
# control:
# "value"
# candidate:
# #<RuntimeError: error>
# test/scientist/experiment_test.rb:447:in `block (5 levels) in <top (required)>'
# ... (more backtrace)
lines = mismatch.to_s.split("\n")
assert_equal "control:", lines[1]
assert_equal " \"value\"", lines[2]
assert_equal "candidate:", lines[3]
assert_equal " #<RuntimeError: error>", lines[4]
assert_match %r(test/scientist/experiment_test.rb:\d+:in `block), lines[5]
end
end
end
describe "before run block" do
it "runs when an experiment is enabled" do
control_ok = candidate_ok = false
before = false
@ex.before_run { before = true }
@ex.use { control_ok = before }
@ex.try { candidate_ok = before }
@ex.run
assert before, "before_run should have run"
assert control_ok, "control should have run after before_run"
assert candidate_ok, "candidate should have run after before_run"
end
it "does not run when an experiment is disabled" do
before = false
def @ex.enabled?
false
end
@ex.before_run { before = true }
@ex.use { "value" }
@ex.try { "value" }
@ex.run
refute before, "before_run should not have run"
end
end
describe "after run block" do
it "runs when an experiment is enabled" do
control_ok = candidate_ok = false
after_result = nil
@ex.after_run { |result| after_result = result }
@ex.use { control_ok = after_result.nil? }
@ex.try { candidate_ok = after_result.nil? }
@ex.run
assert after_result, "after_run should have run"
assert after_result.matched?, "after_run should be called with the result"
assert control_ok, "control should have run before after_run"
assert candidate_ok, "candidate should have run before after_run"
end
it "does not run when an experiment is disabled" do
after_result = nil
def @ex.enabled?
false
end
@ex.after_run { |result| after_result = result }
@ex.use { "value" }
@ex.try { "value" }
@ex.run
refute after_result, "after_run should not have run"
end
end
describe "testing hooks for extending code" do
it "allows a user to provide fabricated durations for testing purposes (old version)" do
@ex.use { true }
@ex.try { true }
@ex.fabricate_durations_for_testing_purposes( "control" => 0.5, "candidate" => 1.0 )
@ex.run
cont = @ex.published_result.control
cand = @ex.published_result.candidates.first
assert_in_delta 0.5, cont.duration, 0.01
assert_in_delta 1.0, cand.duration, 0.01
end
it "allows a user to provide fabricated durations for testing purposes (new version)" do
@ex.use { true }
@ex.try { true }
@ex.fabricate_durations_for_testing_purposes({
"control" => { "duration" => 0.5, "cpu_time" => 0.4 },
"candidate" => { "duration" => 1.0, "cpu_time" => 0.9 }
})
@ex.run
cont = @ex.published_result.control
cand = @ex.published_result.candidates.first
# Wall Time
assert_in_delta 0.5, cont.duration, 0.01
assert_in_delta 1.0, cand.duration, 0.01
# CPU Time
assert_equal 0.4, cont.cpu_time
assert_equal 0.9, cand.cpu_time
end
it "returns actual durations if fabricated ones are omitted for some blocks (old version)" do
@ex.use { true }
@ex.try { sleep 0.1; true }
@ex.fabricate_durations_for_testing_purposes( "control" => 0.5 )
@ex.run
cont = @ex.published_result.control
cand = @ex.published_result.candidates.first
assert_in_delta 0.5, cont.duration, 0.01
assert_in_delta 0.1, cand.duration, 0.01
end
it "returns actual durations if fabricated ones are omitted for some blocks (new version)" do
@ex.use { true }
@ex.try do
start_time = Time.now
while Time.now - start_time < 0.1
# Perform some CPU-intensive work
(1..1000).each { |i| i * i }
end
true
end
@ex.fabricate_durations_for_testing_purposes({ "control" => { "duration" => 0.5, "cpu_time" => 0.4 }})
@ex.run
cont = @ex.published_result.control
cand = @ex.published_result.candidates.first
# Fabricated durations
assert_in_delta 0.5, cont.duration, 0.01
assert_in_delta 0.4, cont.cpu_time, 0.01
# Measured durations
assert_in_delta 0.1, cand.duration, 0.01
assert_in_delta 0.1, cand.cpu_time, 0.01
end
end
end
|