Files
2012-02-21 01:15:00 -05:00

1 line
2.9 KiB
JSON

[{"user_id": 33202, "stars": [], "topic_id": 37417, "date_created": 1306777225.8850551, "message": "Is there a good way to test race conditions with django's test utilities?\n\nfor example: I have a function that sends mail using a management command. It sets a cache key when it starts and removes it when it's done. If cron fires off the management command before it's done, the newly started process sees the cache key and quits without sending any duplicate emails.\n\nI would like to write tests for this functionality (I tried with processes and threads and neither works very well).\n\nAny ideas?\n\nyou can take a look at my attempt with threads here: https://github.com/jiaaro/django-alert/blob/master/test_project/alert_tests/tests.py#L181", "group_id": 81, "id": 1229359}, {"user_id": 6648, "stars": [], "topic_id": 37417, "date_created": 1306806148.38765, "message": "You could perhaps try using a mocked up email client that has a x second delay and then kick it off several times using something like celery. I believe there is a database-backed queue that might not add too much additional configuration if you're not already using it.", "group_id": 81, "id": 1233554}, {"user_id": 33202, "stars": [], "topic_id": 37417, "date_created": 1306846317.341289, "message": "@poswald does celery work with django's testing utilities? namely unittest 2? The main problem I have with subprocesses is that they don't run in the same environment as the unittests and hence... I can't check the results", "group_id": 81, "id": 1237197}, {"user_id": 2362, "stars": [], "topic_id": 37417, "date_created": 1306849944.4314849, "message": "@Jiaaro Yes, celery works with unittest2. Suggest setting BROKER_BACKEND to 'memory' while testing, though.", "group_id": 81, "id": 1237513}, {"user_id": 7376, "stars": [], "topic_id": 37417, "date_created": 1306869237.5525761, "message": "You could also try using the multiprocessing module instead of threads", "group_id": 81, "id": 1240349}, {"user_id": 33202, "stars": [], "topic_id": 37417, "date_created": 1306924811.81652, "message": "@ericmoritz I tried with both threads and processes - processes seemed to go completely outside of the test runner and instead of sending the mail too many times, the test thought that no emails had been sent at all", "group_id": 81, "id": 1251915}, {"user_id": 33202, "stars": [], "topic_id": 37417, "date_created": 1306925725.3693321, "message": "UPDATE: Hey so... as it turns out the threads mode worked fine... I just needed to put a 1ms pause between the start of each thread to get around the race condition on with the cache. All is well now\n\nold code: \nhttps://github.com/jiaaro/django-alert/blob/03a6b6734fabaca2f0794c84ed83d2dccba015c8/test_project/alert_tests/tests.py#L193\n\nupdated code: \nhttps://github.com/jiaaro/django-alert/blob/665e8f0b58b1026b7589f27b6303cc1a5f018c55/test_project/alert_tests/tests.py#L193", "group_id": 81, "id": 1251943}]