tests: profiler test now creates a new temp file
from hathor.resources import ProfilerResource
from twisted.internet.defer import inlineCallbacks
from tests.resources.base_resource import StubSite, _BaseResourceTest
-from pathlib import Path
+import tempfile
import os
+import shutil
class ProfilerTest(_BaseResourceTest._ResourceTest):
@@ -15,35 +16,33 @@ class ProfilerTest(_BaseResourceTest._ResourceTest):
# Options
yield self.web.options("profiler")
- path = 'profiles/profile001.prof'
-
- dump_file = Path(path)
- if dump_file.is_file():
- dump_file.rename('{}.bkp'.format(path))
-
- self.assertFalse(dump_file.is_file())
+ tmpdir = tempfile.mkdtemp()
+ tmpfile = tempfile.NamedTemporaryFile(dir=tmpdir, suffix='.prof', delete=False)
+ filename = os.path.basename(tmpfile.name)
+ full_path = os.path.join(tmpdir, filename)
# Start profiler
response_start = yield self.web.post("profiler", {'start': True})
data_start = response_start.json_value()
self.assertTrue(data_start['success'])
+ with open(full_path, 'r') as f:
+ # In the start the file must be empty
+ self.assertEqual(len(f.read()), 0)
+
# Stop profiler
- response_stop = yield self.web.post("profiler", {'stop': True})
+ response_stop = yield self.web.post("profiler", {'stop': True, 'filepath': full_path})
data_stop = response_stop.json_value()
self.assertTrue(data_stop['success'])
- # Validate dump file created
- self.assertTrue(dump_file.is_file())
+ with open(full_path, 'rb') as f:
+ # After stop profiler file must have something
+ self.assertGreater(len(f.read()), 0)
# Success false
response_error = yield self.web.post("profiler")
data_error = response_error.json_value()
self.assertFalse(data_error['success'])
- # Removing created file
- os.remove(path)
-
- bkp_file = Path('{}.bkp'.format(path))
- if bkp_file.is_file():
- bkp_file.rename(path)
+ # Removing tmpdir
+ shutil.rmtree(tmpdir)
