Exemplo n.º 1
0
    def test_send_string(self):
        from pyramid_sockjs import Session, protocol
        session = Session('id')
        session.manager = _SM()

        session.send('message')
        self.assertEqual(session.queue.get(), 'message')
Exemplo n.º 2
0
    def test_send_string(self):
        from pyramid_sockjs import Session, protocol
        session = Session('id')
        session.manager = _SM()

        session.send('message')
        self.assertEqual(session.queue.get(), 'message')
Exemplo n.º 3
0
    def test_expire(self):
        from pyramid_sockjs import Session
        session = Session('id')

        session.expired = False

        session.expire()
        self.assertTrue(session.expired)
Exemplo n.º 4
0
    def test_expire(self):
        from pyramid_sockjs import Session
        session = Session('id')

        session.expired = False

        session.expire()
        self.assertTrue(session.expired)
Exemplo n.º 5
0
    def test_send_tick(self):
        from pyramid_sockjs import Session, protocol
        session = Session('id')
        session.manager = _SM()

        self.now = self.now + timedelta(hours=1)

        session.send(['message'])
        self.assertEqual(session.expires, self.now + session.timeout)
Exemplo n.º 6
0
    def test_send_tick(self):
        from pyramid_sockjs import Session, protocol
        session = Session('id')
        session.manager = _SM()

        self.now = self.now + timedelta(hours=1)

        session.send(['message'])
        self.assertEqual(session.expires, self.now + session.timeout)
Exemplo n.º 7
0
    def test_get_transport_message(self):
        from pyramid_sockjs import Session
        session = Session('id')
        session.manager = _SM()

        session.send('message')
        self.assertEqual(session.get_transport_message(), 'message')

        from gevent.queue import Empty

        self.assertRaises(Empty, session.get_transport_message, timeout=0.1)
Exemplo n.º 8
0
    def test_broadcast(self):
        Session, sm = self.make_one()

        s1 = Session('s1')
        s2 = Session('s2')
        sm._add(s1)
        sm._add(s2)

        sm.broadcast('msg')
        self.assertEqual(s1.get_transport_message(), 'msg')
        self.assertEqual(s2.get_transport_message(), 'msg')
Exemplo n.º 9
0
    def test_get_transport_message(self):
        from pyramid_sockjs import Session
        session = Session('id')
        session.manager = _SM()

        session.send('message')
        self.assertEqual(session.get_transport_message(), 'message')

        from gevent.queue import Empty

        self.assertRaises(
            Empty, session.get_transport_message, timeout=0.1)
Exemplo n.º 10
0
    def test_get_transport_message_tick(self):
        from pyramid_sockjs import Session
        session = Session('id')
        session.manager = _SM()

        session.send('message')

        self.now = self.now + timedelta(hours=1)

        session.get_transport_message()

        self.assertEqual(session.expires, self.now + session.timeout)
Exemplo n.º 11
0
    def test_get_transport_message_tick(self):
        from pyramid_sockjs import Session
        session = Session('id')
        session.manager = _SM()

        session.send('message')

        self.now = self.now + timedelta(hours=1)

        session.get_transport_message()

        self.assertEqual(session.expires, self.now + session.timeout)
Exemplo n.º 12
0
    def test_active_sessions(self):
        Session, sm = self.make_one()

        s1 = Session('s1')
        s2 = Session('s2')

        sm._add(s1)
        sm._add(s2)

        s2.expire()

        active = list(sm.active_sessions())
        self.assertEqual(len(active), 1)
        self.assertIn(s1, active)
Exemplo n.º 13
0
    def test_active_sessions(self):
        Session, sm = self.make_one()

        s1 = Session('s1')
        s2 = Session('s2')

        sm._add(s1)
        sm._add(s2)

        s2.expire()

        active = list(sm.active_sessions())
        self.assertEqual(len(active), 1)
        self.assertIn(s1, active)
Exemplo n.º 14
0
    def test_fresh(self):
        Session, sm = self.make_one()

        sm._add(Session('id'))
        sm._gc()

        self.assertIn('id', sm)
