File: test_decorators.py

package info (click to toggle)
python-scrapli 2023.7.30-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,536 kB
  • sloc: python: 14,459; makefile: 72
file content (318 lines) | stat: -rw-r--r-- 10,466 bytes parent folder | download | duplicates (2)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import asyncio
import time
from contextlib import suppress

import pytest

from scrapli.decorators import timeout_modifier, timeout_wrapper
from scrapli.exceptions import ScrapliTimeout


@pytest.mark.parametrize(
    "test_data",
    (
        1,
        0,
    ),
    ids=("timeout_operation", "timeout_disabled"),
)
def test_transport_timeout_sync_signals(monkeypatch, sync_transport_no_abc, test_data):
    timeout_transport = test_data
    sync_transport_no_abc._base_transport_args.timeout_transport = timeout_transport

    @timeout_wrapper
    def _open(cls):
        return cls._base_transport_args.timeout_transport

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.transport.base.sync_transport.Transport.open", _open)

    # simply running this to make sure it runs and allows the wrapped function to return nicely
    assert sync_transport_no_abc.open() == timeout_transport


@pytest.mark.parametrize(
    "test_data",
    (
        1,
        0,
    ),
    ids=("timeout_operation", "timeout_disabled"),
)
def test_transport_timeout_sync_multiprocessing(monkeypatch, sync_transport_no_abc, test_data):
    timeout_transport = test_data
    sync_transport_no_abc._base_transport_args.timeout_transport = timeout_transport

    @timeout_wrapper
    def _open(cls):
        return cls._base_transport_args.timeout_transport

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.transport.base.sync_transport.Transport.open", _open)
    # just patch _IS_WINDOWS to force using multiprocessing timeout
    monkeypatch.setattr("scrapli.decorators._IS_WINDOWS", True)

    # simply running this to make sure it runs and allows the wrapped function to return nicely
    assert sync_transport_no_abc.open() == timeout_transport


def test_transport_timeout_sync_timed_out_signals(monkeypatch, sync_transport_no_abc):
    sync_transport_no_abc._base_transport_args.timeout_transport = 0.1

    @timeout_wrapper
    def _open(cls):
        time.sleep(0.5)

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.transport.base.sync_transport.Transport.open", _open)

    with pytest.raises(ScrapliTimeout):
        sync_transport_no_abc.open()


def test_transport_timeout_sync_timed_out_multiprocessing(monkeypatch, sync_transport_no_abc):
    sync_transport_no_abc._base_transport_args.timeout_transport = 0.1

    @timeout_wrapper
    def _open(cls):
        time.sleep(0.5)

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.transport.base.sync_transport.Transport.open", _open)
    # just patch _IS_WINDOWS to force using multiprocessing timeout
    monkeypatch.setattr("scrapli.decorators._IS_WINDOWS", True)

    with pytest.raises(ScrapliTimeout):
        sync_transport_no_abc.open()


async def test_transport_timeout_async_timed_out(monkeypatch, async_transport_no_abc):
    async_transport_no_abc._base_transport_args.timeout_transport = 0.1

    @timeout_wrapper
    async def _open(cls):
        await asyncio.sleep(0.5)

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.transport.base.async_transport.AsyncTransport.open", _open)

    with pytest.raises(ScrapliTimeout):
        await async_transport_no_abc.open()


@pytest.mark.parametrize(
    "test_data",
    (
        1,
        0,
    ),
    ids=("timeout_operation", "timeout_disabled"),
)
async def test_transport_timeout_async(monkeypatch, async_transport_no_abc, test_data):
    timeout_transport = test_data
    async_transport_no_abc._base_transport_args.timeout_transport = timeout_transport

    @timeout_wrapper
    async def _open(cls):
        return cls._base_transport_args.timeout_transport

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.transport.base.async_transport.AsyncTransport.open", _open)

    # simply running this to make sure it runs and allows the wrapped function to return nicely
    assert await async_transport_no_abc.open() == timeout_transport


@pytest.mark.parametrize(
    "test_data",
    (
        1,
        0,
    ),
    ids=("timeout_operation", "timeout_disabled"),
)
def test_channel_timeout_sync_signals(monkeypatch, sync_channel, test_data):
    timeout_ops = test_data
    sync_channel._base_channel_args.timeout_ops = timeout_ops

    @timeout_wrapper
    def _send_input(cls):
        return cls._base_channel_args.timeout_ops

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.channel.sync_channel.Channel.send_input", _send_input)

    # simply running this to make sure it runs and allows the wrapped function to return nicely
    assert sync_channel.send_input() == timeout_ops


