Skip to content
This repository has been archived by the owner on Dec 19, 2017. It is now read-only.

Commit

Permalink
Increasing tests for service
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Truemper committed Apr 18, 2014
1 parent 679f9ab commit 4cb150e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
12 changes: 12 additions & 0 deletions .coveragerc
@@ -0,0 +1,12 @@
# .coveragerc to control coverage.py
[run]
branch = True

[report]
# Regexes for lines to exclude from consideration
exclude_lines =
# Have to re-enable the standard pragma
pragma: no cover

ignore_errors = True

4 changes: 2 additions & 2 deletions supercell/service.py
Expand Up @@ -27,6 +27,7 @@
from logging import Formatter, StreamHandler
import os
import signal
import socket
import sys
import time

Expand Down Expand Up @@ -100,7 +101,6 @@ def main(self):
self.server = HTTPServer(app)

if self.config.socketfd:
import socket
sock = socket.fromfd(int(self.config.socketfd), socket.AF_INET,
socket.SOCK_STREAM)
self.server.add_socket(sock)
Expand All @@ -109,7 +109,7 @@ def main(self):
self.server.start(1)

def sig_handler(sig, frame):
IOLoop.instance().add_callback(self.shutdown)
IOLoop.instance().add_callback(self.shutdown) # pragma: no cover
signal.signal(signal.SIGTERM, sig_handler)
signal.signal(signal.SIGINT, sig_handler)

Expand Down
61 changes: 60 additions & 1 deletion test/test_service.py
Expand Up @@ -24,6 +24,8 @@
else:
from unittest2 import TestCase

import socket

import mock
import pytest

Expand Down Expand Up @@ -114,7 +116,64 @@ def test_main_method(self, ioloop_instance_mock):
service = MyService()
service.main()

self.assertEqual(len(ioloop_instance_mock.mock_calls), 4)
expected = [mock.call(), mock.call().add_handler(mock.ANY, mock.ANY,
mock.ANY),
mock.call(), mock.call().start()]
assert expected == ioloop_instance_mock.mock_calls

@mock.patch('tornado.ioloop.IOLoop.instance')
@mock.patch('socket.fromfd')
def test_startup_with_socket_fd(self, socket_fromfd_mock,
ioloop_instance_mock):

service = MyService()
service.config.socketfd = '123'

service.main()

expected = [mock.call(), mock.call().add_handler(mock.ANY, mock.ANY,
mock.ANY),
mock.call(), mock.call().start()]
assert expected == ioloop_instance_mock.mock_calls

assert (mock.call(123, socket.AF_INET, socket.SOCK_STREAM)
in socket_fromfd_mock.mock_calls)

@mock.patch('tornado.ioloop.IOLoop.instance')
def test_graceful_shutdown_pending_callbacks(self, ioloop_instance_mock):
service = MyService()
service.main()

expected = [mock.call(), mock.call().add_handler(mock.ANY, mock.ANY,
mock.ANY),
mock.call(), mock.call().start()]
assert expected == ioloop_instance_mock.mock_calls

service.shutdown()

expected.extend([mock.call(), mock.call().remove_handler(mock.ANY),
mock.call()._callbacks.__nonzero__(),
mock.call().add_timeout(mock.ANY, mock.ANY)])
assert expected == ioloop_instance_mock.mock_calls

@mock.patch('tornado.ioloop.IOLoop.instance')
def test_graceful_final_shutdown(self, ioloop_instance_mock):
service = MyService()
service.main()
service.config.max_grace_seconds = -10

expected = [mock.call(), mock.call().add_handler(mock.ANY, mock.ANY,
mock.ANY),
mock.call(), mock.call().start()]
assert expected == ioloop_instance_mock.mock_calls

service.shutdown()

expected.extend([mock.call(), mock.call().remove_handler(mock.ANY),
mock.call().stop()])
assert expected == ioloop_instance_mock.mock_calls

service.config.max_grace_seconds = 3


class ApplicationIntegrationTest(AsyncHTTPTestCase):
Expand Down

0 comments on commit 4cb150e

Please sign in to comment.