lundi 22 mai 2017

Increase Rails testing time precision

We're noticing over time that as our machines get more performant, we end up with more Rails specs with timing issues where it appears that timestamps are not distinct. For example:

event1 = create(:event)
event2 = create(:event)
expect(Event.order(:created_at).last).to eq event2

The test above has historically been completely reliable, but more recently become flaky apparently due to our computers being more powerful. Sometimes it's event1, sometimes it's event2. My best guess as to what's happening here is that they end up created so quickly one after another that the timestamps are not distinct. I'm wondering if there is a way to increase the precision of the timestamps in our tests to work around this, or do I just have to manually specify timestamps everywhere? E.g., what we've started doing is something like:

event1 = create(:event, created_at: 1.minute.ago)
event2 = create(:event)
expect(Event.order(:created_at).last).to eq event2

I'm also wondering if it has anything to do with database timestamp precision, or is it just in Rails land?

Aucun commentaire:

Enregistrer un commentaire