test_popsicle.py :  » Network » Twisted » Twisted-1.0.3 » Twisted-1.0.3 » twisted » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Twisted 
Twisted » Twisted 1.0.3 » Twisted 1.0.3 » twisted » test » test_popsicle.py
# Twisted, the Framework of Your Internet
# Copyright (C) 2001-2002 Matthew W. Lefkowitz
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

from twisted.trial import unittest
from twisted.popsicle import mailsicle,freezer
import gc, shutil

class Dummy:
    name = 'UNNAMED!!!'
    __implements__ = mailsicle.IHeaderSaver

    def __init__(self,name):
        self.name = name

    def __repr__(self):
        return '<dummy %r %r>' % (self.name, id(self))

    def setName(self,name):
        self.name = name
        freezer.dirty(self)

    def getContinuations(self):
        return []

    def getItems(self):
        return [("Name",self.name)]

    def descriptiveName(self):
        return "Dummy %s" % self.name

    def getIndexes(self):
        return [('person-name',self.name)]

    def loadItems(self, items, toplevel):
        self.name = items[2][1]

    def loadContinuations(self, cont):
        pass


from twisted.python import log
import os
import shutil

from twisted.popsicle import freezer,mailsicle,picklesicle
from twisted.internet import defer


class gcall:
    def __init__(self, m, *a, **k):
        self.m = m
        self.a = a
        self.k = k
    def __del__(self):
        self.m(*self.a,**self.k)

class Friend:
    """
    Simple persistence test.
    """
    def __init__(self, name):
        """Initialize with a name.
        """
        self.name = name
        self.friendRefs = []

    def __repr__(self):
        return "<Friend %r>" % self.name

    def addFriend(self, friend):
        self.friendRefs.append(freezer.ref(friend))

    def _cbPrint(self, l):
        # print "Friends!", l
        pass

    def printFriendList(self):
        # TODO: convert to "walkFriendList" to reduce interactive output
        l = []
        for f in self.friendRefs:
            l.append(f())
        dl = defer.DeferredList(l)
        dl.addCallback(self._cbPrint)


class PicklesicleTest(unittest.TestCase):
    # Python 2.1 doesn't have gc.get_referrers (and Jython probably doesn't
    # either)
    if hasattr(gc, 'get_referrers'):
        def testPickle(self):
            ps = picklesicle.Picklesicle("POPSICLE", [Friend])
            bob = Friend("bob")
            alice = Friend("alice")
            bob.addFriend(alice)
            alice.addFriend(bob)
            bob.printFriendList()
            # ps.save(bob) # doesn't work as you'd expect, sadly :-\
            freezer.ref(bob).acquireOID(ps)
            freezer.dirty(bob)
            freezer.clean()
            l = []
            alice._tracker = gcall(l.append, 1)
            del alice
            assert l, "Alice not garbage collected."
            assert len(filter(lambda x: isinstance(x, Friend),
                              gc.get_referrers(Friend))) == 1, (
                "More than one friend alive.")
            bob.printFriendList()



class MailsicleTest(unittest.TestCase):
    def makeMailsicle(self, msn):
        if os.path.exists(msn):
            shutil.rmtree(msn)
        return mailsicle.Mailsicle(msn)

    def testPersistentStuff(self):
        try:
            ''.encode('hex')
        except:
            return
        ms = self.makeMailsicle("MAILSICLE_AUTH")
        from twisted.internet.app import Application
        msa = mailsicle.MailsicleAuthorizer(ms)
        a = Application("bobbo", authorizer=msa)
        mss = mailsicle.MailsicleService(ms, "twisted.stuff", a, msa)
        bob = mss.createPerspective("bob")
        bobd = mss.getPerspectiveRequest("bob")
        l = []
        bobd.addCallback(l.append)
        self.assertEquals(l[0],bob)
        idn = l[0].makeIdentity("asdf")
        import operator
        self.assertEquals(operator.truth(idn.verifyPlainPassword('asdf').result),
                          1)
        freezer.clean()
        l = []
        idn.dl = gcall(l.append, 'yes')
        del idn
        del bobd
        self.assertEquals(l[0], 'yes')
        idn = bob.getIdentityRequest().result
        from twisted.python import failure
        if isinstance(idn, failure.Failure):
            raise idn
        rslt = idn.verifyPlainPassword('asdf').result
        self.failUnless(rslt)
        

    def testIndexing(self):
        ms = self.makeMailsicle("BOBJANE_TEST")
        d1 = Dummy("bob")
        d2 = Dummy("jane")
        freezer.register(d1, ms)
        freezer.register(d2, ms)
        freezer.clean()
        del d1
        del d2
        yy = []
        zz = []
        yy = ms.queryIndex("person-name","bob").fetchNow()
        zz = ms.queryIndex("person-name","bob").fetchNow()
        yy.sort()
        zz.sort()
        # log.err(yy[0])
        self.assertEquals(yy,zz)
        self.assertEquals(len(yy),1)
        yy[0].setName("joe")
        zz = []
        yy = []
        #print 'cleaning pops'
        #print freezer.theFreezer.persistentObjects.items()
        freezer.clean()
        # print 'cleaned'
        ms.queryIndex("person-name","bob").fetch().addCallback(yy.extend)
        ms.queryIndex("person-name","joe").fetch().addCallback(zz.extend)
        self.assertEquals(yy,[])
        self.assertEquals(len(zz),1)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.