products/CPSRSS

changeset 216:30f6d0876296 authforward

Testing for per-user caching capabilities
author Georges Racinet on purity.racinet.fr <georges@racinet.fr>
date Thu, 15 Jul 2010 11:29:51 +0200
parents 8d651512b305
children e45ebb5d4820
files RSSChannel.py tests/testRSS.py tests/zope_user2.rss
diffstat 3 files changed, 81 insertions(+), 28 deletions(-) [+]
line diff
     1.1 --- a/RSSChannel.py
     1.2 +++ b/RSSChannel.py
     1.3 @@ -174,13 +174,15 @@
     1.4          evtool.notifyEvent('rss_channel_refresh', self, {})
     1.5  
     1.6      security.declareProtected(View, 'getData')
     1.7 -    def getData(self, maxItems=None):
     1.8 -        """Get the data for this channel, as a dict."""
     1.9 +    def getData(self, maxItems=None, user_id=None):
    1.10 +        """Get the data for this channel, as a dict.
    1.11 +        If user_id is specified, a separate cache is kept for this user.
    1.12 +        This is done anyway in case auth is being forwarded. Having this as
    1.13 +        a direct option is especially useful for tests.
    1.14 +        """
    1.15  
    1.16 -        if self.forward_auth:
    1.17 +        if user_id is None and self.forward_auth:
    1.18              user_id = getSecurityManager().getUser().getId()
    1.19 -        else:
    1.20 -            user_id = None
    1.21  
    1.22          self._maybeRefresh(user_id=user_id)
    1.23          data = self._data[user_id].copy()
    1.24 @@ -200,9 +202,16 @@
    1.25      def _maybeRefresh(self, user_id=None):
    1.26          """Refresh if on lazy refresh and the delay has elapsed."""
    1.27  
    1.28 +        now = int(time.time())
    1.29 +
    1.30 +        delay = self.refresh_delay # acquired from parent (portal_rss)
    1.31 +
    1.32          # cleaning up all stale caches safe this one in order to ease on memory
    1.33          # stale is stronger than to be refreshed (we have last_modified, etag
    1.34          # and such for the latter).
    1.35 +        max_stale = self.max_stale
    1.36 +        if max_stale == 0:
    1.37 +            max_stale = delay * 10
    1.38          for u, t in self._refresh_time.items():
    1.39              if u == user_id:
    1.40                  continue
    1.41 @@ -218,8 +227,6 @@
    1.42              logger.debug('_maybeRefresh: not on lazy refresh')
    1.43              self._refresh(user_id=user_id)
    1.44              return
    1.45 -        delay = self.refresh_delay # acquired from parent (portal_rss)
    1.46 -        now = int(time.time())
    1.47  
    1.48          latest = self._refresh_time.get(user_id, 0)
    1.49          if user_id is None:
     2.1 --- a/tests/testRSS.py
     2.2 +++ b/tests/testRSS.py
     2.3 @@ -6,38 +6,62 @@
     2.4  import CPSRSSTestCase
     2.5  
     2.6  class TestRSSTool(CPSRSSTestCase.CPSRSSTestCase):
     2.7 -    def testEmptyTool(self):
     2.8 +
     2.9 +    def afterSetUp(self):
    2.10 +        rss_tool = self.rss_tool = self.portal.portal_rss
    2.11 +        self.base_url = os.path.abspath(
    2.12 +            os.path.dirname(CPSRSSTestCase.__file__))
    2.13 +        url = os.path.join(self.base_url, 'zope.rss')
    2.14 +        rss_tool.manage_addRSSChannel('channel', url)
    2.15 +        self.channel = rss_tool.channel
    2.16 +
    2.17 +    def testTool(self):
    2.18          rss_tool = self.portal.portal_rss
    2.19          self.assertEquals(rss_tool.meta_type, 'RSS Tool')
    2.20 -        self.assertEquals(rss_tool.objectIds(), [])
    2.21 +        self.assertEquals(rss_tool.objectIds(), ['channel'])
    2.22 +        # now testing if there is no channel
    2.23 +        rss_tool.manage_delObjects(['channel'])
    2.24          rss_tool.refresh()
    2.25  
    2.26      def _testChannel(self, lazy_refresh=0):
    2.27          rss_tool = self.portal.portal_rss
    2.28          rss_tool.lazy_refresh = lazy_refresh
    2.29 -        url = os.path.abspath(os.path.join(
    2.30 -            os.path.dirname(CPSRSSTestCase.__file__), 'zope.rss'))
    2.31 -        rss_tool.manage_addRSSChannel('channel', url)
    2.32 -        self.assertEquals(rss_tool.objectIds(), ['channel'])
    2.33          rss_tool.refresh()
    2.34          d = rss_tool.channel.getData()
    2.35          self.assertEquals(d['url'], 'http://zope.org')
    2.36          self.assertEquals(d['title'], 'Zope.org')
    2.37          self.assertEquals(d['description'], '')
    2.38  
    2.39 +    def _testPerUserChannel(self, lazy_refresh=0):
    2.40 +        self.rss_tool.lazy_refresh = lazy_refresh
    2.41 +
    2.42 +        channel = self.channel
    2.43 +        d1 = channel.getData(user_id='user1')
    2.44 +        self.assertEquals(d1['url'], 'http://zope.org')
    2.45 +        self.assertEquals(d1['title'], 'Zope.org')
    2.46 +        self.assertEquals(d1['description'], '')
    2.47 +
    2.48 +        # we switch the url to simulate channel contents depending on the user
    2.49 +        url1 = channel.channel_url
    2.50 +        channel.channel_url = os.path.join(self.base_url, 'zope_user2.rss')
    2.51 +        d2 = channel.getData(user_id='user2')
    2.52 +        self.assertTrue('user2' in d2['lines'][0]['title'])
    2.53 +
    2.54 +        if not lazy_refresh:
    2.55 +            return
    2.56 +
    2.57 +        # we don't switch back, but user1 content is served from cache
    2.58 +        d1_cache = channel.getData(user_id='user1')
    2.59 +        self.assertEquals(d1, d1_cache)
    2.60 +
    2.61      def testChannelWrongAuthFwd(self):
    2.62          # does not break if extended_auth is missing
    2.63 -        rss_tool = self.portal.portal_rss
    2.64 -        rss_tool.lazy_refresh = False
    2.65 -        url = os.path.abspath(os.path.join(
    2.66 -            os.path.dirname(CPSRSSTestCase.__file__), 'zope.rss'))
    2.67 -        rss_tool.manage_addRSSChannel('channel', url)
    2.68 -        chan = rss_tool.channel
    2.69 -        chan.manage_changeProperties(forward_auth=True)
    2.70 -        self.assertTrue(chan.getProperty('forward_auth'))
    2.71 -        rss_tool.refresh()
    2.72 +        self.channel.manage_changeProperties(forward_auth=True)
    2.73 +        self.assertTrue(self.channel.getProperty('forward_auth'))
    2.74 +        self.rss_tool.refresh()
    2.75 +
    2.76          # it worked anyway
    2.77 -        d = rss_tool.channel.getData()
    2.78 +        d = self.channel.getData()
    2.79          self.assertEquals(d['url'], 'http://zope.org')
    2.80          self.assertEquals(d['title'], 'Zope.org')
    2.81          self.assertEquals(d['description'], '')
    2.82 @@ -45,14 +69,17 @@
    2.83      def testChannelLazy(self):
    2.84          self._testChannel(lazy_refresh=1)
    2.85  
    2.86 +    def testPerUserChannelLazy(self):
    2.87 +        self._testPerUserChannel(lazy_refresh=1)
    2.88 +
    2.89 +    def testPerUserChannelNotLazy(self):
    2.90 +        self._testPerUserChannel(lazy_refresh=0)
    2.91 +
    2.92      def testChannelNotLazy(self):
    2.93          self._testChannel(lazy_refresh=0)
    2.94  
    2.95 -    def testPerUserCache(self):
    2.96 -        pass
    2.97 -
    2.98      def testExcHandling(self):
    2.99 -        rss_tool = self.portal.portal_rss
   2.100 +        rss_tool = self.rss_tool
   2.101          rss_tool.lazy_refresh = False
   2.102          url = 'http://nohost.cps-cms.org'
   2.103          try:
   2.104 @@ -63,7 +90,8 @@
   2.105          else:
   2.106              self.fail("URL that should not be found actually exists !")
   2.107  
   2.108 -        rss_tool.manage_addRSSChannel('channel', url)
   2.109 +        chan = self.channel
   2.110 +        chan.channel_url = url
   2.111          chan = rss_tool.channel
   2.112          chan._retrieveRSSFeed()
   2.113  
     3.1 new file mode 100644
     3.2 --- /dev/null
     3.3 +++ b/tests/zope_user2.rss
     3.4 @@ -0,0 +1,18 @@
     3.5 +<?xml version="1.0" ?>
     3.6 +<rss version="2.0">
     3.7 +
     3.8 +  <channel>
     3.9 +    <title>Zope.org</title>
    3.10 +    <link>http://zope.org</link>
    3.11 +    <language>en</language>
    3.12 +    <description></description>
    3.13 +
    3.14 +  <item>
    3.15 +       <title>For user2 : mxmCounter - A filesystem based hit counter product for Zope, CMF &amp; Plone sites</title>
    3.16 +       <link>http://zope.org/Members/maxm/news/mxmCounter_release</link>
    3.17 +       <guid>http://zope.org/Members/maxm/news/mxmCounter_release</guid>
    3.18 +       <pubDate>Thu, 26 Feb 2004 00:57:48 -0500</pubDate>
    3.19 +       <description>mxm has just released version 1.0.0 of mxmCounter. It counts hits on webpages.</description>
    3.20 +  </item>
    3.21 +  </channel>
    3.22 +</rss>