Skip to content

Commit e866900

Browse files
committed
Add SIGTERM test for ongoing collection
1 parent ce5a0d7 commit e866900

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

pytroll_collectors/tests/test_geographic_gatherer.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,63 @@ def test_sigterm(tmp_config_file, tmp_config_parser):
553553
proc.join()
554554

555555
assert proc.exitcode == 0
556+
557+
558+
def test_sigterm_with_collection(tmp_config_file, tmp_config_parser):
559+
"""Test that SIGTERM signal is handled when there is collection ongoing."""
560+
import os
561+
import signal
562+
import time
563+
from multiprocessing import Process
564+
565+
from pytroll_collectors.geographic_gatherer import GeographicGatherer
566+
567+
with open(tmp_config_file, mode="w") as fp:
568+
tmp_config_parser.write(fp)
569+
570+
opts = arg_parse(["-c", "posttroll_section", "-p", "40000", "-n", "false", "-i", "localhost:12345",
571+
str(tmp_config_file)])
572+
# Use a fake trigger that initially sets some granules and after a while clears them
573+
with patch("pytroll_collectors.geographic_gatherer.PostTrollTrigger",
574+
new=FakeTriggerWithGranules):
575+
gatherer = GeographicGatherer(opts)
576+
proc = Process(target=gatherer.run)
577+
proc.start()
578+
time.sleep(1)
579+
os.kill(proc.pid, signal.SIGTERM)
580+
proc.join()
581+
582+
assert proc.exitcode == 0
583+
584+
585+
class FakeTriggerWithGranules:
586+
"""Fake trigger class used in testing SIGTERM handling.
587+
588+
At creation, adds "foo" to collector granules. When is_alive() is called the second time, it clears the granules.
589+
"""
590+
591+
def __init__(self, collectors, *args, **kwargs):
592+
"""Initialize the trigger class."""
593+
self.collectors = collectors
594+
for col in self.collectors:
595+
col.granules.append("foo")
596+
self._args = args
597+
self._kwargs = kwargs
598+
self._counter = 0
599+
600+
def is_alive(self):
601+
"""Return True for alive thread."""
602+
if self._counter > 0:
603+
# On the second call clear the granules
604+
for col in self.collectors:
605+
col.granules = []
606+
self._counter += 1
607+
return True
608+
609+
def start(self):
610+
"""Start the trigger."""
611+
pass
612+
613+
def stop(self):
614+
"""Stop the trigger."""
615+
pass

0 commit comments

Comments
 (0)