Ejemplo n.º 1
0
 def setUpClass(self):
     print(' EXECUTE '.center(70, '='), file=sys.stderr)
     print('-' * 70, file=sys.stderr)
     self.srv = TarantoolServer()
     self.srv.script = 'test/suites/box.lua'
     self.srv.start()
     self.con = tarantool.Connection(self.srv.host, self.srv.args['primary'])
Ejemplo n.º 2
0
 def setUpClass(self):
     print(' DML '.center(70, '='))
     print('-' * 70)
     self.srv = TarantoolServer()
     self.srv.script = 'tests/suites/box.lua'
     self.srv.start()
     self.con = tarantool.Connection('localhost', self.srv.args['primary'])
     self.adm = self.srv.admin
     self.space_created = self.adm("box.schema.create_space('space_1')")
     self.adm("""
     box.space['space_1']:create_index('primary', {
         type = 'tree',
         parts = {1, 'num'},
         unique = true})
     """.replace('\n', ' '))
     self.adm("""
     box.space['space_1']:create_index('secondary', {
         type = 'tree',
         parts = {2, 'num', 3, 'str'},
         unique = false})
     """.replace('\n', ' '))
     self.space_created = self.adm("box.schema.create_space('space_2')")
     self.adm("""
     box.space['space_2']:create_index('primary', {
         type = 'hash',
         parts = {1, 'num'},
         unique = true})
     """.replace('\n', ' '))
     self.adm("json = require('json')")
     self.adm("fiber = require('fiber')")
     self.adm("uuid = require('uuid')")
Ejemplo n.º 3
0
    def test_07_call_17(self):
        con = tarantool.Connection(self.srv.host, self.srv.args['primary'])
        con.authenticate('test', 'test')
        self.assertSequenceEqual(con.call('json.decode', '[123, 234, 345]'),
                                 [[123, 234, 345]])
        self.assertSequenceEqual(con.call('json.decode', ['[123, 234, 345]']),
                                 [[123, 234, 345]])
        self.assertSequenceEqual(
            con.call('json.decode', ('[123, 234, 345]', )), [[123, 234, 345]])
        with self.assertRaisesRegexp(tarantool.DatabaseError, '(32, .*)'):
            con.call('json.decode')
        with self.assertRaisesRegexp(tarantool.DatabaseError, '(32, .*)'):
            con.call('json.decode', '{[1, 2]: "world"}')
        ans = con.call('fiber.time')
        self.assertEqual(len(ans), 1)
        self.assertIsInstance(ans[0], float)
        ans = con.call('fiber.time64')
        self.assertEqual(len(ans), 1)
        self.assertIsInstance(ans[0], tarantool.utils.integer_types)
        ans = con.call('uuid.str')
        self.assertEqual(len(ans), 1)
        self.assertIsInstance(ans[0], str)

        self.assertSequenceEqual(con.call('box.tuple.new', [1, 2, 3, 'fld_1']),
                                 [[1, 2, 3, 'fld_1']])
        self.assertSequenceEqual(con.call('box.tuple.new', 'fld_1'),
                                 [['fld_1']])

        con.close()
Ejemplo n.º 4
0
    def setUpClass(self):
        params = 'connection.encoding: {}'.format(repr(self.encoding))
        print(' SCHEMA ({}) '.format(params).center(70, '='), file=sys.stderr)
        print('-' * 70, file=sys.stderr)
        self.srv = TarantoolServer()
        self.srv.script = 'unit/suites/box.lua'
        self.srv.start()
        self.con = tarantool.Connection(self.srv.host,
                                        self.srv.args['primary'],
                                        encoding=self.encoding)
        self.sch = self.con.schema

        # The relevant test cases mainly target Python 2, where
        # a user may want to pass a string literal as a space or
        # an index name and don't bother whether all symbols in it
        # are ASCII.
        self.unicode_space_name_literal = '∞'
        self.unicode_index_name_literal = '→'

        self.unicode_space_name_u = u'∞'
        self.unicode_index_name_u = u'→'
        self.unicode_space_id, self.unicode_index_id = self.srv.admin("""
            do
                local space = box.schema.create_space('\\xe2\\x88\\x9e')
                local index = space:create_index('\\xe2\\x86\\x92')
                return space.id, index.id
            end
        """)
