Skip to content

Commit 2c399c5

Browse files
committed
format
1 parent ff88bcd commit 2c399c5

File tree

5 files changed

+155
-80
lines changed

5 files changed

+155
-80
lines changed

tests/local_cache/test_loadbalance.py

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
import pytest
2-
from unittest.mock import patch
31
import socket
2+
from unittest.mock import patch
3+
4+
import pytest
45

5-
from alluxiofs.client.loadbalance import (
6-
WorkerNetAddress,
7-
WorkerListLoadBalancer,
8-
DNSLoadBalancer,
9-
)
106
from alluxiofs.client.config import AlluxioClientConfig
11-
from alluxiofs.client.const import ALLUXIO_WORKER_HTTP_SERVER_PORT_DEFAULT_VALUE
7+
from alluxiofs.client.const import (
8+
ALLUXIO_WORKER_HTTP_SERVER_PORT_DEFAULT_VALUE,
9+
)
10+
from alluxiofs.client.loadbalance import DNSLoadBalancer
11+
from alluxiofs.client.loadbalance import WorkerListLoadBalancer
12+
from alluxiofs.client.loadbalance import WorkerNetAddress
13+
1214

1315
class TestWorkerNetAddress:
1416
def test_init_defaults(self):
1517
addr = WorkerNetAddress()
1618
assert addr.host == "localhost"
17-
assert addr.http_server_port == ALLUXIO_WORKER_HTTP_SERVER_PORT_DEFAULT_VALUE
19+
assert (
20+
addr.http_server_port
21+
== ALLUXIO_WORKER_HTTP_SERVER_PORT_DEFAULT_VALUE
22+
)
1823

1924
def test_from_host_and_port(self):
2025
addr = WorkerNetAddress.from_host_and_port("worker1", 12345)
@@ -24,7 +29,10 @@ def test_from_host_and_port(self):
2429
def test_from_host(self):
2530
addr = WorkerNetAddress.from_host("worker1")
2631
assert addr.host == "worker1"
27-
assert addr.http_server_port == ALLUXIO_WORKER_HTTP_SERVER_PORT_DEFAULT_VALUE
32+
assert (
33+
addr.http_server_port
34+
== ALLUXIO_WORKER_HTTP_SERVER_PORT_DEFAULT_VALUE
35+
)
2836

2937
def test_from_dict(self):
3038
d = {"host": "worker1", "http_server_port": 12345}
@@ -37,34 +45,45 @@ def test_to_dict(self):
3745
d = WorkerNetAddress.to_dict(addr)
3846
assert d == {"host": "worker1", "http_server_port": 12345}
3947

48+
4049
class TestWorkerListLoadBalancer:
4150
def test_init_hosts_only(self):
42-
config = AlluxioClientConfig(worker_hosts="worker1,worker2", worker_http_port=29999)
51+
config = AlluxioClientConfig(
52+
worker_hosts="worker1,worker2", worker_http_port=29999
53+
)
4354
lb = WorkerListLoadBalancer(config)
4455
assert lb.workers == ["worker1", "worker2"]
4556
assert lb.ports == [29999, 29999]
4657

4758
def test_init_hosts_with_ports(self):
48-
config = AlluxioClientConfig(worker_hosts="worker1:10001,worker2:10002")
59+
config = AlluxioClientConfig(
60+
worker_hosts="worker1:10001,worker2:10002"
61+
)
4962
lb = WorkerListLoadBalancer(config)
5063
assert lb.workers == ["worker1", "worker2"]
5164
assert lb.ports == [10001, 10002]
5265

5366
def test_init_mixed(self):
54-
config = AlluxioClientConfig(worker_hosts="worker1,worker2:10002", worker_http_port=29999)
67+
config = AlluxioClientConfig(
68+
worker_hosts="worker1,worker2:10002", worker_http_port=29999
69+
)
5570
lb = WorkerListLoadBalancer(config)
5671
assert lb.workers == ["worker1", "worker2"]
5772
assert lb.ports == [29999, 10002]
5873

5974
def test_get_worker_random(self):
60-
config = AlluxioClientConfig(worker_hosts="worker1", worker_http_port=29999)
75+
config = AlluxioClientConfig(
76+
worker_hosts="worker1", worker_http_port=29999
77+
)
6178
lb = WorkerListLoadBalancer(config)
6279
worker = lb.get_worker()
6380
assert worker.host == "worker1"
6481
assert worker.http_server_port == 29999
6582

6683
def test_get_worker_hash(self):
67-
config = AlluxioClientConfig(worker_hosts="worker1,worker2", worker_http_port=29999)
84+
config = AlluxioClientConfig(
85+
worker_hosts="worker1,worker2", worker_http_port=29999
86+
)
6887
lb = WorkerListLoadBalancer(config)
6988
path = "test_path"
7089
worker1 = lb.get_worker(path)
@@ -73,25 +92,30 @@ def test_get_worker_hash(self):
7392
assert worker1.host in ["worker1", "worker2"]
7493

