I am trying to fix a problem when running multiple tests that all login and search for emails in a shared IMAP email server account. Tests use a helper that uses NET::IMAP for login/search/delete and they occasionally fail when logging in or searching for emails. I think this is because of concurrent access of the mailbox by multiple rspec test runs in parallel.
I read about the MonitorMixin in Ruby but from what I understand that is for use by threads spawned within a process rather than between processes. I found this blog post that describes using Redis::Semaphore to implement a distributed lock: https://docs.knapsackpro.com/2017/when-distributed-locks-might-be-helpful-in-ruby-on-rails-application. But that needs Redis.
The tests are for a Ruby Sinatra app with postgres, so I do have postgres to use for shared locking amongst all rspec tests. So I guess I could build a simple database semaphore, like https://udby.com/archives/14 seems to describe.
Is there a simpler way to solve this problem of multiple processes needing to access a shared resource?