Ejemplo n.º 5
0
    def test_02_wrong_auth(self):
        # Create a connection with wrong credentials, but don't
        # connect it.
        con = tarantool.Connection(self.srv.host,
                                   self.srv.args['primary'],
                                   connect_now=False,
                                   user='******')

        # Start a server.
        self.srv.start()

        # Trigger a reconnection due to wrong credentials.
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            with self.assertRaises(tarantool.error.DatabaseError):
                con.ping()

        # Set right credentials and verify that the reconnection
        # succeeds.
        con.user = None
        self.assertIs(con.ping(notime=True), "Success")

        # Close the connection and stop the server.
        con.close()
        self.srv.stop()
Ejemplo n.º 6
0
 def test_03_set_object_hook_and_default(self):
     dt = datetime.fromtimestamp(1546300800)
     con = tarantool.Connection(self.srv.host,
                                self.srv.args['primary'],
                                unpack_object_hook=object_unpack,
                                pack_default=object_pack)
     ret = con.call("simple_return", dt)
     self.assertEqual(ret._data[0], dt)
Ejemplo n.º 7
0
 def setUpClass(self):
     print(' SCHEMA '.center(70, '='))
     print('-' * 70)
     self.srv = TarantoolServer()
     self.srv.script = 'tests/suites/box.lua'
     self.srv.start()
     self.con = tarantool.Connection('localhost', self.srv.args['primary'])
     self.sch = self.con.schema
Ejemplo n.º 8
0
def get_server():
    return tarantool.Connection("127.0.0.1",
                                3301,
                                user="******",
                                password=None,
                                socket_timeout=10,
                                reconnect_max_attempts=3,
                                reconnect_delay=1,
                                connect_now=True)
Ejemplo n.º 9
0
    def test_01_set_default(self):
        con = tarantool.Connection(self.srv.host,
                                   self.srv.args['primary'],
                                   pack_default=object_pack)

        ret = con.call("simple_return", datetime.fromtimestamp(1546300800))
        self.assertDictEqual(ret._data[0], {
            "__type__": "datetime",
            "obj": 1546300800
        })
Ejemplo n.º 10
0
 def setUpClass(self):
     print(' DBAPI '.center(70, '='), file=sys.stderr)
     print('-' * 70, file=sys.stderr)
     self.srv = TarantoolServer()
     self.srv.script = 'test/suites/box.lua'
     self.srv.start()
     self.con = tarantool.Connection(self.srv.host,
                                     self.srv.args['primary'])
     self.driver = dbapi
     self.connect_kw_args = dict(host=self.srv.host,
                                 port=self.srv.args['primary'])
Ejemplo n.º 11
0
def test_consumer():
    message1 = {"key": "test1", "value": "test1"}

    message2 = {"key": "test1", "value": "test2"}

    message3 = {"key": "test1", "value": "test3"}

    loop = asyncio.get_event_loop()

    async def send():
        producer = AIOKafkaProducer(loop=loop,
                                    bootstrap_servers='localhost:9092')
        # Get cluster layout and initial topic/partition leadership information
        await producer.start()
        try:
            # Produce message
            for msg in (message1, message2, message3):
                await producer.send_and_wait(
                    "test_consumer",
                    value=msg['value'].encode('utf-8'),
                    key=msg['key'].encode('utf-8'))

        finally:
            # Wait for all pending messages to be delivered or expire.
            await producer.stop()

    loop.run_until_complete(send())

    server = tarantool.Connection("127.0.0.1",
                                  3301,
                                  user="******",
                                  password=None,
                                  socket_timeout=20,
                                  reconnect_max_attempts=3,
                                  reconnect_delay=1,
                                  connect_now=True)

    attempts = 0
    while True:
        try:
            response = server.call("consumer.consume", ())
        # tarantool in docker sometimes stacks
        except:
            attempts += 1
            if attempts < 3:
                continue
            else:
                assert True is False
        else:
            break

    assert set(*response) == {"test1", "test2", "test3"}
Ejemplo n.º 12
0
    def test_00_not_set(self):
        con = tarantool.Connection(self.srv.host, self.srv.args['primary'])

        ret = con.call("simple_return", {
            "__type__": "datetime",
            "obj": 1546300800
        })
        self.assertDictEqual(ret._data[0], {
            "__type__": "datetime",
            "obj": 1546300800
        })

        with self.assertRaises(TypeError):
            con.call("simple_return", datetime.fromtimestamp(1546300800))
