-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathgrid_client_proxy_test.py
More file actions
277 lines (235 loc) · 9.3 KB
/
grid_client_proxy_test.py
File metadata and controls
277 lines (235 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Copyright 2025 Flower Labs GmbH. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""GridClientProxy tests."""
import unittest
import unittest.mock
from collections.abc import Callable, Iterable
from typing import Any, cast
from unittest.mock import Mock, patch
import numpy as np
import flwr
from flwr.common import Error, Message, RecordDict
from flwr.common import recorddict_compat as compat
from flwr.common.typing import (
Code,
Config,
EvaluateIns,
EvaluateRes,
FitIns,
FitRes,
GetParametersIns,
GetParametersRes,
GetPropertiesIns,
GetPropertiesRes,
Parameters,
Properties,
Status,
)
from flwr.server.compat.grid_client_proxy import GridClientProxy
MESSAGE_PARAMETERS = Parameters(tensors=[b"abc"], tensor_type="np")
CLIENT_PROPERTIES = cast(Properties, {"tensor_type": "numpy.ndarray"})
CLIENT_STATUS = Status(code=Code.OK, message="OK")
ERROR_REPLY = Error(code=0, reason="mock error")
RUN_ID = 61016
NODE_ID = 1
class GridClientProxyTestCase(unittest.TestCase):
"""Tests for GridClientProxy."""
def setUp(self) -> None:
"""Set up mocks for tests."""
grid = Mock()
grid.get_node_ids.return_value = [1]
client = GridClientProxy(node_id=NODE_ID, grid=grid, run_id=61016)
self.patcher = patch(
"flwr.server.compat.grid_client_proxy.Message",
side_effect=self._mock_message_init,
)
self.grid = grid
self.client = client
self.created_msg: Message | None = None
self.called_times: int = 0
self.patcher.start()
def tearDown(self) -> None:
"""Tear down mocks."""
self.patcher.stop()
def test_get_properties(self) -> None:
"""Test positive case."""
# Prepare
res = GetPropertiesRes(status=CLIENT_STATUS, properties=CLIENT_PROPERTIES)
self.grid.send_and_receive.side_effect = self._exec_send_and_receive(res)
request_properties: Config = {"tensor_type": "str"}
ins = GetPropertiesIns(config=request_properties)
# Execute
value = self.client.get_properties(ins, timeout=None, group_id=0)
# Assert
self._common_assertions(ins)
self.assertEqual(value.properties["tensor_type"], "numpy.ndarray")
def test_get_parameters(self) -> None:
"""Test positive case."""
# Prepare
res = GetParametersRes(
status=CLIENT_STATUS,
parameters=MESSAGE_PARAMETERS,
)
self.grid.send_and_receive.side_effect = self._exec_send_and_receive(res)
ins = GetParametersIns(config={})
# Execute
value = self.client.get_parameters(ins, timeout=None, group_id=0)
# Assert
self._common_assertions(ins)
self.assertEqual(value, res)
def test_fit(self) -> None:
"""Test positive case."""
# Prepare
res = FitRes(
status=CLIENT_STATUS,
parameters=MESSAGE_PARAMETERS,
num_examples=10,
metrics={},
)
self.grid.send_and_receive.side_effect = self._exec_send_and_receive(res)
parameters = flwr.common.ndarrays_to_parameters([np.ones((2, 2))])
ins = FitIns(parameters, {})
# Execute
value = self.client.fit(ins=ins, timeout=None, group_id=0)
# Assert
self._common_assertions(ins)
self.assertEqual(value, res)
def test_evaluate(self) -> None:
"""Test positive case."""
# Prepare
res = EvaluateRes(
status=CLIENT_STATUS,
loss=0.0,
num_examples=0,
metrics={},
)
self.grid.send_and_receive.side_effect = self._exec_send_and_receive(res)
parameters = Parameters(tensors=[b"random params%^&*F"], tensor_type="np")
ins = EvaluateIns(parameters, {})
# Execute
value = self.client.evaluate(ins, timeout=None, group_id=0)
# Assert
self._common_assertions(ins)
self.assertEqual(value, res)
def test_get_properties_and_fail(self) -> None:
"""Test negative case."""
# Prepare
self.grid.send_and_receive.side_effect = self._exec_send_and_receive(
None, error_reply=True
)
request_properties: Config = {"tensor_type": "str"}
ins = GetPropertiesIns(config=request_properties)
# Execute and assert
self.assertRaises(
ValueError, self.client.get_properties, ins, timeout=None, group_id=0
)
self._common_assertions(ins)
def test_get_parameters_and_fail(self) -> None:
"""Test negative case."""
# Prepare
self.grid.send_and_receive.side_effect = self._exec_send_and_receive(
None, error_reply=True
)
ins = GetParametersIns(config={})
# Execute and assert
self.assertRaises(
ValueError, self.client.get_parameters, ins, timeout=None, group_id=0
)
self._common_assertions(ins)
def test_fit_and_fail(self) -> None:
"""Test negative case."""
# Prepare
self.grid.send_and_receive.side_effect = self._exec_send_and_receive(
None, error_reply=True
)
parameters = flwr.common.ndarrays_to_parameters([np.ones((2, 2))])
ins = FitIns(parameters, {})
# Execute and assert
self.assertRaises(ValueError, self.client.fit, ins, timeout=None, group_id=0)
self._common_assertions(ins)
def test_evaluate_and_fail(self) -> None:
"""Test negative case."""
# Prepare
self.grid.send_and_receive.side_effect = self._exec_send_and_receive(
None, error_reply=True
)
parameters = Parameters(tensors=[b"random params%^&*F"], tensor_type="np")
ins = EvaluateIns(parameters, {})
# Execute and assert
self.assertRaises(
ValueError, self.client.evaluate, ins, timeout=None, group_id=0
)
self._common_assertions(ins)
def _mock_message_init( # pylint: disable=R0913,too-many-positional-arguments
self,
content: RecordDict,
dst_node_id: int,
message_type: str,
ttl: float | None = None,
group_id: str | None = None,
) -> Message:
"""Create a new message.
This is a method for the Mock object.
"""
self.called_times += 1
self.created_msg = Message(
content, dst_node_id, message_type, ttl=ttl, group_id=group_id
)
return self.created_msg
def _exec_send_and_receive(
self,
res: GetParametersRes | GetPropertiesRes | FitRes | EvaluateRes | None,
error_reply: bool = False,
) -> Callable[[Iterable[Message]], Iterable[Message]]:
"""Get the generate_replies function that sets the return value of grid's
send_and_receive when called."""
def generate_replies(messages: Iterable[Message]) -> Iterable[Message]:
msg = list(messages)[0]
recorddict = None
if error_reply:
pass
elif isinstance(res, GetParametersRes):
recorddict = compat.getparametersres_to_recorddict(res, True)
elif isinstance(res, GetPropertiesRes):
recorddict = compat.getpropertiesres_to_recorddict(res)
elif isinstance(res, FitRes):
recorddict = compat.fitres_to_recorddict(res, True)
elif isinstance(res, EvaluateRes):
recorddict = compat.evaluateres_to_recorddict(res)
if recorddict is not None:
ret = Message(recorddict, reply_to=msg)
else:
ret = Message(ERROR_REPLY, reply_to=msg)
# Reply messages given the push message
return [ret]
return generate_replies
def _common_assertions(self, original_ins: Any) -> None:
"""Check common assertions."""
# Check if the created message contains the orignal *Ins
assert self.created_msg is not None
actual_ins = { # type: ignore
GetPropertiesIns: compat.recorddict_to_getpropertiesins,
GetParametersIns: compat.recorddict_to_getparametersins,
FitIns: (lambda x: compat.recorddict_to_fitins(x, True)),
EvaluateIns: (lambda x: compat.recorddict_to_evaluateins(x, True)),
}[type(original_ins)](self.created_msg.content)
self.assertEqual(self.called_times, 1)
self.assertEqual(actual_ins, original_ins)
# Check if send_and_receive is called once with expected args/kwargs.
self.grid.send_and_receive.assert_called_once()
try:
self.grid.send_and_receive.assert_any_call([self.created_msg])
except AssertionError:
self.grid.send_and_receive.assert_any_call(messages=[self.created_msg])