def test_hbase_usage_with_transaction(self):
        local_config = DotDict({
            'hbase_host': 'host',
            'database_name': 'name',
            'hbase_port': 9090,
            'hbase_timeout': 9000,
            'number_of_retries': 2,
            'logger': SilentFakeLogger(),
            'executor_identity': lambda: 'dwight'  # bogus thread id
        })
        a_fake_hbase_connection = FakeHB_Connection(local_config)
        with mock.patch.object(
                connection_context, 'HBaseConnection',
                mock.Mock(return_value=a_fake_hbase_connection)):
            hb_context = connection_context.HBasePooledConnectionContext(
                local_config)

            def all_ok(connection, dummy):
                eq_(dummy, 'hello')
                return True

            transaction = TransactionExecutor(local_config, hb_context)
            result = transaction(all_ok, 'hello')
            ok_(result)
            eq_(a_fake_hbase_connection.close_counter, 0)
            eq_(a_fake_hbase_connection.rollback_counter, 0)
            eq_(a_fake_hbase_connection.commit_counter, 1)

            def bad_deal(connection, dummy):
                raise KeyError('fred')

            assert_raises(KeyError, transaction, bad_deal, 'hello')
            # at this point, the underlying connection has been deleted from
            # the pool, because it was considered to be a bad connection.
            eq_(a_fake_hbase_connection.close_counter, 0)
            eq_(a_fake_hbase_connection.commit_counter, 1)

            hb_context.close()
            # because the connection was previously deleted from the pool,
            # no connection gets closed at this point.
            eq_(a_fake_hbase_connection.close_counter, 0)
Beispiel #2
0
    def test_hbase_usage_with_transaction(self, mocked_hbcl):
        local_config = DotDict({
            'hbase_host': 'host',
            'database_name': 'name',
            'hbase_port': 9090,
            'hbase_timeout': 9000,
            'number_of_retries': 2,
            'logger': SilentFakeLogger(),
        })
        a_fake_hbase_connection = FakeHB_Connection()
        mocked_hbcl.HBaseConnectionForCrashReports = \
            mock.Mock(return_value=a_fake_hbase_connection)
        hb_context = HBaseConnectionContextPooled(local_config, local_config)

        def all_ok(connection, dummy):
            self.assertEqual(dummy, 'hello')
            return True

        transaction = TransactionExecutor(local_config, hb_context)
        result = transaction(all_ok, 'hello')
        self.assertTrue(result)
        self.assertEqual(mocked_hbcl.HBaseConnectionForCrashReports.call_count,
                         2)
        self.assertEqual(a_fake_hbase_connection.close_counter, 1)
        self.assertEqual(a_fake_hbase_connection.rollback_counter, 0)
        self.assertEqual(a_fake_hbase_connection.commit_counter, 1)

        def bad_deal(connection, dummy):
            raise KeyError('fred')

        self.assertRaises(KeyError, transaction, bad_deal, 'hello')
        self.assertEqual(mocked_hbcl.HBaseConnectionForCrashReports.call_count,
                         2)
        self.assertEqual(a_fake_hbase_connection.close_counter, 1)
        self.assertEqual(a_fake_hbase_connection.rollback_counter, 1)
        self.assertEqual(a_fake_hbase_connection.commit_counter, 1)

        hb_context.close()
        self.assertEqual(a_fake_hbase_connection.close_counter, 2)
    def test_hbase_usage_with_transaction(self):
        local_config = DotDict({
            'hbase_host': 'host',
            'database_name': 'name',
            'hbase_port': 9090,
            'hbase_timeout': 9000,
            'number_of_retries': 2,
            'logger': SilentFakeLogger(),
            'executor_identity': lambda: 'dwight'  # bogus thread id
        })
        a_fake_hbase_connection = FakeHB_Connection(local_config)
        with mock.patch.object(
                connection_context, 'HBaseConnection',
                mock.Mock(return_value=a_fake_hbase_connection)):
            hb_context = connection_context.HBaseConnectionContext(
                local_config)

            def all_ok(connection, dummy):
                eq_(dummy, 'hello')
                return True

            transaction = TransactionExecutor(local_config, hb_context)
            result = transaction(all_ok, 'hello')
            ok_(result)
            eq_(a_fake_hbase_connection.close_counter, 1)
            eq_(a_fake_hbase_connection.rollback_counter, 0)
            eq_(a_fake_hbase_connection.commit_counter, 1)

            def bad_deal(connection, dummy):
                raise KeyError('fred')

            assert_raises(KeyError, transaction, bad_deal, 'hello')
            eq_(a_fake_hbase_connection.close_counter, 2)
            eq_(a_fake_hbase_connection.commit_counter, 1)

            hb_context.close()
            eq_(a_fake_hbase_connection.close_counter, 2)
Beispiel #4
0
    def test_hbase_usage_with_transaction(self):
        local_config = DotDict({
            'hbase_host': 'host',
            'database_name': 'name',
            'hbase_port': 9090,
            'hbase_timeout': 9000,
            'number_of_retries': 2,
            'logger': SilentFakeLogger(),
        })
        a_fake_hbase_connection = FakeHB_Connection2(local_config)
        a_fake_hbase_pool = mock.MagicMock()
        a_fake_hbase_pool.connection = a_fake_hbase_connection
        with mock.patch.object(happybase, 'ConnectionPool',
                               mock.Mock(return_value=a_fake_hbase_pool)):
            hb_context = HappyBasePooledConnectionContextMock(local_config)

            def all_ok(connection, dummy):
                self.assertEqual(dummy, 'hello')
                return True

            transaction = TransactionExecutor(local_config, hb_context)
            result = transaction(all_ok, 'hello')
            self.assertTrue(result)
            self.assertEqual(a_fake_hbase_connection.close_counter, 0)
            self.assertEqual(a_fake_hbase_connection.rollback_counter, 0)
            self.assertEqual(a_fake_hbase_connection.commit_counter, 1)

            def bad_deal(connection, dummy):
                raise KeyError('fred')

            self.assertRaises(KeyError, transaction, bad_deal, 'hello')
            self.assertEqual(a_fake_hbase_connection.close_counter, 0)
            self.assertEqual(a_fake_hbase_connection.commit_counter, 1)

            hb_context.close()
            self.assertEqual(a_fake_hbase_connection.close_counter, 0)