Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the write_to_file error with multi times write #5

Merged
merged 1 commit into from May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion bplustree/memory.py
Expand Up @@ -54,7 +54,7 @@ def write_to_file(file_fd: io.FileIO, dir_fileno: Optional[int],
length_to_write = len(data)
written = 0
while written < length_to_write:
written = file_fd.write(data[written:])
written += file_fd.write(data[written:])
if fsync:
fsync_file_and_dir(file_fd.fileno(), dir_fileno)

Expand Down
17 changes: 16 additions & 1 deletion tests/test_memory.py
Expand Up @@ -7,7 +7,7 @@

from bplustree.node import LeafNode
from bplustree.memory import (
FileMemory, open_file_in_dir, WAL, ReachedEndOfFile
FileMemory, open_file_in_dir, WAL, ReachedEndOfFile, write_to_file
)
from bplustree.const import TreeConf
from .conftest import filename
Expand Down Expand Up @@ -61,6 +61,21 @@ def test_open_file_in_dir():
os.close(dir_fd)


def test_write_to_file_multi_times():
def side_effect(*args, **kwargs):
if len(args) == 1:
data = args[0]
if len(data) > 5:
return 5
else:
return len(data)

mock_fd = mock.MagicMock()
mock_fd.write.side_effect = side_effect

write_to_file(mock_fd, None, b'abcdefg')


@mock.patch('bplustree.memory.platform.system', return_value='Windows')
def test_open_file_in_dir_windows(_):
file_fd, dir_fd = open_file_in_dir(filename)
Expand Down