peterbe / django-static

Template tags for better serving static files from templates in Django
BSD 2-Clause "Simplified" License
193 stars 28 forks source link

Tests occasionally fail due to differing times #9

Closed slinkp closed 13 years ago

slinkp commented 13 years ago

The test function _test_staticfile_single has a faulty assumption that calling int(time.time()) shortly after calling the template tag will get the same timestamp that was used in the template tag. AFAICT The template tag gets its timestamp from the mod time of the file it creates. These two timestamps can differ by 1 if the test is run very close to the end of a second. This happens fairly often (on the order of once every 10 runs on my laptop).

This can be trivially improved somewhat by moving the "now = int(time.time())" line to just before the call to template.render(). However, it still fails frequently - maybe once every 40 runs on my system.

Here's a patch that seems to fix all the failures, although I'm not sure I like the approach. An alternative might be to monkeypatch os.stat just during the test run, to force the generated filenames to be predictable.

Anyway, patch is at https://gist.github.com/720549

peterbe commented 13 years ago

Thanks for the patch. I'll apply it and see if there's anything else I can do to prevent unpredictable error failures.

peterbe commented 13 years ago

Alright!! https://github.com/peterbe/django-static/commit/8e7fe8643dc8ddb32a3369f496229b49b620a758 Thanks for the patch I wasn't able to apply it with patch so I manually copy and pasted the relevant changes. It still fails sometimes but now much more rare.