def test_load_existing_session(self):
        factory = create_session_factory(self.dummy_sid, self.dummy_storage)
        request = mock.Mock()

        self.dummy_sid.get_session_id.return_value = 123

        @asyncio.coroutine
        def load(session_id):
            self.assertEqual(session_id, 123)
            return ({'foo': 'bar'}, 321)

        self.dummy_storage.load_session_data.side_effect = load

        @asyncio.coroutine
        def go():
            waiter = asyncio.Future(loop=self.loop)
            factory(request, waiter, loop=self.loop)
            sess = yield from asyncio.wait_for(waiter,
                                               timeout=1,
                                               loop=self.loop)

            self.assertEqual(dict(sess), {'foo': 'bar'})
            self.dummy_sid.get_session_id.assert_called_once_with(request)
            request.add_response_callback.assert_called_once_with(
                factory._save, session=sess)

        self.loop.run_until_complete(go())
        self.dummy_storage.save_session_data.assert_call_count(0)
    def test_load_existing_session(self):
        factory = create_session_factory(self.dummy_sid, self.dummy_storage)
        request = mock.Mock()

        self.dummy_sid.get_session_id.return_value = 123

        @asyncio.coroutine
        def load(session_id):
            self.assertEqual(session_id, 123)
            return ({'foo': 'bar'}, 321)

        self.dummy_storage.load_session_data.side_effect = load

        @asyncio.coroutine
        def go():
            waiter = asyncio.Future(loop=self.loop)
            factory(request, waiter, loop=self.loop)
            sess = yield from asyncio.wait_for(waiter, timeout=1,
                                               loop=self.loop)

            self.assertEqual(dict(sess), {'foo': 'bar'})
            self.dummy_sid.get_session_id.assert_called_once_with(request)
            request.add_response_callback.assert_called_once_with(
                factory._save, session=sess)

        self.loop.run_until_complete(go())
        self.dummy_storage.save_session_data.assert_call_count(0)
    def test_create_new_session(self):
        factory = create_session_factory(self.dummy_sid, self.dummy_storage)

        @asyncio.coroutine
        def load(session_id):
            return (None, None)

        self.dummy_storage.load_session_data.side_effect = load

        @asyncio.coroutine
        def go():
            waiter = asyncio.Future(loop=self.loop)
            req = mock.Mock()

            factory(req, waiter, loop=self.loop)

            sess = yield from asyncio.wait_for(waiter,
                                               timeout=1,
                                               loop=self.loop)

            self.assertIsInstance(sess, Session)
            self.assertTrue(sess.new)
            self.assertIsNone(sess.identity)
            req.add_response_callback.assert_called_once_with(factory._save,
                                                              session=sess)

        self.loop.run_until_complete(go())

        self.dummy_storage.save_session_data.assert_call_count(0)
    def test_create_new_session(self):
        factory = create_session_factory(self.dummy_sid, self.dummy_storage)

        @asyncio.coroutine
        def load(session_id):
            return (None, None)

        self.dummy_storage.load_session_data.side_effect = load

        @asyncio.coroutine
        def go():
            waiter = asyncio.Future(loop=self.loop)
            req = mock.Mock()

            factory(req, waiter, loop=self.loop)

            sess = yield from asyncio.wait_for(waiter, timeout=1,
                                               loop=self.loop)

            self.assertIsInstance(sess, Session)
            self.assertTrue(sess.new)
            self.assertIsNone(sess.identity)
            req.add_response_callback.assert_called_once_with(
                factory._save, session=sess)

        self.loop.run_until_complete(go())

        self.dummy_storage.save_session_data.assert_call_count(0)
 def test_asserts(self):
     with self.assertRaises(AssertionError):
         create_session_factory(None, self.dummy_storage)
     with self.assertRaises(AssertionError):
         create_session_factory(self.dummy_sid, None)
    def test_global_event_loop(self):
        asyncio.set_event_loop(self.loop)

        factory = create_session_factory(self.dummy_sid, self.dummy_storage)
        self.assertIsInstance(factory, _SessionFactory)
 def test_asserts(self):
     with self.assertRaises(AssertionError):
         create_session_factory(None, self.dummy_storage)
     with self.assertRaises(AssertionError):
         create_session_factory(self.dummy_sid, None)
    def test_global_event_loop(self):
        asyncio.set_event_loop(self.loop)

        factory = create_session_factory(self.dummy_sid, self.dummy_storage)
        self.assertIsInstance(factory, _SessionFactory)