Skip to content

Commit

Permalink
python tests: Implemented signal.alarm for Windows
Browse files Browse the repository at this point in the history
signal.alarm is not available in Windows and would trigger an exception
when called. Implemented this to mentain compatibility between
Windows and Linux for python tests.

Signed-off-by: Paul-Daniel Boca <[email protected]>
Acked-by: Alin Gabriel Serdean <[email protected]>
Signed-off-by: Gurucharan Shetty <[email protected]>
  • Loading branch information
Paul Boca authored and shettyg committed Aug 3, 2016
1 parent 922247c commit 36d5163
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
23 changes: 23 additions & 0 deletions python/ovs/fatal_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import atexit
import os
import signal
import sys

import ovs.vlog

Expand Down Expand Up @@ -134,3 +135,25 @@ def _init():
if signal.getsignal(signr) == signal.SIG_DFL:
signal.signal(signr, _signal_handler)
atexit.register(_atexit_handler)


def signal_alarm(timeout):
if sys.platform == "win32":
import os
import time
import threading

class Alarm (threading.Thread):
def __init__(self, timeout):
super(Alarm, self).__init__()
self.timeout = timeout
self.setDaemon(True)

def run(self):
time.sleep(self.timeout)
os._exit(1)

alarm = Alarm(timeout)
alarm.start()
else:
signal.alarm(timeout)
4 changes: 2 additions & 2 deletions tests/appctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
# limitations under the License.

import argparse
import signal
import sys

import ovs.daemon
import ovs.unixctl
import ovs.unixctl.client
import ovs.util
import ovs.vlog
from ovs.fatal_signal import signal_alarm


def connect_to_target(target):
Expand Down Expand Up @@ -52,7 +52,7 @@ def main():
args = parser.parse_args()

if args.timeout:
signal.alarm(int(args.timeout))
signal_alarm(int(args.timeout))

ovs.vlog.Vlog.init()
target = args.target
Expand Down
4 changes: 2 additions & 2 deletions tests/test-ovsdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import getopt
import re
import os
import signal
import sys
import uuid

Expand All @@ -29,6 +28,7 @@
import ovs.ovsuuid
import ovs.poller
import ovs.util
from ovs.fatal_signal import signal_alarm
import six


Expand Down Expand Up @@ -676,7 +676,7 @@ def main(argv):
except TypeError:
raise error.Error("value %s on -t or --timeout is not at "
"least 1" % value)
signal.alarm(timeout)
signal_alarm(timeout)
else:
sys.exit(0)

Expand Down
3 changes: 2 additions & 1 deletion tests/test-unix-socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import sys

import ovs.socket_util
from ovs.fatal_signal import signal_alarm


def main(argv):
Expand All @@ -33,7 +34,7 @@ def main(argv):
sockname2 = sockname1

signal.signal(signal.SIGALRM, signal.SIG_DFL)
signal.alarm(5)
signal_alarm(5)

# Create a listening socket under name 'sockname1'.
error, sock1 = ovs.socket_util.make_unix_socket(socket.SOCK_STREAM, False,
Expand Down

0 comments on commit 36d5163

Please sign in to comment.