Class: KnapsackPro::Tracker

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/knapsack_pro/tracker.rb

Constant Summary collapse

DEFAULT_TEST_FILE_TIME =

when test file is pending, empty with no tests or has syntax error then assume time execution to better allocate it in Queue Mode for future CI build runs

0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTracker

Returns a new instance of Tracker.



12
13
14
15
16
17
# File 'lib/knapsack_pro/tracker.rb', line 12

def initialize
  @global_time_since_beginning = 0
  KnapsackPro::Config::TempFiles.ensure_temp_directory_exists!
  FileUtils.mkdir_p(tracker_dir_path)
  set_defaults
end

Instance Attribute Details

#current_test_pathObject



48
49
50
51
52
# File 'lib/knapsack_pro/tracker.rb', line 48

def current_test_path
  return unless @current_test_path

  KnapsackPro::TestFileCleaner.clean(@current_test_path)
end

#global_timeObject (readonly)

seconds



9
10
11
# File 'lib/knapsack_pro/tracker.rb', line 9

def global_time
  @global_time
end

#global_time_since_beginningObject (readonly)

seconds



9
10
11
# File 'lib/knapsack_pro/tracker.rb', line 9

def global_time_since_beginning
  @global_time_since_beginning
end

#prerun_tests_loadedObject (readonly)

seconds



9
10
11
# File 'lib/knapsack_pro/tracker.rb', line 9

def prerun_tests_loaded
  @prerun_tests_loaded
end

#test_files_with_timeObject (readonly)

seconds



9
10
11
# File 'lib/knapsack_pro/tracker.rb', line 9

def test_files_with_time
  @test_files_with_time
end

Instance Method Details

#reset!Object



19
20
21
22
23
24
25
26
# File 'lib/knapsack_pro/tracker.rb', line 19

def reset!
  set_defaults

  # Remove report only when the reset! method is called explicitly.
  # The report should be persisted on the disk so that multiple tracker instances can share the report state.
  # Tracker instance can be created by knapsack_pro process and a separate tracker is created by rake task (e.g., RSpec) in Regular Mode.
  File.delete(prerun_tests_report_path) if File.exist?(prerun_tests_report_path)
end

#reset_timerObject



32
33
34
# File 'lib/knapsack_pro/tracker.rb', line 32

def reset_timer
  @start_time = now_without_mock_time.to_f
end

#set_prerun_tests(test_file_paths) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/knapsack_pro/tracker.rb', line 54

def set_prerun_tests(test_file_paths)
  test_file_paths.each do |test_file_path|
    # Set a default time for test file
    # in case when the test file will not be run
    # due syntax error or being pending.
    # The time is required by Knapsack Pro API.
    @test_files_with_time[test_file_path] = {
      time_execution: DEFAULT_TEST_FILE_TIME,
      measured_time: false,
    }
  end

  save_prerun_tests_report(@test_files_with_time)

  @prerun_tests_loaded = true
end

#start_timerObject



28
29
30
# File 'lib/knapsack_pro/tracker.rb', line 28

def start_timer
  @start_time ||= now_without_mock_time.to_f
end

#stop_timerObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/knapsack_pro/tracker.rb', line 36

def stop_timer
  execution_time = @start_time ? now_without_mock_time.to_f - @start_time : 0.0

  if @current_test_path
    update_global_time(execution_time)
    update_test_file_time(execution_time)
    reset_timer
  end

  execution_time
end

#to_aObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/knapsack_pro/tracker.rb', line 71

def to_a
  # When the test files are not loaded in the memory then load them from the disk.
  # Useful for the Regular Mode when the memory is not shared between tracker instances.
  # Tracker instance can be created by knapsack_pro process and a separate tracker is created by rake task (e.g., RSpec)
  load_prerun_tests unless prerun_tests_loaded

  test_files = []
  @test_files_with_time.each do |path, hash|
    test_files << {
      path: path,
      time_execution: hash[:time_execution]
    }
  end
  test_files
end