Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5bada62

Browse files
authoredJan 7, 2025··
Fix slurm_format_bytes_ceil logic and add unit tests (#675)
* Fix slurm_format_bytes_ceil logic, add unit tests * Set default return value to 1K
1 parent bf20ab1 commit 5bada62

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed
 

‎dask_jobqueue/slurm.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,26 @@ def slurm_format_bytes_ceil(n):
116116
"""Format bytes as text.
117117
118118
SLURM expects KiB, MiB or Gib, but names it KB, MB, GB. SLURM does not handle Bytes, only starts at KB.
119+
Thus minimum returned value is '1K'.
119120
120-
>>> slurm_format_bytes_ceil(1)
121+
Parameters
122+
----------
123+
n: int
124+
Number of bytes
125+
126+
Returns
127+
-------
128+
str
129+
Bytes formatted as string
130+
131+
Examples
132+
--------
133+
>>> slurm_format_bytes_ceil(12)
121134
'1K'
122135
>>> slurm_format_bytes_ceil(1234)
123136
'2K'
124137
>>> slurm_format_bytes_ceil(12345678)
125-
'13M'
138+
'12M'
126139
>>> slurm_format_bytes_ceil(1234567890)
127140
'2G'
128141
>>> slurm_format_bytes_ceil(15000000000)
@@ -134,7 +147,7 @@ def slurm_format_bytes_ceil(n):
134147
return "%dM" % math.ceil(n / (1024**2))
135148
if n >= 1024:
136149
return "%dK" % math.ceil(n / 1024)
137-
return "1K" % n
150+
return "1K"
138151

139152

140153
class SLURMCluster(JobQueueCluster):

‎dask_jobqueue/tests/test_slurm.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from dask.utils import format_bytes, parse_bytes
1111

1212
from dask_jobqueue import SLURMCluster
13+
from dask_jobqueue.slurm import slurm_format_bytes_ceil
1314

1415
from . import QUEUE_WAIT
1516

@@ -343,3 +344,18 @@ def test_slurm_runner():
343344
)
344345
output = output.decode()
345346
assert "Test passed" in output
347+
348+
349+
@pytest.mark.parametrize(
350+
["input", "expected_output"],
351+
[
352+
(12, "1K"),
353+
(1234, "2K"),
354+
(12345678, "12M"),
355+
(15000000000, "14G"),
356+
],
357+
)
358+
def test_slurm_format_bytes_ceil(input, expected_output):
359+
actual_output = slurm_format_bytes_ceil(input)
360+
361+
assert actual_output == expected_output

0 commit comments

Comments
 (0)
Please sign in to comment.