From 59f8de2d854003889f5e045a1dd8a9f2ea7f6fef Mon Sep 17 00:00:00 2001
From: Eugine Blikh <bigbes@gmail.com>
Date: Mon, 2 Sep 2013 17:53:51 +0400
Subject: [PATCH] Add consistent lua test to replication

---
 test/lib/tarantool_server.py          |  39 ++++---
 test/replication/consistent.test.lua  | 153 ++++++++++++++++++++++++++
 test/replication/hot_standby.test.lua |  80 ++++++--------
 3 files changed, 212 insertions(+), 60 deletions(-)
 create mode 100644 test/replication/consistent.test.lua

diff --git a/test/lib/tarantool_server.py b/test/lib/tarantool_server.py
index 9fdf257de4..aeaa1e5b26 100644
--- a/test/lib/tarantool_server.py
+++ b/test/lib/tarantool_server.py
@@ -170,23 +170,23 @@ class LuaTest(FuncTest):
                     try:
                         if matched3.group(1) == 'create':
                             name, namecon = re.match("(.*)\s+to\s+(.*)", matched3.group(2)).groups()
-                            self.suite_ini['connection'][namecon] = AdminConnection('localhost', self.suite_ini['server'][name].port)
+                            self.suite_ini['connections'][namecon] = AdminConnection('localhost', self.suite_ini['server'][name].port)
                         elif matched3.group(1) == 'drop':
                             name = matched3.group(2)
-                            if name in self.suite_ini['connections']:
-                                if self.suite_ini['connections'][name].is_connected:
-                                    self.suite_ini['connection'][name].disconnect()
-                                self.suite_ini['connection'].pop(name)
+                            if name in self.suite_ini['connections'] and not self.suite_ini['connections'] is curcon:
+                                self.suite_ini['connections'][name].disconnect()
+                                self.suite_ini['connections'].pop(name)
                             else:
                                 raise LuaPreprocessorException("Wrong connection name: " + name)
                         elif matched3.group(1) == 'set':
-                            name = matched3.group(2)
-                            if name in self.suite_ini['connections']:
-                                if not self.suite_ini['connections'][name].is_connected:
-                                    self.suite_ini['connections'][name].connect()
-                                curcon = self.suite_ini['connections'][name]
+                            name = re.split(",\s*", matched3.group(2))
+                            for _name in name:
+                                if not _name in self.suite_ini['connections']:
+                                    raise LuaPreprocessorException("Wrong connection name: " + _name)
+                            if len(name) == 1:
+                                curcon = self.suite_ini['connections'][name[0]]
                             else:
-                                raise LuaPreprocessorException("Wrong connection name: " + name)
+                                curcon = [self.suite_ini['connections'][_name] for _name in name]
                         else:
                             raise LuaPreprocessorException("Wrong command for connection - " + repr(matched3.group(1)))
                     except (AttributeError, ValueError) as e:
@@ -212,17 +212,25 @@ class LuaTest(FuncTest):
             else:
                 if not delimiter:
                     if line.strip():
-                        server.admin(line.strip())
+                        curcon(line.strip())
                     continue
                 cmd.write(line)
                 if cmd.getvalue().endswith(delimiter + '\n') and cmd.getvalue():
-                    res = curcon(cmd.getvalue()[:-len(delimiter)].replace('\n\n', '\n'), silent=True)
+                    if isinstance(curcon, list):
+                        for con in curcon:
+                            res = con(cmd.getvalue()[:-len(delimiter)].replace('\n\n', '\n'), silent=True)
+                    else:
+                        res = curcon(cmd.getvalue()[:-len(delimiter)].replace('\n\n', '\n'), silent=True)
                     sys.stdout.write(cmd.getvalue()[:-1].strip() + '\n')
                     sys.stdout.write(res.replace("\r\n", "\n"))
                     cmd.close()
                     cmd = None
-        if cmd and cmd.getvalue():
-            res = curcon(cmd.getvalue()[:-len(delimiter)].replace('\n\n', '\n'), silent=True)
+        if cmd and cmd.getvalue().strip():
+            if isinstance(curcon, list):
+                for con in curcon:
+                    res = con(cmd.getvalue()[:-len(delimiter)].replace('\n\n', '\n'), silent=True)
+            else:
+                res = curcon(cmd.getvalue()[:-len(delimiter)].replace('\n\n', '\n'), silent=True)
             sys.stdout.write(cmd.getvalue()[:-1].strip() + '\n')
             sys.stdout.write(res.replace("\r\n", "\n"))
             cmd.close