7594
def test_get_multiple_worker(self):
76-
config = AlluxioClientConfig(worker_hosts="worker1,worker2,worker3", worker_http_port=29999)
95+
config = AlluxioClientConfig(
96+
worker_hosts="worker1,worker2,worker3", worker_http_port=29999
97+
)
7798
lb = WorkerListLoadBalancer(config)
7899
workers = lb.get_multiple_worker("test_path", 2)
79100
assert len(workers) == 2
80101
assert workers[0] != workers[1]
81102
assert workers[0].host in ["worker1", "worker2", "worker3"]
82103
assert workers[1].host in ["worker1", "worker2", "worker3"]
83104

105+
84106
class TestDNSLoadBalancer:
85107
@patch("socket.getaddrinfo")
86108
def test_resolve_address_success(self, mock_getaddrinfo):
87109
mock_getaddrinfo.return_value = [
88-
(socket.AF_INET, socket.SOCK_STREAM, 6, '', ('192.168.1.1', 80))
110+
(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("192.168.1.1", 80))
89111
]
90112
config = AlluxioClientConfig(load_balance_domain="example.com")
91113
lb = DNSLoadBalancer(config)
92114
ip = lb.resolve_address()
93115
assert ip == "192.168.1.1"
94-
mock_getaddrinfo.assert_called_with("example.com", None, family=socket.AF_INET)
116+
mock_getaddrinfo.assert_called_with(
117+
"example.com", None, family=socket.AF_INET
118+
)
95119

96120
@patch("socket.getaddrinfo")
97121
def test_resolve_address_no_address(self, mock_getaddrinfo):
@@ -112,7 +136,9 @@ def test_resolve_address_gaierror(self, mock_getaddrinfo):
112136
@patch("alluxiofs.client.loadbalance.DNSLoadBalancer.resolve_address")
113137
def test_get_worker(self, mock_resolve):
114138
mock_resolve.return_value = "192.168.1.1"
115-
config = AlluxioClientConfig(load_balance_domain="example.com", worker_http_port=29999)
139+
config = AlluxioClientConfig(
140+
load_balance_domain="example.com", worker_http_port=29999
141+
)
116142
lb = DNSLoadBalancer(config)
117143
worker = lb.get_worker("path")
118144
assert worker.host == "192.168.1.1"
@@ -121,7 +147,9 @@ def test_get_worker(self, mock_resolve):
121147
@patch("alluxiofs.client.loadbalance.DNSLoadBalancer.resolve_address")
122148
def test_get_worker_no_args(self, mock_resolve):
123149
mock_resolve.return_value = "192.168.1.1"
124-
config = AlluxioClientConfig(load_balance_domain="example.com", worker_http_port=29999)
150+
config = AlluxioClientConfig(
151+
load_balance_domain="example.com", worker_http_port=29999
152+
)
125153
lb = DNSLoadBalancer(config)
126154
worker = lb.get_worker()
127155
assert worker.host == "192.168.1.1"
@@ -130,7 +158,9 @@ def test_get_worker_no_args(self, mock_resolve):
130158
@patch("alluxiofs.client.loadbalance.DNSLoadBalancer.resolve_address")
131159
def test_get_multiple_worker(self, mock_resolve):
132160
mock_resolve.side_effect = ["192.168.1.1", "192.168.1.2"]
133-
config = AlluxioClientConfig(load_balance_domain="example.com", worker_http_port=29999)
161+
config = AlluxioClientConfig(
162+
load_balance_domain="example.com", worker_http_port=29999
163+
)
134164
lb = DNSLoadBalancer(config)
135165
workers = lb.get_multiple_worker("path", 2)
136166
assert len(workers) == 2

tests/local_cache/test_local_cache.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def test_atomic_write_success(self, mock_fetch):
7070
file_path = "/test/file"
7171
part_index = 0
7272
path_hash = self.manager.compute_hash(file_path)
73+
assert path_hash is not None
7374
local_path = self.manager._get_local_path(file_path, part_index)
7475

7576
# Mock fetch to write some data
@@ -225,7 +226,7 @@ def test_set_file_loading(self):
225226
os.remove(local_path + "_loading")
226227

227228
# Fail (already cached)
228-
with open(local_path, "w") as f:
229+
with open(local_path, "w"):
229230
pass
230231
self.assertFalse(self.manager.set_file_loading(file_path, part_index))
231232

@@ -292,6 +293,7 @@ def test_read_file_range_miss_triggers_download(self, mock_download):
292293
# We need to mock manager.read_from_cache because it's called inside read_file_range
293294

