Skip to content

Commit

Permalink
Exception handling for Server.run and py.tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hyades committed Jul 19, 2013
1 parent 832e0cb commit 8807d07
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
11 changes: 8 additions & 3 deletions python-api/gstswitch/exception.py
@@ -1,5 +1,10 @@
class Error(Exception):
"""docstring for Error"""
class PathError(Exception):
"""docstring for PathError"""
def __init__(self):
pass


class ServerProcessError(object):
"""docstring for ServerProcessError"""
def __init__(self):
super(Error, self).__init__()
pass
24 changes: 17 additions & 7 deletions python-api/gstswitch/server.py
Expand Up @@ -3,6 +3,7 @@
import signal
import subprocess

from errno import *
from exception import *
from time import sleep

Expand Down Expand Up @@ -40,7 +41,8 @@ def run(self, gst_option=''):
self.gst_option_string = gst_option
print "Starting server"
self.proc = self._run_process()
self.pid = self.proc.pid
if self.proc:
self.pid = self.proc.pid
# TODO: Sleep time may vary
sleep(self.SLEEP_TIME)

Expand All @@ -59,6 +61,7 @@ def _run_process(self):
self.audio_port,
self.control_port,
self.record_file)
cmd = " ".join(cmd.split())
proc = self._start_process(cmd)
return proc

Expand All @@ -67,16 +70,23 @@ def _start_process(self, cmd):
:param cmd: The command which needs to be excecuted
:returns: process created
:raises IOError: Fail to open /dev/null (os.devnull)
:raises PathError: Unable to find gst-switch-srv at path specified
:raises ServerProcessError: Creating the gst-switch-srv gives a OS based error.
"""
print 'Creating process %s' % (cmd)
try:
tempf = open(os.devnull, 'w')
process = subprocess.Popen(cmd.split(), stdout=tempf, stderr=tempf, bufsize=-1, shell=False)
except IOError as e:
print "I/O error: os.devnull: {0}".format(e.strerror)
with open(os.devnull, 'w') as tempf:
process = subprocess.Popen(cmd.split(), stdout=tempf, stderr=tempf, bufsize=-1, shell=False)
print cmd
return process
except IOError:
print "cannot open os.devnull"
except OSError as e:
print "OS error: {0}: {1}".format(cmd, e.strerror)
return process
if e.errno == ENOENT:
raise PathError
else:
raise ServerProcessError

def terminate(self):
"""Terminates the server
Expand Down
2 changes: 1 addition & 1 deletion python-api/gstswitch/test.py
Expand Up @@ -57,7 +57,7 @@

# time.sleep(0.1)
sources.terminate()
output.terminate()
# output.terminate()
s.terminate()

finally:
Expand Down
19 changes: 19 additions & 0 deletions python-api/gstswitch/test_server.py
@@ -0,0 +1,19 @@
from server import Server
import pytest
from exception import *


class TestRun(object):

def test_invalid_path(self):
path = '/usr/'
s = Server(path=path)
with pytest.raises(PathError):
s.run()

def test_normal(self):
path = '/home/hyades/gst/master/gstreamer/tools/'
s = Server(path=path)
s.run()
assert s.proc is not None
s.terminate()

0 comments on commit 8807d07

Please sign in to comment.