Skip to content

Commit

Permalink
Try to stablize pyfs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgross committed Feb 28, 2024
1 parent 48d13c9 commit 48bb980
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/pcloud/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _do_request(self, method, authenticate=True, json=True, endpoint=None, **kw)
params.update(kw)
log.debug("Doing request to %s%s", endpoint, method)
log.debug("Params: %s", params)
resp = self.session.get(endpoint + method, params=params, stream=False)
resp = self.session.get(endpoint + method, params=params)
if json:
result = resp.json()
else:
Expand Down Expand Up @@ -242,7 +242,7 @@ def _upload(self, method, files, **kwargs):
fields = list(kwargs.items())
fields.extend(files)
m = MultipartEncoder(fields=fields)
resp = self.session.post(
resp = requests.post(
self.endpoint + method, data=m, headers={"Content-Type": m.content_type}
)
return resp.json()
Expand Down
15 changes: 13 additions & 2 deletions src/pcloud/pcloudfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,14 @@ def on_close(pcloudfile):
if _mode.appending:
resp = self.pcloud.file_open(path=_path, flags=flags)
fd = resp.get("fd")
if fd is None:
# try a second time, if file could not be opened
resp = self.pcloud.file_open(path=_path, flags=api.O_WRITE)
fd = resp.get("fd")
if fd is not None:
data = self.pcloud.file_read(fd=fd, count=info.size)
if resp.get('result') != 0:
api.log.error(f'Error reading file {_path} failed with {resp}')
pcloud_file.seek(0, os.SEEK_END)
pcloud_file.raw.write(data)
resp = self.pcloud.file_close(fd=fd)
Expand All @@ -344,9 +350,14 @@ def on_close(pcloudfile):
resp = self.pcloud.file_open(path=_path, flags=api.O_WRITE)
fd = resp.get("fd")
if fd is None:
api.log.error(f'Error opening file {_path} failed with {resp}')
# try a second time, if file could not be opened
resp = self.pcloud.file_open(path=_path, flags=api.O_WRITE)
fd = resp.get("fd")
if fd is None:
api.log.error(f'Error opening file {_path} failed with {resp}')
else:
pcloud_file.raw.write(self.pcloud.file_read(fd=fd, count=info.size))
data = self.pcloud.file_read(fd=fd, count=info.size)
pcloud_file.raw.write(data)
resp = self.pcloud.file_close(fd=fd)
if resp.get('result') != 0:
api.log.error(f'Error closing file {_path} failed with {resp}')
Expand Down
5 changes: 5 additions & 0 deletions src/pcloud/tests/test_pyfs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import time
import unittest
import uuid

Expand Down Expand Up @@ -38,3 +39,7 @@ def tearDown(self):
self.pcloudfs.removetree(self.testdir)
except ResourceNotFound: # pragma: no coverage
pass
# The pCloud API tends to get unstable under load
# Put some latency in the tests with this hack
# to stabilize tests
time.sleep(3)

0 comments on commit 48bb980

Please sign in to comment.