Ejemplo n.º 13
0
    def test_03_connect_after_close(self):
        # Start a server and connect to it.
        self.srv.start()
        con = tarantool.Connection(self.srv.host, self.srv.args['primary'])
        con.ping()

        # Close the connection and connect again.
        con.close()
        con.connect()

        # Verify that the connection is alive.
        con.ping()

        # Close the connection and stop the server.
        con.close()
        self.srv.stop()
Ejemplo n.º 14
0
    def test_02_set_object_hook(self):
        con = tarantool.Connection(self.srv.host,
                                   self.srv.args['primary'],
                                   unpack_object_hook=object_unpack)

        ret = con.call("simple_return", {
            "__type__": "datetime",
            "obj": 1546300800
        })
        self.assertEqual(ret._data[0], datetime.fromtimestamp(1546300800))

        ret = con.call("simple_return", {
            "__type__": "1datetime",
            "obj": 1546300800
        })
        self.assertEqual(ret._data[0], {
            "__type__": "1datetime",
            "obj": 1546300800
        })
Ejemplo n.º 15
0
    def test_01_simple(self):
        # Create a connection, but don't connect it.
        con = tarantool.Connection(self.srv.host, self.srv.args['primary'],
                                   connect_now=False)

        # Trigger a reconnection due to server unavailability.
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            with self.assertRaises(tarantool.error.NetworkError):
                con.ping()

        # Start a server and verify that the reconnection
        # succeeds.
        self.srv.start()
        self.assertIs(con.ping(notime=True), "Success")

        # Close the connection and stop the server.
        con.close()
        self.srv.stop()
Ejemplo n.º 16
0
 def __init__(self,
              host="localhost",
              port=3301,
              user=None,
              password=None,
              reconnect_n=10,
              reconnect_delay=1,
              timeout=5,
              log=None):
     self.host = host
     self.port = port
     self.reconnect_n = reconnect_n
     self.reconnect_delay = reconnect_delay
     self.connection = tarantool.Connection(host,
                                            port,
                                            user=user,
                                            password=password,
                                            connect_now=False,
                                            connection_timeout=timeout)
     self.log = log or logging
Ejemplo n.º 17
0
    def _refresh_connection(self):
        if self.db:
            try:
                ping = self.db.ping()
            except NetworkError as e:
                logging.error('tarantool connection error: %s', e.message)
                self.db = None

        if not self.db:
            try:
                self.db = tarantool.Connection(
                    self._host,
                    self._port,
                    user=self._user,
                    password=self._password,
                    socket_timeout=self._connection_timeout,
                    reconnect_max_attempts=self._reconnect_max_attempts,
                    reconnect_delay=self._reconnect_delay)
            except tarantool.error.NetworkError as e:
                logging.error('tarantool connection error: %s', e.message)
                self.db = None
                raise StoreError('tarantool error: %s', e.message)
 def __init__(
     self,
     space,
     host=SERVER_HOST,
     port=SERVER_PORT,
     user=None,
     password=None,
     reconnect_max_attempts=RECONNECT_MAX_ATTEMPTS,
     reconnect_delay=RECONNECT_DELAY,
     connect_now=False,
     connection_timeout=CONNECTION_TIMEOUT,
 ):
     self.connection = tarantool.Connection(
         host=host,
         port=port,
         user=user,
         password=password,
         reconnect_max_attempts=reconnect_max_attempts,
         reconnect_delay=reconnect_delay,
         connect_now=False,
         connection_timeout=connection_timeout,
     )
     self.space = space
Ejemplo n.º 19
0
def bench_tarantool(n, b):
    import tarantool

    conn = tarantool.Connection(host='127.0.0.1',
                                port=3305,
                                user='******',
                                password='******')
    conn.connect()
    b = 1
    n_requests_per_bulk = int(math.ceil(n / b))

    start = datetime.datetime.now()
    for _ in range(n_requests_per_bulk):
        # conn.ping()
        # await conn.call('test')
        # await conn.eval('return "hello"')
        conn.select(512)
        # await conn.replace('tester', [2, 'hhhh'])
        # await conn.update('tester', [2], [(':', 1, 1, 3, 'yo!')])

    end = datetime.datetime.now()
    elapsed = end - start
    print('Elapsed: {}, RPS: {}'.format(elapsed, n / elapsed.total_seconds()))