Debian Codegraph @

tests/test_buffered_pipe.py

raw
1 # Copyright (C) 2006-2007 Robey Pointer <[email protected]>
2 #
3 # This file is part of paramiko.
4 #
5 # Paramiko is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation; either version 2.1 of the License, or (at your option)
8 # any later version.
9 #
10 # Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 """
20 Some unit tests for BufferedPipe.
21 """
22
23 import threading
24 import time
25 import unittest
26
27 from paramiko.buffered_pipe import BufferedPipe, PipeTimeout
28 from paramiko import pipe
29
30
31 def delay_thread(p):
32 p.feed("a")
33 time.sleep(0.5)
34 p.feed("b")
35 p.close()
36
37
38 def close_thread(p):
39 time.sleep(0.2)
40 p.close()
41
42
43 class BufferedPipeTest(unittest.TestCase):
44 def test_buffered_pipe(self):
45 p = BufferedPipe()
46 self.assertTrue(not p.read_ready())
47 p.feed("hello.")
48 self.assertTrue(p.read_ready())
49 data = p.read(6)
50 self.assertEqual(b"hello.", data)
51
52 p.feed("plus/minus")
53 self.assertEqual(b"plu", p.read(3))
54 self.assertEqual(b"s/m", p.read(3))
55 self.assertEqual(b"inus", p.read(4))
56
57 p.close()
58 self.assertTrue(not p.read_ready())
59 self.assertEqual(b"", p.read(1))
60
61 def test_delay(self):
62 p = BufferedPipe()
63 self.assertTrue(not p.read_ready())
64 threading.Thread(target=delay_thread, args=(p,)).start()
65 self.assertEqual(b"a", p.read(1, 0.1))
66 try:
67 p.read(1, 0.1)
68 self.assertTrue(False)
69 except PipeTimeout:
70 pass
71 self.assertEqual(b"b", p.read(1, 1.0))
72 self.assertEqual(b"", p.read(1))
73
74 def test_close_while_reading(self):
75 p = BufferedPipe()
76 threading.Thread(target=close_thread, args=(p,)).start()
77 data = p.read(1, 1.0)
78 self.assertEqual(b"", data)
79
80 def test_or_pipe(self):
81 p = pipe.make_pipe()
82 p1, p2 = pipe.make_or_pipe(p)
83 self.assertFalse(p._set)
84 p1.set()
85 self.assertTrue(p._set)
86 p2.set()
87 self.assertTrue(p._set)
88 p1.clear()
89 self.assertTrue(p._set)
90 p2.clear()
91 self.assertFalse(p._set)