Test added for db.remove()

Fix for https://bugs.launchpad.net/fuel/+bug/1401521 was committed w/o unit tests.
It is here.
StatSender is refactored slightly to make testing easier.

Closes-Bug: #1402000

Change-Id: Ida4120c612ba0ad3cfec18eb27d2e4a295d7c503
This commit is contained in:
Aleksey Kasatkin 2014-12-12 19:34:17 +02:00
parent 1beeae3584
commit 404030f924
2 changed files with 45 additions and 16 deletions

View File

@ -179,31 +179,31 @@ class StatsSender(object):
"Get statistics settings failed: %s", six.text_type(e))
return False
def run(self, *args, **kwargs):
def send_stats_once(self):
def dithered(medium):
return randint(int(medium * 0.9), int(medium * 1.1))
while True:
try:
if self.must_send_stats():
if self.ping_collector():
self.send_action_log()
self.send_installation_info()
time.sleep(dithered(settings.STATS_SEND_INTERVAL))
else:
time.sleep(dithered(settings.COLLECTOR_PING_INTERVAL))
try:
if self.must_send_stats():
if self.ping_collector():
self.send_action_log()
self.send_installation_info()
time.sleep(dithered(settings.STATS_SEND_INTERVAL))
else:
time.sleep(dithered(settings.STATS_ENABLE_CHECK_INTERVAL))
except Exception as e:
logger.error("Stats sender exception: %s", six.text_type(e))
finally:
db.remove()
time.sleep(dithered(settings.COLLECTOR_PING_INTERVAL))
else:
time.sleep(dithered(settings.STATS_ENABLE_CHECK_INTERVAL))
except Exception as e:
logger.error("Stats sender exception: %s", six.text_type(e))
finally:
db.remove()
def run():
logger.info("Starting standalone stats sender...")
try:
StatsSender().run()
while True:
StatsSender().send_stats_once()
except (KeyboardInterrupt, SystemExit):
logger.info("Stopping standalone stats sender...")

View File

@ -342,3 +342,32 @@ class TestStatisticsSender(BaseTestCase):
)
log_error.assert_called_once_with(
"Sending data to collector failed: %s", "custom")
@patch('nailgun.statistics.statsenderd.time.sleep')
def test_send_stats_once_after_dberror(self, sleep):
def fn():
# try to commit wrong data
Cluster.create(
{
"id": "500",
"release_id": "500"
}
)
self.db.commit()
ss = StatsSender()
ss.send_stats_once()
# one call was made (all went ok)
self.assertEqual(sleep.call_count, 1)
with patch.object(ss,
'must_send_stats',
fn):
ss.send_stats_once()
# no more calls was made because of exception
self.assertEqual(sleep.call_count, 1)
ss.send_stats_once()
# one more call was made (all went ok)
self.assertEqual(sleep.call_count, 2)