Changeset 450:cc102304fe0e

Show
Ignore:
Timestamp:
08/01/08 15:03:16 (5 months ago)
Author:
"Brian Warner <warner@lothar.com>"
branch:
default
Message:

test_logging: add first tests for IncidentGatherer?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ChangeLog

    r449 r450  
    112008-08-01  Brian Warner  <warner@lothar.com> 
     2 
     3        * foolscap/logging/gatherer.py (IncidentGathererService): get 
     4        control over stdout, so we can exercise more code during tests 
     5        (IncidentGathererService.startService): oops, this needs to be 
     6        startService instead of start 
     7 
     8        * foolscap/test/test_logging.py (IncidentGatherer): basic test of 
     9        an incident gatherer, just setup and connection so far 
    210 
    311        * foolscap/test/common.py (StallMixin): factor stall() out into a 
  • foolscap/logging/gatherer.py

    r444 r450  
    294294    implements(RILogObserver) 
    295295 
    296     def __init__(self, basedir, nodeid_s, gatherer, publisher): 
     296    def __init__(self, basedir, nodeid_s, gatherer, publisher, stdout): 
    297297        if not os.path.isdir(basedir): 
    298298            os.makedirs(basedir) 
     
    301301        self.gatherer = gatherer 
    302302        self.publisher = publisher 
     303        self.stdout = stdout 
     304        self.caught_up_d = defer.Deferred() 
    303305 
    304306    def connect(self): 
     
    311313        except EnvironmentError: 
    312314            pass 
    313         print "connected to %s, last known incident is %s" % (self.nodeid_s, 
    314                                                               latest) 
     315        print >>self.stdout, "connected to %s, last known incident is %s" \ 
     316              % (self.nodeid_s, latest) 
    315317        # now subscribe to everything since then 
    316318        d = self.publisher.callRemote("subscribe_to_incidents", self, 
    317319                                      catch_up=True, since=latest) 
     320        # for testing, we arrange for this Deferred (which governs the return 
     321        # from remote_logport) to not fire until we've finished catching up 
     322        # on all incidents. 
     323        d.addCallback(lambda res: self.caught_up_d) 
    318324        return d 
    319325 
    320326    def remote_new_incident(self, name, trigger): 
    321         print "got incident", name 
     327        print >>self.stdout, "got incident", name 
    322328        # name= should look like "incident-2008-07-29-204211-aspkxoi". We 
    323329        # prevent name= from containing path metacharacters like / or : by 
     
    357363 
    358364    def remote_done_with_incident_catchup(self): 
     365        self.caught_up_d.callback(None) 
    359366        return None 
    360367 
     
    386393    tacFile = "incident-gatherer.tac" 
    387394 
    388     def __init__(self, classifiers=[], basedir=None): 
     395    def __init__(self, classifiers=[], basedir=None, stdout=None): 
    389396        GatheringBase.__init__(self, basedir) 
    390397        self.classifiers = [] 
    391398        self.classifiers.extend(classifiers) 
     399        self.stdout = stdout 
    392400 
    393401    def addClassifier(self, f): 
     
    395403 
    396404 
    397     def start(self, res): 
     405    def startService(self): 
    398406        indir = os.path.join(self.basedir, "incidents") 
    399407        if not os.path.isdir(indir): 
     
    403411            os.makedirs(outputdir) 
    404412            self.classify_stored_incidents(indir) 
    405         return GatheringBase.start(self, res
     413        GatheringBase.startService(self
    406414 
    407415    def classify_stored_incidents(self, indir): 
    408         print "No classified/ directory: reclassifying stored incidents" 
     416        stdout = self.stdout or sys.stdout 
     417        print >>stdout, "No classified/ directory: reclassifying stored incidents" 
    409418        # now classify all stored incidents 
    410419        for nodeid_s in os.listdir(indir): 
     
    438447            raise BadTubID("%s is not a valid base32-encoded Tub ID" % tubid_s) 
    439448        basedir = os.path.join(self.basedir, "incidents", tubid_s) 
    440         o = IncidentObserver(basedir, tubid_s, self, publisher) 
     449        stdout = self.stdout or sys.stdout 
     450        o = IncidentObserver(basedir, tubid_s, self, publisher, stdout) 
    441451        d = o.connect() 
    442452        d.addCallback(lambda res: None) 
     
    444454 
    445455    def new_incident(self, abs_fn, rel_fn, nodeid_s, incident): 
    446         print "NEW INCIDENT", rel_fn 
     456        stdout = self.stdout or sys.stdout 
     457        print >>stdout, "NEW INCIDENT", rel_fn 
    447458        self.classify_incident(rel_fn, nodeid_s, incident) 
    448459 
  • foolscap/test/test_logging.py

    r448 r450  
    1414from foolscap import Referenceable 
    1515from foolscap.tokens import NoLocationError 
    16 from foolscap.test.common import PollMixin, GoodEnoughTub 
     16from foolscap.test.common import PollMixin, StallMixin, GoodEnoughTub 
    1717 
    1818 
     
    866866    test_subscribe.timeout = 20 
    867867 
    868 class Gatherer(PollMixin, unittest.TestCase, LogfileReaderMixin): 
     868class MyIncidentGathererService(gatherer.IncidentGathererService): 
     869    verbose = False 
     870 
     871    def remote_logport(self, nodeid, publisher): 
     872        d = gatherer.IncidentGathererService.remote_logport(self, 
     873                                                            nodeid, publisher) 
     874        d.addCallback(lambda res: self.d.callback(publisher)) 
     875        return d 
     876 
     877class IncidentGatherer(unittest.TestCase, 
     878                       PollMixin, StallMixin, LogfileReaderMixin): 
     879    def setUp(self): 
     880        self.parent = service.MultiService() 
     881        self.parent.startService() 
     882        self.logger = log.FoolscapLogger() 
     883 
     884    def tearDown(self): 
     885        d = defer.succeed(None) 
     886        d.addCallback(lambda res: self.parent.stopService()) 
     887        d.addCallback(flushEventualQueue) 
     888        return d 
     889 
     890    def test_incident_gatherer(self): 
     891        basedir = "logging/IncidentGatherer/incident_gatherer" 
     892        os.makedirs(basedir) 
     893        self.logger.setLogDir(basedir) 
     894 
     895        # create an incident gatherer, which will make its own Tub 
     896        ig_basedir = os.path.join(basedir, "ig") 
     897        os.makedirs(ig_basedir) 
     898        null = StringIO() 
     899        ig = MyIncidentGathererService(basedir=ig_basedir, stdout=null) 
     900        ig.tub_class = GoodEnoughTub 
     901        ig.d = defer.Deferred() 
     902        ig.setServiceParent(self.parent) 
     903 
     904        t = GoodEnoughTub() 
     905        t.logger = self.logger 
     906        t.setServiceParent(self.parent) 
     907        l = t.listenOn("tcp:0:interface=127.0.0.1") 
     908        t.setLocation("127.0.0.1:%d" % l.getPortnum()) 
     909        t.setOption("log-gatherer-furl", ig.my_furl) 
     910 
     911        d = ig.d 
     912 
     913        # give the call to remote_logport a chance to retire 
     914        d.addCallback(self.stall, 0.5) 
     915 
     916        return d 
     917 
     918 
     919class Gatherer(unittest.TestCase, LogfileReaderMixin, StallMixin, PollMixin): 
    869920    def setUp(self): 
    870921        self.parent = service.MultiService() 
     
    878929        return d 
    879930 
    880  
    881     def stall(self, res, delay=1.0): 
    882         d = defer.Deferred() 
    883         reactor.callLater(delay, d.callback, res) 
    884         return d 
    885931 
    886932    def _emit_messages_and_flush(self, res, t):