Exemplo n.º 15
0
    def test_add_expired(self):
        Session, sm = self.make_one()

        session = Session('id')
        session.expire()

        self.assertRaises(ValueError, sm._add, session)
Exemplo n.º 16
0
 def test_closed(self):
     from pyramid_sockjs import Session, STATE_CLOSED
     session = Session('id')
     session.open()
     session.closed()
     self.assertTrue(session.expired)
     self.assertEqual(session.state, STATE_CLOSED)
Exemplo n.º 17
0
    def test_tick(self):
        from pyramid_sockjs import Session
        session = Session('id')

        self.now = self.now + timedelta(hours=1)
        session.tick()
        self.assertEqual(session.expires, self.now + session.timeout)
Exemplo n.º 18
0
    def test_tick_custom(self):
        from pyramid_sockjs import Session
        session = Session('id', timedelta(seconds=20))

        self.now = self.now + timedelta(hours=1)
        session.tick(timedelta(seconds=30))
        self.assertEqual(session.expires, self.now + timedelta(seconds=30))
Exemplo n.º 19
0
    def test_acquire_locked(self):
        Session, sm = self.make_one()
        session = Session('id')
        sm._add(session)

        s = sm.acquire(session)

        self.assertRaises(KeyError, sm.acquire, session)
Exemplo n.º 20
0
    def test_ctor(self):
        from pyramid_sockjs import Session, STATE_NEW
        session = Session('id')

        self.assertEqual(session.id, 'id')
        self.assertEqual(session.expired, False)
        self.assertEqual(session.expires, self.now + timedelta(seconds=10))

        self.assertEqual(session.hits, 0)
        self.assertEqual(session.heartbeats, 0)
        self.assertEqual(session.state, STATE_NEW)

        session = Session('id', timedelta(seconds=15))

        self.assertEqual(session.id, 'id')
        self.assertEqual(session.expired, False)
        self.assertEqual(session.expires, self.now + timedelta(seconds=15))
Exemplo n.º 21
0
    def test_str(self):
        from pyramid_sockjs import Session, STATE_OPEN, STATE_CLOSING
        session = Session('test')
        session.hits = 10
        session.heartbeats = 50
        session.state = STATE_OPEN

        self.assertEqual(str(session),
                         "id='test' connected hits=10 heartbeats=50")

        session.state = STATE_CLOSING
        self.assertEqual(str(session),
                         "id='test' disconnected hits=10 heartbeats=50")

        session.send('msg')
        self.assertEqual(
            str(session),
            "id='test' disconnected queue[1] hits=10 heartbeats=50")
Exemplo n.º 22
0
    def test_gc_removed(self):
        Session, sm = self.make_one()

        sm._add(Session('id'))
        del sm['id']

        self.assertEqual(len(sm.pool), 1)
        sm._gc()

        self.assertEqual(len(sm.pool), 0)
Exemplo n.º 23
0
    def test_gc_one_expire(self):
        Session, sm = self.make_one()

        session1 = Session('id1')
        session1.open()

        session2 = Session('id2')
        session2.open()

        sm._add(session1)
        sm._add(session2)

        self.now = session1.expires + timedelta(seconds=10)

        session2.tick()

        sm._gc()
        self.assertNotIn('id1', sm)
        self.assertIn('id2', sm)
Exemplo n.º 24
0
    def test_add(self):
        Session, sm = self.make_one()
        session = Session('id')

        sm._add(session)
        self.assertIn('id', sm)
        self.assertIs(sm['id'], session)
        self.assertIs(sm.pool[0][1], session)
        self.assertIs(session.manager, sm)
        self.assertIs(session.registry, sm.registry)
Exemplo n.º 25
0
    def test_heartbeat(self):
        from pyramid_sockjs import Session
        session = Session('id')

        self.assertEqual(session.heartbeats, 0)

        session.heartbeat()
        self.assertEqual(session.heartbeats, 1)

        session.heartbeat()
        self.assertEqual(session.heartbeats, 2)
