Skip to content

Commit dbf59db

Browse files
agattidpgeorge
authored andcommitted
tests/extmod/vfs_blockdev_invalid.py: Handle low memory conditions.
This commit modifies the "extmod/vfs_blockdev_invalid" test to better behave on boards with low available memory. Before these changes the test would fail on ESP8266 (at least), due to low memory, but in a way that could not be easily solved as the error occurred in the middle of the test. The test has been rewritten to delay its output until the very end, so if a low memory condition occurs and needs to stop execution then no real output will show up before the skip marker. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
1 parent 750a366 commit dbf59db

File tree

2 files changed

+41
-42
lines changed

2 files changed

+41
-42
lines changed

tests/extmod/vfs_blockdev_invalid.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ def ioctl(self, op, arg):
4646
try:
4747
bdev = RAMBlockDevice(50)
4848
except MemoryError:
49-
print("SKIP")
49+
print("SKIP-TOO-LARGE")
5050
raise SystemExit
5151

5252

53-
def test(vfs_class):
54-
print(vfs_class)
53+
ERROR_EIO = (OSError, "[Errno 5] EIO")
54+
ERROR_EINVAL = (OSError, "[Errno 22] EINVAL")
55+
ERROR_TYPE = (TypeError, "can't convert str to int")
56+
57+
58+
def test(vfs_class, test_data):
5559
bdev.read_res = 0 # reset function results
5660
bdev.write_res = 0
5761

@@ -61,29 +65,51 @@ def test(vfs_class):
6165
with fs.open("test", "w") as f:
6266
f.write("a" * 64)
6367

64-
for res in (0, -5, 5, 33, "invalid"):
65-
# -5 is a legitimate negative failure (EIO), positive integer
66-
# is not
67-
68+
for res, error_open, error_read in test_data:
6869
# This variant will fail on open
6970
bdev.read_res = res
7071
try:
7172
with fs.open("test", "r") as f:
72-
print("opened")
73+
assert error_open is None
7374
except Exception as e:
74-
print(type(e), e)
75+
assert error_open is not None
76+
assert (type(e), str(e)) == error_open
7577

7678
# This variant should succeed on open, may fail on read
7779
# unless the filesystem cached the contents already
7880
bdev.read_res = 0
7981
try:
8082
with fs.open("test", "r") as f:
8183
bdev.read_res = res
82-
print("read 1", f.read(1))
83-
print("read rest", f.read())
84+
assert f.read(1) == "a"
85+
assert f.read() == "a" * 63
86+
assert error_read is None
8487
except Exception as e:
85-
print(type(e), e)
88+
assert error_read is not None
89+
assert (type(e), str(e)) == error_read
8690

8791

88-
test(vfs.VfsLfs2)
89-
test(vfs.VfsFat)
92+
try:
93+
test(
94+
vfs.VfsLfs2,
95+
(
96+
(0, None, None),
97+
(-5, ERROR_EIO, None),
98+
(5, ERROR_EINVAL, None),
99+
(33, ERROR_EINVAL, None),
100+
("invalid", ERROR_TYPE, None),
101+
),
102+
)
103+
test(
104+
vfs.VfsFat,
105+
(
106+
(0, None, None),
107+
(-5, ERROR_EIO, ERROR_EIO),
108+
(5, ERROR_EIO, ERROR_EIO),
109+
(33, ERROR_EIO, ERROR_EIO),
110+
("invalid", ERROR_TYPE, ERROR_TYPE),
111+
),
112+
)
113+
print("OK")
114+
except MemoryError:
115+
print("SKIP-TOO-LARGE")
Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1 @@
1-
<class 'VfsLfs2'>
2-
opened
3-
read 1 a
4-
read rest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
5-
<class 'OSError'> [Errno 5] EIO
6-
read 1 a
7-
read rest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
8-
<class 'OSError'> [Errno 22] EINVAL
9-
read 1 a
10-
read rest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
11-
<class 'OSError'> [Errno 22] EINVAL
12-
read 1 a
13-
read rest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
14-
<class 'TypeError'> can't convert str to int
15-
read 1 a
16-
read rest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
17-
<class 'VfsFat'>
18-
opened
19-
read 1 a
20-
read rest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
21-
<class 'OSError'> [Errno 5] EIO
22-
<class 'OSError'> [Errno 5] EIO
23-
<class 'OSError'> [Errno 5] EIO
24-
<class 'OSError'> [Errno 5] EIO
25-
<class 'OSError'> [Errno 5] EIO
26-
<class 'OSError'> [Errno 5] EIO
27-
<class 'TypeError'> can't convert str to int
28-
<class 'TypeError'> can't convert str to int
1+
OK

0 commit comments

Comments
 (0)