@@ -373,6 +381,7 @@ class TarantoolServer(Server):
             os.unlink(os.path.join(self.vardir, self.default_config_name))
         else:
             self.config = os.path.abspath(config)
+            print self.config
             shutil.copy(self.config, os.path.join(self.vardir, self.default_config_name))
         self.admin.execute("box.cfg.reload()", silent=silent)
 
diff --git a/test/replication/consistent.test.lua b/test/replication/consistent.test.lua
new file mode 100644
index 0000000000..75e4db9ad0
--- /dev/null
+++ b/test/replication/consistent.test.lua
@@ -0,0 +1,153 @@
+--# create server replica with configuration='replication/cfg/replica.cfg'
+--# start server replica
+--# setopt delimiter ';'
+--# set connection default
+box.replace(box.schema.SPACE_ID, 0, 0, 'tweedledum');
+box.replace(box.schema.INDEX_ID, 0, 0, 'primary', 'hash', 1, 1, 0, 'num');
+
+--# set connection default, replica
+function _insert(_begin, _end, msg) 
+    a = {}
+    for i = _begin, _end do
+        table.insert(a, box.insert(0, i, msg..' - '..i))
+    end
+    return unpack(a)
+end;
+
+function _select(_begin, _end)
+    a = {}
+    while box.info.lsn < _end + 3 do
+        box.fiber.sleep(0.001)
+    end
+    for i = _begin, _end do
+        table.insert(a, box.select(0, 0, i))
+    end
+    return unpack(a)
+end;
+
+--# setopt delimiter ''
+
+--# set connection default
+_insert(1, 10, 'master')
+_select(1, 10)
+--# set connection replica
+_select(1, 10)
+
+--# set connection default
+-- Master LSN:
+box.info.lsn
+--# set connection replica 
+-- Replica LSN:
+box.info.lsn
+
+-----------------------------
+--  Master LSN > Replica LSN
+-----------------------------
+--------------------
+-- Replica to Master
+--------------------
+--# reconfigure server replica with configuration 'replication/cfg/replica_to_master.cfg'
+--# set connection default
+_insert(11, 20, 'master')
+_select(11, 20)
+--# set connection replica
+_insert (11, 15, 'replica')
+_select (11, 15)
+
+--# set connection default
+-- Master LSN:
+box.info.lsn
+--# set connection replica 
+-- Replica LSN:
+box.info.lsn
+
+-------------------
+-- rollback Replica
+-------------------
+--# reconfigure server replica with configuration='replication/cfg/replica.cfg'
+_select(11, 20)
+--# set connection default
+-- Master LSN:
+box.info.lsn
+--# set connection replica 
+-- Replica LSN:
+box.info.lsn
+
+------------------------------
+--  Master LSN == Replica LSN
+------------------------------
+--------------------
+-- Replica to Master
+--------------------
+--# reconfigure server replica with configuration='replication/cfg/replica_to_master.cfg'
+--# set connection default
+_insert(21, 30, 'master')
+_select(21, 30)
+--# set connection replica
+_insert(21, 30, 'replica')
+_select(21, 30)
+
+--# set connection default
+-- Master LSN:
+box.info.lsn
+--# set connection replica 
+-- Replica LSN:
+box.info.lsn
+
+-------------------
+-- rollback Replica
+-------------------
+--# reconfigure server replica with configuration='replication/cfg/replica.cfg'
+_select(21, 30)
+
+--# set connection default
+-- Master LSN:
+box.info.lsn
+--# set connection replica 
+-- Replica LSN:
+box.info.lsn
+
+-----------------------------
+--  Master LSN < Replica LSN
+-----------------------------
+--------------------
+-- Replica to Master
+--------------------
+--# reconfigure server replica with configuration='replication/cfg/replica_to_master.cfg'
+--# set connection default
+_insert(31, 40, 'master')
+_select(31, 40)
+--# set connection replica
+_insert(31, 50, 'replica')
+_select(31, 50)
+
+--# set connection default
+-- Master LSN:
+box.info.lsn
+--# set connection replica 
+-- Replica LSN:
+box.info.lsn
+
+-------------------
+-- rollback Replica
+-------------------
+--# reconfigure server replica with configuration='replication/cfg/replica.cfg'
+_select(31, 50)
+--# set connection default
+_insert(41, 60, 'master')
+--# set connection replica
+_select(41, 60)
+
+--# set connection default
+-- Master LSN:
+box.info.lsn
+--# set connection replica 
+-- Replica LSN:
+box.info.lsn
+
+-- Test that a replica replies with master connection URL on update request
+box.insert(0, 0, 'replica is RO')
+--# stop server replica
+--# cleanup server replica
+--# set connection default
+box.space[0]:drop();
diff --git a/test/replication/hot_standby.test.lua b/test/replication/hot_standby.test.lua
index 44ef0d5669..dd20a5b4f9 100644
--- a/test/replication/hot_standby.test.lua
+++ b/test/replication/hot_standby.test.lua
@@ -2,62 +2,52 @@
 --# create server replica with configuration='replication/cfg/replica.cfg'
 --# start server hot_standby
 --# start server replica