Exemplo n.º 26
0
    def test_gc_expire(self):
        from pyramid_sockjs import STATE_CLOSED
        Session, sm = self.make_one()

        session = Session('id')
        session.open()

        sm._add(session)

        self.now = session.expires + timedelta(seconds=10)

        sm._gc()
        self.assertNotIn('id', sm)
        self.assertTrue(session.expired)
        self.assertEqual(session.state, STATE_CLOSED)
Exemplo n.º 27
0
    def test_broadcast(self):
        Session, sm = self.make_one()

        s1 = Session('s1')
        s2 = Session('s2')
        sm._add(s1)
        sm._add(s2)

        sm.broadcast('msg')
        self.assertEqual(s1.get_transport_message(), 'msg')
        self.assertEqual(s2.get_transport_message(), 'msg')
Exemplo n.º 28
0
    def test_gc_one_expire(self):
        Session, sm = self.make_one()

        session1 = Session('id1')
        session1.open()

        session2 = Session('id2')
        session2.open()

        sm._add(session1)
        sm._add(session2)

        self.now = session1.expires + timedelta(seconds=10)

        session2.tick()

        sm._gc()
        self.assertNotIn('id1', sm)
        self.assertIn('id2', sm)
Exemplo n.º 29
0
    def test_acquire(self):
        Session, sm = self.make_one()

        session = Session('id')
        sm._add(session)

        self.now = self.now + timedelta(hours=1)

        request = object()

        s = sm.acquire(session, request=request)

        self.assertIs(s, session)
        self.assertIs(s.request, request)
        self.assertIn('id', sm.acquired)
        self.assertTrue(sm.acquired['id'])
        self.assertTrue(sm.is_acquired(s))
        self.assertEqual(session.expires, self.now + timedelta(seconds=10))
Exemplo n.º 30
0
    def test_clear(self):
        from pyramid_sockjs.session import STATE_CLOSED

        Session, sm = self.make_one()

        s1 = Session('s1')
        s1.open()
        s2 = Session('s2')
        s2.open()

        sm._add(s1)
        sm._add(s2)

        sm.clear()

        self.assertFalse(bool(sm))
        self.assertTrue(s1.expired)
        self.assertTrue(s2.expired)
        self.assertEqual(s1.state, STATE_CLOSED)
        self.assertEqual(s2.state, STATE_CLOSED)
Exemplo n.º 31
0
    def test_str(self):
        from pyramid_sockjs import Session, STATE_OPEN, STATE_CLOSING
        session = Session('test')
        session.hits = 10
        session.heartbeats = 50
        session.state = STATE_OPEN
        session.manager = _SM()

        self.assertEqual(str(session),
                         "id='test' connected hits=10 heartbeats=50")

        session.state = STATE_CLOSING
        self.assertEqual(str(session),
                         "id='test' disconnected hits=10 heartbeats=50")

        session.send('msg')
        self.assertEqual(
            str(session),
            "id='test' disconnected queue[1] hits=10 heartbeats=50")
Exemplo n.º 32
0
    def test_clear(self):
        from pyramid_sockjs.session import STATE_CLOSED

        Session, sm = self.make_one()

        s1 = Session('s1')
        s1.open()
        s2 = Session('s2')
        s2.open()

        sm._add(s1)
        sm._add(s2)

        sm.clear()

        self.assertFalse(bool(sm))
        self.assertTrue(s1.expired)
        self.assertTrue(s2.expired)
        self.assertEqual(s1.state, STATE_CLOSED)
        self.assertEqual(s2.state, STATE_CLOSED)
Exemplo n.º 33
0
 def test_open(self):
     from pyramid_sockjs import Session, STATE_OPEN
     session = Session('id')
     session.open()
     self.assertEqual(session.state, STATE_OPEN)
Exemplo n.º 34
0
    def test_acquire_unknown(self):
        Session, sm = self.make_one()
        session = Session('id')

        self.assertRaises(KeyError, sm.acquire, session)
Exemplo n.º 35
0
    def test_get(self):
        Session, sm = self.make_one()
        session = Session('id')

        sm._add(session)
        self.assertIs(sm.get('id'), session)