File: test_reporter_public_start_stop.rb

package info (click to toggle)
ruby-memory-profiler 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: ruby: 1,128; makefile: 4
file content (46 lines) | stat: -rw-r--r-- 1,560 bytes parent folder | download
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
# frozen_string_literal: true

require_relative 'test_reporter'

class TestReporterPublicStartStop < TestReporter
  # This class extends TestReporter so it includes all of the tests from
  # TestReporter plus any additional test_* cases below and it
  # overrides create_report to use the start/stop methods

  # This specifically tests the public API of the start and stop methods of the
  # MemoryProfiler module itself, and even does some extra tests exercising
  # edge case handling of `current_reporter` which is done in those methods.
  #
  # When something fails here, and not in the private api tests, then there is
  # something wrong specifically in the methods handling the `current_reporter`
  # that needs to be fixed.
  def create_report(options = {}, &profiled_block)
    profiled_block ||= -> { default_block }
    MemoryProfiler.start(options)
    profiled_block.call rescue nil
    MemoryProfiler.stop
  end

  def test_module_stop_with_no_start
    results = MemoryProfiler.stop
    assert_nil(results)
  end

  def test_module_double_start
    MemoryProfiler.start
    reporter = MemoryProfiler::Reporter.current_reporter

    MemoryProfiler.start
    same_reporter = MemoryProfiler::Reporter.current_reporter
    default_block
    results = MemoryProfiler.stop

    assert_equal(reporter, same_reporter)
    # Some extra here due to variables needed in the test above
    assert_equal(16, results.total_allocated)
  end

  def test_exception_handling
    # This overrides and skips exception handling from the base TestReporter
  end
end