forked from benoitc/gaffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pid_channel.py
More file actions
224 lines (167 loc) · 5.08 KB
/
Copy pathtest_pid_channel.py
File metadata and controls
224 lines (167 loc) · 5.08 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
# -*- coding: utf-8 -
#
# This file is part of gaffer. See the NOTICE for more information.
import os
import sys
import time
import pytest
import pyuv
from gaffer.gafferd.http import HttpHandler
from gaffer.httpclient import Server, Process
from gaffer.httpclient.websocket import IOChannel
from gaffer.manager import Manager
from gaffer.process import ProcessConfig
from test_manager import dummy_cmd
from test_http import MockConfig
TEST_HOST = '127.0.0.1'
TEST_PORT = (os.getpid() % 31000) + 1024
TEST_URI = "%s:%s" % (TEST_HOST, TEST_PORT)
if sys.version_info >= (3, 0):
linesep = os.linesep.encode()
else:
linesep = os.linesep
def start_manager():
http_handler = HttpHandler(MockConfig(bind=TEST_URI))
m = Manager()
m.start(apps=[http_handler])
time.sleep(0.2)
return m
def get_server(loop):
return Server("http://%s:%s" % (TEST_HOST, TEST_PORT), loop=loop)
def init():
m = start_manager()
s = get_server(m.loop)
return (m, s)
def test_stdio():
m, s = init()
emitted = []
def cb(ch, data):
emitted.append(data)
responses = []
def cb2(ch, result, error):
responses.append((result, error))
if sys.platform == 'win32':
config = ProcessConfig("echo", "cmd.exe",
args=["/c", "proc_stdin_stdout.py"],
redirect_output=["stdout"], redirect_input=True)
else:
config = ProcessConfig("echo", "./proc_stdin_stdout.py",
cwd=os.path.dirname(__file__), redirect_output=["stdout"],
redirect_input=True)
# load the process
m.load(config)
time.sleep(0.2)
# start a channel
p = s.get_process(1)
channel = p.socket()
channel.start()
# subscribe to remote events
channel.start_read(cb)
# write to STDIN
channel.write(b"ECHO" + linesep, cb2)
def stop(handle):
channel.stop_read()
channel.close()
m.stop()
t = pyuv.Timer(m.loop)
t.start(stop, 0.3, 0.0)
m.run()
assert len(emitted) == 1
assert emitted == [b'ECHO\n\n']
assert responses == [(b"OK", None)]
def test_mode_readable():
m, s = init()
if sys.platform == 'win32':
config = ProcessConfig("echo", "cmd.exe",
args=["/c", "proc_stdin_stdout.py"],
redirect_output=["stdout"], redirect_input=True)
else:
config = ProcessConfig("echo", "./proc_stdin_stdout.py",
cwd=os.path.dirname(__file__), redirect_output=["stdout"],
redirect_input=True)
# load the process
m.load(config)
time.sleep(0.2)
# start a channel
p1 = s.get_process(1)
channel = p1.socket(mode=pyuv.UV_READABLE)
channel.start()
# subscribe to remote events
channel.start_read(lambda ch, d: None)
with pytest.raises(IOError):
channel.write(b"ECHO" + linesep)
def stop(handle):
channel.stop_read()
channel.close()
m.stop()
t = pyuv.Timer(m.loop)
t.start(stop, 0.3, 0.0)
m.run()
def test_mode_writable():
m, s = init()
responses = []
def cb2(ch, result, error):
responses.append((result, error))
if sys.platform == 'win32':
config = ProcessConfig("echo", "cmd.exe",
args=["/c", "proc_stdin_stdout.py"],
redirect_output=["stdout"], redirect_input=True)
else:
config = ProcessConfig("echo", "./proc_stdin_stdout.py",
cwd=os.path.dirname(__file__), redirect_output=["stdout"],
redirect_input=True)
# load the process
m.load(config)
time.sleep(0.2)
# start a channel
p1 = s.get_process(1)
channel = p1.socket(mode=pyuv.UV_WRITABLE)
channel.start()
# subscribe to remote events
with pytest.raises(IOError):
channel.start_read(lambda ch, d: None)
channel.write(b"ECHO" + linesep, cb2)
def stop(handle):
channel.stop_read()
channel.close()
m.stop()
t = pyuv.Timer(m.loop)
t.start(stop, 0.3, 0.0)
m.run()
assert responses == [(b"OK", None)]
def test_custom_stream():
m, s = init()
emitted = []
def cb(ch, data):
emitted.append(data)
responses = []
def cb2(ch, result, error):
responses.append((result, error))
if sys.platform == 'win32':
config = ProcessConfig("echo", "cmd.exe",
args=["/c", "proc_custom_stream.py"],
custom_streams=["ctrl"])
else:
config = ProcessConfig("echo", "./proc_custom_stream.py",
cwd=os.path.dirname(__file__), custom_streams=["ctrl"])
m.load(config)
# start a channel
p = s.get_process(1)
channel = p.socket(stream="ctrl")
channel.start()
# subscribe to remote events
channel.start_read(cb)
# write to STDIN
channel.write(b"ECHO" + linesep, cb2)
def stop(handle):
channel.stop_read()
channel.close()
m.stop()
t = pyuv.Timer(m.loop)
t.start(stop, 0.4, 0.0)
m.loop.run()
assert len(emitted) == 1
assert emitted == [b'ECHO\n']
assert responses == [(b"OK", None)]
if __name__ == "__main__":
test_custom_stream()