MCPcopy
hub / github.com/django/django / test_makedirs_race_handling

Method test_makedirs_race_handling

tests/file_storage/tests.py:437–473  ·  view source on GitHub ↗

File storage should be robust against directory creation race conditions.

(self)

Source from the content-addressed store, hash-verified

435 other_temp_storage.delete(mixed_case)
436
437 def test_makedirs_race_handling(self):
438 """
439 File storage should be robust against directory creation race
440 conditions.
441 """
442 real_makedirs = os.makedirs
443
444 # Monkey-patch os.makedirs, to simulate a normal call, a raced call,
445 # and an error.
446 def fake_makedirs(path, mode=0o777, exist_ok=False):
447 if path == os.path.join(self.temp_dir, "normal"):
448 real_makedirs(path, mode, exist_ok)
449 elif path == os.path.join(self.temp_dir, "raced"):
450 real_makedirs(path, mode, exist_ok)
451 if not exist_ok:
452 raise FileExistsError()
453 elif path == os.path.join(self.temp_dir, "error"):
454 raise PermissionError()
455 else:
456 self.fail("unexpected argument %r" % path)
457
458 try:
459 os.makedirs = fake_makedirs
460
461 self.storage.save("normal/test.file", ContentFile("saved normally"))
462 with self.storage.open("normal/test.file") as f:
463 self.assertEqual(f.read(), b"saved normally")
464
465 self.storage.save("raced/test.file", ContentFile("saved with race"))
466 with self.storage.open("raced/test.file") as f:
467 self.assertEqual(f.read(), b"saved with race")
468
469 # Exceptions aside from FileExistsError are raised.
470 with self.assertRaises(PermissionError):
471 self.storage.save("error/test.file", ContentFile("not saved"))
472 finally:
473 os.makedirs = real_makedirs
474
475 def test_remove_race_handling(self):
476 """

Callers

nothing calls this directly

Calls 4

ContentFileClass · 0.90
saveMethod · 0.45
openMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected