Refactor race condition reblog service spec (#24526)
This commit is contained in:
		
							parent
							
								
									ab79bac584
								
							
						
					
					
						commit
						1c31bdc9fc
					
				
					 1 changed files with 17 additions and 5 deletions
				
			
		| 
						 | 
				
			
			@ -35,13 +35,25 @@ RSpec.describe ReblogService, type: :service do
 | 
			
		|||
  end
 | 
			
		||||
 | 
			
		||||
  context 'when the reblogged status is discarded in the meantime' do
 | 
			
		||||
    let(:status) { Fabricate(:status, account: alice, visibility: :public) }
 | 
			
		||||
    let(:status) { Fabricate(:status, account: alice, visibility: :public, text: 'discard-status-text') }
 | 
			
		||||
 | 
			
		||||
    # Add a callback to discard the status being reblogged after the
 | 
			
		||||
    # validations pass but before the database commit is executed.
 | 
			
		||||
    before do
 | 
			
		||||
      # Update the in-database attribute without reflecting the change in
 | 
			
		||||
      # the object. This cannot simulate all race conditions, but it is
 | 
			
		||||
      # pretty close.
 | 
			
		||||
      Status.where(id: status.id).update_all(deleted_at: Time.now.utc) # rubocop:disable Rails/SkipsModelValidations
 | 
			
		||||
      Status.class_eval do
 | 
			
		||||
        before_save :discard_status
 | 
			
		||||
        def discard_status
 | 
			
		||||
          Status
 | 
			
		||||
            .where(id: reblog_of_id)
 | 
			
		||||
            .where(text: 'discard-status-text')
 | 
			
		||||
            .update_all(deleted_at: Time.now.utc) # rubocop:disable Rails/SkipsModelValidations
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    # Remove race condition simulating `discard_status` callback.
 | 
			
		||||
    after do
 | 
			
		||||
      Status._save_callbacks.delete(:discard_status)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it 'raises an exception' do
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue