简体   繁体   中英

Testing Redis transaction with RSpec

I am using a WATCH/MULTI/EXEC command sequence to complete a transaction in a Rails app. I'd like to spec out the behavior of the app both when the transaction succeeds and the transaction fails (specifically when the watched key is modified in another session during the transaction).

Concretely speaking, given the (contrived) example below, how might I write a spec that always modifies testval in Redis while the arbitrary computation is being performed?

def test_watch
  REDIS.watch("testval") do
    val = REDIS.get('testval')
    val += 1000 # arbitrary computation
    REDIS.multi do |m|
      REDIS.set('testval', val)
    end
  end
end

You actually don't need to modify your key 'testval' in another session, you can do it in the same session and your transaction will still fail. So you just need to find a way to inject a REDIS.set('testval', some_value) between the watch and multi and your transaction will fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM