def test_expired_sessions_can_be_obtained(self):
        self.set_date('2011-09-21 01:05:10')
        control = '/tmp/stream_test'
        open(control, 'w')

        session = StreamSessionManager()

        session.add('*****@*****.**', timeout = 10)
        session.add('*****@*****.**', timeout = 20, condition = lambda: os.path.exists(control))
        session.add('*****@*****.**', timeout = 30)
        session.add('*****@*****.**', condition = lambda: os.path.exists(control))

        self.set_date('2011-09-21 01:05:11')
        expired = session.expire()
        self.assertEquals(expired, [])

        # test1 expires
        self.set_date('2011-09-21 01:05:21') 
        expired = session.expire()
        self.assertEquals(expired, ['*****@*****.**'])

        # test2 expires, even if condition is still true
        self.set_date('2011-09-21 01:05:31')
        expired = session.expire()
        self.assertEquals(expired, ['*****@*****.**'])

        # test3 expires, but it remains there because there are two sessions
        self.set_date('2011-09-21 01:05:41')
        expired = session.expire()
        self.assertEquals(expired, [])

        # condition is false, now all receivers are gone
        os.remove(control)
        expired = session.expire()
        self.assertEquals(expired, ['*****@*****.**'])
    def test_session_support_multiple_jids(self):
        self.set_date('2011-09-21 01:05:10')
        control = '/tmp/stream_test'
        open(control, 'w')

        session = StreamSessionManager()

        session.add('*****@*****.**', timeout = 10)
        session.add('*****@*****.**', timeout = 20, condition = lambda: os.path.exists(control))
        session.add('*****@*****.**', timeout = 30)
        session.add('*****@*****.**', condition = lambda: os.path.exists(control))

        self.set_date('2011-09-21 01:05:11')
        session.expire()
        self.assertEquals(len(session.receivers), 3)
        self.assertTrue('*****@*****.**' in session.receivers)
        self.assertTrue('*****@*****.**' in session.receivers)
        self.assertTrue('*****@*****.**' in session.receivers)

        # test1 expires
        self.set_date('2011-09-21 01:05:21') 
        session.expire()
        self.assertEquals(len(session.receivers), 2)
        self.assertTrue('*****@*****.**' not in session.receivers)
        self.assertTrue('*****@*****.**' in session.receivers)
        self.assertTrue('*****@*****.**' in session.receivers)

        # test2 expires, even if condition is still true
        self.set_date('2011-09-21 01:05:31')
        session.expire()
        self.assertEquals(len(session.receivers), 1)
        self.assertTrue('*****@*****.**' not in session.receivers)
        self.assertTrue('*****@*****.**' not in session.receivers)
        self.assertTrue('*****@*****.**' in session.receivers)

        # test3 expires, but it remains there because there are two sessions
        self.set_date('2011-09-21 01:05:41')
        session.expire()
        self.assertEquals(len(session.receivers), 1)
        self.assertTrue('*****@*****.**' not in session.receivers)
        self.assertTrue('*****@*****.**' not in session.receivers)
        self.assertTrue('*****@*****.**' in session.receivers)

        # condition is false, now all receivers are gone
        os.remove(control)
        session.expire()
        self.assertEquals(len(session.receivers), 0)
        self.assertTrue('*****@*****.**' not in session.receivers)
        self.assertTrue('*****@*****.**' not in session.receivers)
        self.assertTrue('*****@*****.**' not in session.receivers)
    def test_session_expires_after_timeout(self):
        self.set_date('2011-09-21 01:05:10')

        session = StreamSessionManager()

        session.add('*****@*****.**', timeout = 10)

        self.set_date('2011-09-21 01:05:11')

        session.expire()
        self.assertEquals(session.receivers, ['*****@*****.**'])
    
        self.set_date('2011-09-21 01:05:21')
        session.expire()
        self.assertEquals(session.receivers, [])
    def test_session_expires_on_given_condition(self):

        control = '/tmp/stream_test'
        open(control, 'w')
        
        session = StreamSessionManager()

        session.add('*****@*****.**', condition=lambda: os.path.exists(control))

        session.expire()
        self.assertEquals(session.receivers, ['*****@*****.**'])
        session.expire()
        self.assertEquals(session.receivers, ['*****@*****.**'])

        os.remove(control)
        session.expire()
        self.assertEquals(session.receivers, [])