forked from ParallelSSH/parallel-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_reader.py
86 lines (76 loc) · 2.85 KB
/
test_reader.py
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
# This file is part of parallel-ssh.
#
# Copyright (C) 2014-2022 Panos Kittenis and contributors.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, version 2.1.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import unittest
from random import random, randint, randrange
from string import ascii_letters
from gevent.queue import Queue
from gevent import spawn, sleep
from pssh.clients.reader import ConcurrentRWBuffer
class TestReaderBuffer(unittest.TestCase):
def setUp(self):
self.buffer = ConcurrentRWBuffer()
self.data = b'test'
def test_write(self):
self.buffer.write(self.data)
data = self.buffer.read()
self.assertEqual(data, self.data)
def test_multi_write_read(self):
written_data = self.data
self.buffer.write(self.data)
more_data = b"more data"
written_data += more_data
self.buffer.write(more_data)
data = self.buffer.read()
self.assertEqual(data, written_data)
new_write_data = b"yet more data"
self.buffer.write(new_write_data)
data = self.buffer.read()
self.assertEqual(data, new_write_data)
new_write_data = b"even more data"
self.buffer.write(new_write_data)
data = self.buffer.read()
self.assertEqual(data, new_write_data)
def test_concurrent_rw(self):
written_data = Queue()
def _writer(_buffer):
while True:
data = b"".join([ascii_letters[m].encode() for m in [randrange(0, 8) for _ in range(8)]])
_buffer.write(data)
written_data.put(data)
sleep(0.2)
writer = spawn(_writer, self.buffer)
writer.start()
sleep(0.5)
data = self.buffer.read()
_data = b""
while written_data.qsize() !=0 :
_data += written_data.get()
self.assertEqual(data, _data)
sleep(0.5)
data = self.buffer.read()
_data = b""
while written_data.qsize() !=0 :
_data += written_data.get()
self.assertEqual(data, _data)
writer.kill()
writer.get()
def test_non_cur_write(self):
data = b"asdf"
self.buffer.write(data)
self.buffer._buffer.seek(0)
self.buffer.write(data)
self.assertEqual(self.buffer.read(), data + data)