294295
original_read = self.manager.read_from_cache
296+
assert original_read is not None
295297

296298
call_count = 0
297299

tests/local_cache/test_prefetch_policy.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from unittest.mock import MagicMock
2-
from alluxiofs.client.prefetch_policy import (
3-
NoPrefetchPolicy,
4-
FixedWindowPrefetchPolicy,
5-
AdaptiveWindowPrefetchPolicy,
6-
)
2+
3+
from alluxiofs.client.prefetch_policy import AdaptiveWindowPrefetchPolicy
4+
from alluxiofs.client.prefetch_policy import FixedWindowPrefetchPolicy
5+
from alluxiofs.client.prefetch_policy import NoPrefetchPolicy
6+
77

88
class TestNoPrefetchPolicy:
99
def test_get_blocks(self):
@@ -23,6 +23,7 @@ def test_get_blocks_full_file(self):
2323
# file_size=5000 -> blocks 0, 1, 2, 3, 4. (4*1024=4096, 5000 is in block 4)
2424
assert policy.get_blocks(0, -1, 5000) == (0, 4)
2525

26+
2627
class TestFixedWindowPrefetchPolicy:
2728
def test_init_defaults(self):
2829
policy = FixedWindowPrefetchPolicy(block_size=1024)
@@ -67,6 +68,7 @@ def test_get_blocks_boundary(self):
6768
# Should return (0, 4)
6869
assert policy.get_blocks(0, 1024, 5000) == (0, 4)
6970

71+
7072
class TestAdaptiveWindowPrefetchPolicy:
7173
def test_init(self):
7274
config = MagicMock()
@@ -122,7 +124,7 @@ def test_random_access_resets_window(self):
122124

123125
policy = AdaptiveWindowPrefetchPolicy(block_size=1024, config=config)
124126
policy.prefetch_ahead = 5
125-
policy.last_offset = 10240 # block 10
127+
policy.last_offset = 10240 # block 10
126128

127129
# Jump back to block 0
128130
# index=0, last_index=10. index < last_index.
@@ -139,11 +141,10 @@ def test_jump_forward_resets_window(self):
139141

140142
policy = AdaptiveWindowPrefetchPolicy(block_size=1024, config=config)
141143
policy.prefetch_ahead = 2
142-
policy.last_offset = 0 # block 0
144+
policy.last_offset = 0 # block 0
143145

144146
# Jump far forward to block 10
145147
# index=10, last_index=0. index-last_index-1 = 9 > prefetch_ahead(2).
146148
# else branch: prefetch_ahead = min_prefetch (0).
147149
policy.get_blocks(10240, 1024, 100000)
148150
assert policy.prefetch_ahead == 0
149-

tests/local_cache/test_transfer.py

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import pytest
21
import json
3-
from unittest.mock import MagicMock, patch
2+
from unittest.mock import MagicMock
3+
from unittest.mock import patch
4+
5+
import pytest
46

5-
from alluxiofs.client.transfer import UfsInfo, UFSUpdater
67
from alluxiofs.client.const import ALLUXIO_UFS_INFO_REFRESH_INTERVAL_MINUTES
8+
from alluxiofs.client.transfer import UfsInfo
9+
from alluxiofs.client.transfer import UFSUpdater
10+
711

812
class TestUfsInfo:
913
def test_init(self):
@@ -12,6 +16,7 @@ def test_init(self):
1216
assert info.ufs_full_path == "ufs_path"
1317
assert info.options == {"opt": "val"}
1418

19+
1520
class TestUFSUpdater:
1621
@pytest.fixture
1722
def mock_alluxio(self):
@@ -31,17 +36,17 @@ def test_init_with_alluxio(self, mock_alluxio):
3136

3237
def test_init_without_alluxio(self):
3338
updater = UFSUpdater(None)
34-
assert updater.interval_seconds == ALLUXIO_UFS_INFO_REFRESH_INTERVAL_MINUTES * 60
39+
assert (
40+
updater.interval_seconds
41+
== ALLUXIO_UFS_INFO_REFRESH_INTERVAL_MINUTES * 60
42+
)
3543
assert updater.alluxio is None
3644

