Skip to content

Commit

Permalink
Fix forking too fast code
Browse files Browse the repository at this point in the history
  • Loading branch information
sileht committed Nov 7, 2016
1 parent 303092c commit f4d5144
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cotyledon/_service_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ def _slowdown_respawn_if_needed(self):
if time.time() - self._forktimes[0] < expected_children:
LOG.info('Forking too fast, sleeping')
time.sleep(1)
self._forktimes.pop(0)
self._forktimes.append(time.time())
self._forktimes.pop(0)
self._forktimes.append(time.time())

def _start_worker(self, service_id, worker_id):
self._slowdown_respawn_if_needed()
Expand Down
3 changes: 3 additions & 0 deletions cotyledon/tests/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@

LOG = logging.getLogger("cotyledon.tests.examples")

# We don't want functional tests to wait for this:
cotyledon.ServiceManager._slowdown_respawn_if_needed = lambda *args: True


class FullService(cotyledon.Service):
name = "heavy"
Expand Down
File renamed without changes.
37 changes: 37 additions & 0 deletions cotyledon/tests/test_unit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import mock

import cotyledon
from cotyledon.tests import base


class FakeService(cotyledon.Service):
pass


class SomeTest(base.TestCase):
def test_forking_slowdown(self):
sm = cotyledon.ServiceManager()
sm.add(FakeService, workers=3)
with mock.patch('time.sleep') as sleep:
sm._slowdown_respawn_if_needed()
sm._slowdown_respawn_if_needed()
sm._slowdown_respawn_if_needed()
# We simulatge 3 more spawn
sm._slowdown_respawn_if_needed()
sm._slowdown_respawn_if_needed()
sm._slowdown_respawn_if_needed()
self.assertEqual(2, len(sleep.mock_calls))
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ install_command = pip install -U {opts} {packages}
setenv =
VIRTUAL_ENV={envdir}
deps = -r{toxinidir}/test-requirements.txt
commands = {toxinidir}/tools/pretty_tox.sh
commands = {toxinidir}/tools/pretty_tox.sh {posargs}

[testenv:pep8]
deps =
Expand Down

0 comments on commit f4d5144

Please sign in to comment.