@pytest.mark.parametrize(
    "test_data",
    (
        1,
        0,
    ),
    ids=("timeout_operation", "timeout_disabled"),
)
def test_channel_timeout_sync_multiprocessing(monkeypatch, sync_channel, test_data):
    timeout_ops = test_data
    sync_channel._base_channel_args.timeout_ops = timeout_ops

    @timeout_wrapper
    def _send_input(cls):
        return cls._base_channel_args.timeout_ops

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.channel.sync_channel.Channel.send_input", _send_input)
    # just patch _IS_WINDOWS to force using multiprocessing timeout
    monkeypatch.setattr("scrapli.decorators._IS_WINDOWS", True)

    # simply running this to make sure it runs and allows the wrapped function to return nicely
    assert sync_channel.send_input() == timeout_ops


def test_channel_timeout_sync_timed_out_signals(monkeypatch, sync_channel):
    sync_channel._base_channel_args.timeout_ops = 0.1

    @timeout_wrapper
    def _send_input(cls):
        time.sleep(0.5)

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.channel.sync_channel.Channel.send_input", _send_input)

    with pytest.raises(ScrapliTimeout):
        sync_channel.send_input()


def test_channel_timeout_sync_timed_out_multiprocessing(monkeypatch, sync_channel):
    sync_channel._base_channel_args.timeout_ops = 0.1

    @timeout_wrapper
    def _send_input(cls):
        time.sleep(0.5)

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.channel.sync_channel.Channel.send_input", _send_input)

    # just patch _IS_WINDOWS to force using multiprocessing timeout
    monkeypatch.setattr("scrapli.decorators._IS_WINDOWS", True)

    with pytest.raises(ScrapliTimeout):
        sync_channel.send_input()


async def test_channel_timeout_async_timed_out(monkeypatch, async_channel):
    async_channel._base_channel_args.timeout_ops = 0.1

    @timeout_wrapper
    async def _send_input(cls):
        await asyncio.sleep(0.5)

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.channel.async_channel.AsyncChannel.send_input", _send_input)

    with pytest.raises(ScrapliTimeout):
        await async_channel.send_input()


@pytest.mark.parametrize(
    "test_data",
    (
        1,
        0,
    ),
    ids=("timeout_operation", "timeout_disabled"),
)
async def test_channel_timeout_async(monkeypatch, async_channel, test_data):
    timeout_ops = test_data
    async_channel._base_channel_args.timeout_ops = timeout_ops

    @timeout_wrapper
    async def _send_input(cls):
        return cls._base_channel_args.timeout_ops

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.channel.async_channel.AsyncChannel.send_input", _send_input)

    # simply running this to make sure it runs and allows the wrapped function to return nicely
    assert await async_channel.send_input() == timeout_ops


@pytest.mark.parametrize(
    "test_data",
    (
        999,
        30,
        None,
    ),
    ids=("timeout_modified", "timeout_unchanged", "timeout_not_provided"),
)
def test_timeout_modifier(monkeypatch, sync_driver, test_data):
    timeout_ops = test_data
    assert sync_driver.timeout_ops == 30

    @timeout_modifier
    def _test_timeout_modifier(cls, timeout_ops):
        return cls.timeout_ops

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.driver.base.sync_driver.Driver.open", _test_timeout_modifier)

    modified_timeout = sync_driver.open(timeout_ops=timeout_ops)
    assert modified_timeout == timeout_ops if timeout_ops else 30
    assert sync_driver.timeout_ops == 30


@pytest.mark.parametrize(
    "test_data",
    (
        999,
        30,
        None,
    ),
    ids=("timeout_modified", "timeout_unchanged", "timeout_not_provided"),
)
async def test_timeout_modifier_async(monkeypatch, async_driver, test_data):
    timeout_ops = test_data
    assert async_driver.timeout_ops == 30

    @timeout_modifier
    async def _test_timeout_modifier(cls, timeout_ops):
        return cls.timeout_ops

    # stupid patch but does confirm the timeout modifier works as expected!
    monkeypatch.setattr("scrapli.driver.base.async_driver.AsyncDriver.open", _test_timeout_modifier)
    modified_timeout = await async_driver.open(timeout_ops=timeout_ops)
    assert modified_timeout == timeout_ops if timeout_ops else 30
    assert async_driver.timeout_ops == 30


def test_restore_timeout_ops(monkeypatch, sync_driver):
    timeout_ops = 5
    assert sync_driver.timeout_ops == 30

    @timeout_modifier
    def _test_raise_exception(cls, timeout_ops):
        raise ScrapliTimeout

    monkeypatch.setattr("scrapli.driver.base.sync_driver.Driver.open", _test_raise_exception)

    with suppress(ScrapliTimeout):
        sync_driver.open(timeout_ops=timeout_ops)
    assert sync_driver.timeout_ops == 30


async def test_restore_timeout_ops_async(monkeypatch, async_driver):
    timeout_ops = 5
    assert async_driver.timeout_ops == 30

    @timeout_modifier
    async def _test_raise_exception(cls, timeout_ops):
        raise ScrapliTimeout

    monkeypatch.setattr("scrapli.driver.base.async_driver.AsyncDriver.open", _test_raise_exception)

    with suppress(ScrapliTimeout):
        await async_driver.open(timeout_ops=timeout_ops)
    assert async_driver.timeout_ops == 30