Debian Codegraph @

t/unit/backends/test_filesystem.py

raw
1 import os
2 import pickle
3 import sys
4 import tempfile
5 import time
6 from unittest.mock import patch
7
8 import pytest
9
10 import t.skip
11 from celery import states, uuid
12 from celery.backends import filesystem
13 from celery.backends.filesystem import FilesystemBackend
14 from celery.exceptions import ImproperlyConfigured
15
16
17 @t.skip.if_win32
18 class test_FilesystemBackend:
19
20 def setup_method(self):
21 self.directory = tempfile.mkdtemp()
22 self.url = 'file://' + self.directory
23 self.path = self.directory.encode('ascii')
24
25 def test_a_path_is_required(self):
26 with pytest.raises(ImproperlyConfigured):
27 FilesystemBackend(app=self.app)
28
29 def test_a_path_in_url(self):
30 tb = FilesystemBackend(app=self.app, url=self.url)
31 assert tb.path == self.path
32
33 @pytest.mark.parametrize("url,expected_error_message", [
34 ('file:///non-existing', filesystem.E_PATH_INVALID),
35 ('url://non-conforming', filesystem.E_PATH_NON_CONFORMING_SCHEME),
36 (None, filesystem.E_NO_PATH_SET)
37 ])
38 def test_raises_meaningful_errors_for_invalid_urls(
39 self,
40 url,
41 expected_error_message
42 ):
43 with pytest.raises(
44 ImproperlyConfigured,
45 match=expected_error_message
46 ):
47 FilesystemBackend(app=self.app, url=url)
48
49 def test_localhost_is_removed_from_url(self):
50 url = 'file://localhost' + self.directory
51 tb = FilesystemBackend(app=self.app, url=url)
52 assert tb.path == self.path
53
54 def test_missing_task_is_PENDING(self):
55 tb = FilesystemBackend(app=self.app, url=self.url)
56 assert tb.get_state('xxx-does-not-exist') == states.PENDING
57
58 def test_mark_as_done_writes_file(self):
59 tb = FilesystemBackend(app=self.app, url=self.url)
60 tb.mark_as_done(uuid(), 42)
61 assert len(os.listdir(self.directory)) == 1
62
63 def test_done_task_is_SUCCESS(self):
64 tb = FilesystemBackend(app=self.app, url=self.url)
65 tid = uuid()
66 tb.mark_as_done(tid, 42)
67 assert tb.get_state(tid) == states.SUCCESS
68
69 def test_correct_result(self):
70 data = {'foo': 'bar'}
71
72 tb = FilesystemBackend(app=self.app, url=self.url)
73 tid = uuid()
74 tb.mark_as_done(tid, data)
75 assert tb.get_result(tid) == data
76
77 def test_get_many(self):
78 data = {uuid(): 'foo', uuid(): 'bar', uuid(): 'baz'}
79
80 tb = FilesystemBackend(app=self.app, url=self.url)
81 for key, value in data.items():
82 tb.mark_as_done(key, value)
83
84 for key, result in tb.get_many(data.keys()):
85 assert result['result'] == data[key]
86
87 def test_forget_deletes_file(self):
88 tb = FilesystemBackend(app=self.app, url=self.url)
89 tid = uuid()
90 tb.mark_as_done(tid, 42)
91 tb.forget(tid)
92 assert len(os.listdir(self.directory)) == 0
93
94 @pytest.mark.usefixtures('depends_on_current_app')
95 def test_pickleable(self):
96 tb = FilesystemBackend(app=self.app, url=self.url, serializer='pickle')
97 assert pickle.loads(pickle.dumps(tb))
98
99 @pytest.mark.skipif(sys.platform == 'win32', reason='Test can fail on '
100 'Windows/FAT due to low granularity of st_mtime')
101 def test_cleanup(self):
102 tb = FilesystemBackend(app=self.app, url=self.url)
103 yesterday_task_ids = [uuid() for i in range(10)]
104 today_task_ids = [uuid() for i in range(10)]
105 for tid in yesterday_task_ids:
106 tb.mark_as_done(tid, 42)
107 day_length = 0.2
108 time.sleep(day_length) # let FS mark some difference in mtimes
109 for tid in today_task_ids:
110 tb.mark_as_done(tid, 42)
111 with patch.object(tb, 'expires', 0):
112 tb.cleanup()
113 # test that zero expiration time prevents any cleanup
114 filenames = set(os.listdir(tb.path))
115 assert all(
116 tb.get_key_for_task(tid) in filenames
117 for tid in yesterday_task_ids + today_task_ids
118 )
119 # test that non-zero expiration time enables cleanup by file mtime
120 with patch.object(tb, 'expires', day_length):
121 tb.cleanup()
122 filenames = set(os.listdir(tb.path))
123 assert not any(
124 tb.get_key_for_task(tid) in filenames
125 for tid in yesterday_task_ids
126 )
127 assert all(
128 tb.get_key_for_task(tid) in filenames
129 for tid in today_task_ids
130 )