---# setopt delimiter ';'
-box.replace(box.schema.SPACE_ID, 0, 0, 'tweedledum');
-box.replace(box.schema.INDEX_ID, 0, 0, 'primary', 'hash', 1, 1, 0, 'num');
-
-a = {}
-for i = 1, 10 do
-    table.insert(a, box.insert(0, i, 'the tuple '..tostring(i)))
-end
-return a;
-
-a = {}
-for i = 1, 10 do
-    table.insert(a, box.select(0, 0, i))
-end
-return a;
+box.replace(box.schema.SPACE_ID, 0, 0, 'tweedledum')
+box.replace(box.schema.INDEX_ID, 0, 0, 'primary', 'hash', 1, 1, 0, 'num')
 
---# set connection replica
-while box.info.lsn < 11 do
-    box.fiber.sleep(0.001)
+--# setopt delimiter ';'
+--# set connection default, hot_standby, replica
+function _insert(_begin, _end)
+    a = {}
+    for i = _begin, _end do
+        table.insert(a, box.insert(0, i, 'the tuple '..i))
+    end
+    return unpack(a)
+end;
+function _insert(_begin, _end)
+    a = {}
+    for i = _begin, _end do
+        table.insert(a, box.select(0, 0, i))
+    end
+    return unpack(a)
 end;
+function _wait_lsn(_lsn)
+    while box.info.lsn < _lsn do
+        box.fiber.sleep(0.001)
+    end
+end;
+--# setopt delimiter ''
+--# set connection default
+_insert(1, 10)
+_select(1, 10)
 
-a = {}
-for i = 1, 10 do
-    table.insert(a, box.select(0, 0, i))
-end
-return a;
+--# set connection replica
+_wait_lsn(13)
+_select(1, 10)
 
 --# stop server default
 box.fiber.sleep(0.2)
 
 --# set connection hot_standby
-box.replace(box.schema.SPACE_ID, 0, 0, 'tweedledum');
-box.replace(box.schema.INDEX_ID, 0, 0, 'primary', 'hash', 1, 1, 0, 'num');
+box.replace(box.schema.SPACE_ID, 0, 0, 'tweedledum')
+box.replace(box.schema.INDEX_ID, 0, 0, 'primary', 'hash', 1, 1, 0, 'num')
 
-a = {}
-for i = 11, 20 do
-    table.insert(a, box.insert(0, i, 'the tuple '..tostring(i)))
-end
-return a;
-
-a = {}
-for i = 11, 20 do
-    table.insert(a, box.select(0, 0, i))
-end
-return a;
+_insert(11, 20)
+_select(11, 20)
 
 --# set connection replica
-while box.info.lsn < 21 do
-    box.fiber.sleep(0.01)
-end;
-
-a = {}
-for i = 11, 20 do
-    table.insert(a, box.select(0, 0, i))
-end
-return a;
+_wait_lsn(23)
+_select(11, 20)
 
 --# stop server hot_standby
 --# stop server replica
-- 
GitLab