3745
def test_parse_ufs_info_valid(self, mock_alluxio):
3846
updater = UFSUpdater(mock_alluxio)
39-
json_str = json.dumps({
40-
"s3://bucket/path": {
41-
"alluxio_path": "/mnt/s3",
42-
"key": "value"
43-
}
44-
})
47+
json_str = json.dumps(
48+
{"s3://bucket/path": {"alluxio_path": "/mnt/s3", "key": "value"}}
49+
)
4550
result = updater.parse_ufs_info(json_str)
4651
assert len(result) == 1
4752
assert result[0].ufs_full_path == "s3://bucket/path"
@@ -73,12 +78,19 @@ def test_parse_ufs_info_value_not_dict(self, mock_alluxio):
7378
@patch("alluxiofs.client.transfer.register_unregistered_ufs_to_fsspec")
7479
@patch("alluxiofs.client.transfer.filesystem")
7580
@patch("alluxiofs.client.transfer.fsspec.get_filesystem_class")
76-
def test_register_ufs_fallback(self, mock_get_fs_class, mock_filesystem, mock_register, mock_get_protocol, mock_alluxio):
81+
def test_register_ufs_fallback(
82+
self,
83+
mock_get_fs_class,
84+
mock_filesystem,
85+
mock_register,
86+
mock_get_protocol,
87+
mock_alluxio,
88+
):
7789
updater = UFSUpdater(mock_alluxio)
7890
ufs_info = UfsInfo("/mnt/s3", "s3://bucket", {"key": "val"})
7991

8092
mock_get_protocol.return_value = "s3"
81-
mock_get_fs_class.return_value = True # Simulate supported protocol
93+
mock_get_fs_class.return_value = True # Simulate supported protocol
8294
mock_fs_instance = MagicMock()
8395
mock_filesystem.return_value = mock_fs_instance
8496

@@ -92,11 +104,13 @@ def test_register_ufs_fallback(self, mock_get_fs_class, mock_filesystem, mock_re
92104

93105
@patch("alluxiofs.client.transfer.register_unregistered_ufs_to_fsspec")
94106
@patch("alluxiofs.client.transfer.fsspec.get_filesystem_class")
95-
def test_register_ufs_fallback_unsupported(self, mock_get_fs_class, mock_register, mock_alluxio):
107+
def test_register_ufs_fallback_unsupported(
108+
self, mock_get_fs_class, mock_register, mock_alluxio
109+
):
96110
updater = UFSUpdater(mock_alluxio)
97111
ufs_info = UfsInfo("/mnt/unknown", "unknown://bucket", {})
98112

99-
mock_get_fs_class.return_value = None # Simulate unsupported protocol
113+
mock_get_fs_class.return_value = None # Simulate unsupported protocol
100114

101115
with pytest.raises(ValueError, match="Unsupported protocol"):
102116
updater.register_ufs_fallback([ufs_info])
@@ -113,12 +127,20 @@ def test_get_alluxio_path_from_ufs_full_path(self, mock_alluxio):
113127
updater._cached_ufs = {"s3://bucket": "fs_instance"}
114128
updater._path_map = {"s3://bucket": "/mnt/s3"}
115129

116-
assert updater.get_alluxio_path_from_ufs_full_path("s3://bucket/file") == "/mnt/s3/file"
117-
assert updater.get_alluxio_path_from_ufs_full_path("hdfs://namenode/file") is None
130+
assert (
131+
updater.get_alluxio_path_from_ufs_full_path("s3://bucket/file")
132+
== "/mnt/s3/file"
133+
)
134+
assert (
135+
updater.get_alluxio_path_from_ufs_full_path("hdfs://namenode/file")
136+
is None
137+
)
118138

119139
@patch("alluxiofs.client.transfer.UFSUpdater.parse_ufs_info")
120140
@patch("alluxiofs.client.transfer.UFSUpdater.register_ufs_fallback")
121-
def test_execute_update_success(self, mock_register, mock_parse, mock_alluxio):
141+
def test_execute_update_success(
142+
self, mock_register, mock_parse, mock_alluxio
143+
):
122144
updater = UFSUpdater(mock_alluxio)
123145
mock_alluxio.get_ufs_info_from_worker.return_value = '{"json": "data"}'
124146
mock_parse.return_value = ["parsed_info"]
@@ -139,7 +161,9 @@ def test_execute_update_fail_none(self, mock_alluxio):
139161

140162
def test_execute_update_exception(self, mock_alluxio):
141163
updater = UFSUpdater(mock_alluxio)
142-
mock_alluxio.get_ufs_info_from_worker.side_effect = Exception("Network error")
164+
mock_alluxio.get_ufs_info_from_worker.side_effect = Exception(
165+
"Network error"
166+
)
143167

144168
updater._execute_update()
145169
# Should catch exception and log error
@@ -186,6 +210,9 @@ def test_must_get_methods_wait(self, mock_alluxio):
186210
with pytest.raises(ValueError):
187211
updater.must_get_ufs_from_path("path")
188212

189-
updater.get_alluxio_path_from_ufs_full_path = MagicMock(return_value="/path")
190-
assert updater.must_get_alluxio_path_from_ufs_full_path("path") == "/path"
191-
213+
updater.get_alluxio_path_from_ufs_full_path = MagicMock(
214+
return_value="/path"
215+
)
216+
assert (
217+
updater.must_get_alluxio_path_from_ufs_full_path("path") == "/path"
218+
)

0 commit comments

Comments
 (0)