Best Test-prof_ruby code snippet using Monitor.track
job_monitor_spec.rb
Source:job_monitor_spec.rb
...67 end68 end69 describe '#processed' do70 before do71 job_monitor.track_success72 job_monitor.track_success73 job_monitor.track_failure74 end75 it 'sum successes and failures' do76 expect(job_monitor.processed).to eq 377 end78 end79 describe '#status' do80 subject { job_monitor.status }81 context 'without monitorable' do82 it { is_expected.to eq :unknown }83 end84 context 'monitorable is running' do85 before do86 monitorable = spy 'job', running?: true87 allow(job_monitor).to receive(:monitorable).and_return(monitorable)88 end89 it { is_expected.to eq :running }90 end91 context 'monitorable is succeeded' do92 before do93 monitorable = spy 'job', running?: false, succeeded?: true94 allow(job_monitor).to receive(:monitorable).and_return(monitorable)95 end96 it { is_expected.to eq :succeeded }97 end98 context 'monitorable is failed' do99 before do100 monitorable = spy 'job', running?: false, succeeded?: false, failed?: true101 allow(job_monitor).to receive(:monitorable).and_return(monitorable)102 end103 it { is_expected.to eq :failed }104 end105 context 'monitorable is queued' do106 before do107 monitorable = spy 'job', running?: false, succeeded?: false, failed?: false108 allow(job_monitor).to receive(:monitorable).and_return(monitorable)109 end110 it { is_expected.to eq :queued }111 end112 end113 describe '#track_success' do114 it 'increment successes' do115 expect(job_monitor.successes).to eq 0116 job_monitor.track_success117 expect(job_monitor.successes).to eq 1118 end119 it 'increment processed' do120 expect(job_monitor.processed).to eq 0121 job_monitor.track_success122 expect(job_monitor.processed).to eq 1123 end124 end125 describe '#track_failure' do126 it 'increment successes' do127 expect(job_monitor.failures).to eq 0128 job_monitor.track_failure129 expect(job_monitor.failures).to eq 1130 end131 it 'increment processed' do132 expect(job_monitor.processed).to eq 0133 job_monitor.track_failure134 expect(job_monitor.processed).to eq 1135 end136 end137 end138end...
tracker.rb
Source:tracker.rb
...3 extend ActiveSupport::Concern4 class_methods do5 # params:6 # actions(Array): å¨ä½çç±»åï¼åªæ¯æ[:create, :delete, update: []],7 # å¦ track :create, :delete, :update è®°å½æå
¥ï¼å é¤ï¼æ´æ°äºä»¶8 # å¦ track :create, :delete, update: :priceï¼è®°å½æå
¥ï¼å é¤ï¼æå®è¡çæ´æ°äºä»¶9 # å¦ track [:create, :delete, update: [:price, :publish_at]10 # å¦ track [:create, :delete, update: {only: [:publish_at, :price]}11 # å¦ track [:create, :delete, update: {except: :updated_at}12 def track(*actions)13 actions.each do |action|14 if action.is_a?(Symbol) || action.is_a?(String)15 case action.to_s.downcase16 when 'create'17 after_create -> { track_action('create') }18 when 'destroy'19 after_destroy -> { track_action('destroy') }20 when 'update'21 after_update -> { track_action('update') }22 end23 elsif action.is_a?(Hash) && action.keys.map(&:to_s) == ['update']24 columns = get_columns_to_track(action.values.first)25 after_update do26 columns.each do |column|27 track_action_update(column.to_s, try("#{column}_was"), try(column))28 end29 end30 end31 end32 rescue StandardError => e33 puts e.inspect34 end35 def get_columns_to_track(obj)36 columns = column_names.map(&:to_sym)37 columns_to_track = if obj.is_a? Hash38 if obj[:only].present?39 Array(obj[:only])40 elsif obj[:except].present?41 columns - Array(obj[:except])42 else43 []44 end45 else46 Array(obj)47 end48 columns & columns_to_track49 end50 end51 def track_action(action)52 produce action: action53 end54 def track_action_update(column_name, old_value, new_value)55 return nil if new_value == old_value56 meta_data = {57 action: 'update',58 content: column_name,59 status: new_value,60 old_status: old_value61 }62 produce meta_data63 end64 def track_client_id65 return @track_client_id if @track_client_id66 current_user_identfier = ActionMonitor.configuration.current_user_identfier67 @track_client_id = current_user_identfier ? eval(current_user_identfier) : try(:current_user).try(:id)68 end69 def producer70 @track_producer ||= ActionMonitor::Producer.allocate71 end72 def resource_id73 return @track_resource_id if @track_resource_id74 resource_identfier = ActionMonitor.configuration.resource_identfier.try(:to_sym)75 identfier = try(resource_identfier).try(:to_sym) || :id76 @track_resource_id = try(identfier)77 end78 def produce(meta_data)79 data = meta_data.merge(resource_id: resource_id)80 app_name = ActionMonitor.configuration.app_name || Rails.application.class81 producer.output("#{app_name}_#{self.class}", track_client_id, data)82 end83 end84end...
dwp_report_status_job_spec.rb
Source:dwp_report_status_job_spec.rb
...4 let(:app_insight) { instance_double(ApplicationInsights::TelemetryClient, flush: '') }5 let(:mailer) { instance_double(ActionMailer::MessageDelivery, deliver_now: true) }6 before do7 allow(ApplicationInsights::TelemetryClient).to receive(:new).and_return app_insight8 allow(app_insight).to receive(:track_event)9 allow(DwpMonitor).to receive(:new).and_return dwp_monitor10 allow(ApplicationMailer).to receive(:dwp_is_down_notifier).and_return mailer11 described_class.perform_now12 end13 context 'offline' do14 let(:dwp_monitor) { instance_double(DwpMonitor, state: 'offline') }15 it { expect(mailer).to have_received(:deliver_now) }16 it { expect(app_insight).to have_received(:track_event).with("Running DWP status check at #{Time.zone.now.to_fs(:db)}") }17 it { expect(app_insight).to have_received(:track_event).with("Sending DWP status is offline notication at #{Time.zone.now.to_fs(:db)}") }18 end19 context 'online' do20 let(:dwp_monitor) { instance_double(DwpMonitor, state: 'online') }21 it { expect(mailer).not_to have_received(:deliver_now) }22 it { expect(app_insight).to have_received(:track_event).with("Running DWP status check at #{Time.zone.now.to_fs(:db)}") }23 it { expect(app_insight).not_to have_received(:track_event).with("Sending DWP status is offline notication at #{Time.zone.now.to_fs(:db)}") }24 end25 context 'warning' do26 let(:dwp_monitor) { instance_double(DwpMonitor, state: 'warning') }27 it { expect(mailer).not_to have_received(:deliver_now) }28 it { expect(app_insight).to have_received(:track_event).with("Running DWP status check at #{Time.zone.now.to_fs(:db)}") }29 it { expect(app_insight).not_to have_received(:track_event).with("Sending DWP status is offline notication at #{Time.zone.now.to_fs(:db)}") }30 end31 end32end...
track
Using AI Code Generation
1 @data = {}2 def add(key, value)3 def get(key)4sd.add("hello", "world")5puts sd.get("hello")
track
Using AI Code Generation
1 sleep(rand(0.1))2threads.each {|t| t.join}3 @data = {}4 def add(key, value)5 def get(key)6sd.add("hello", "world")7puts sd.get("hello")8 @mon_cond.wait(@mon_mutex) until @mon_owner.nil?
track
Using AI Code Generation
1 m.sleep(5)2m.sleep(2)3 m.sleep(5)4m.sleep(2)5 m.sleep(5)6m.sleep(2)7 m.sleep(5)
track
Using AI Code Generation
1 @data = {}2 def add(key, value)3 def get(key)4sd.add("hello", "world")5puts sd.get("hello")6 @data = {}7 def add(key, value)8 def get(key)9sd.add("hello", "world")10puts sd.get("hello")11 @mon_cond.wait(@mon_mutex) until @mon_owner.nil?
track
Using AI Code Generation
1 m.sleep(5)2m.sleep(2)3 m.sleep(5)4m.sleep(2)5 m.sleep(5)6m.sleep(2)7 m.sleep(5)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!