Skip to content

Commit

Permalink
feat: add integration_test
Browse files Browse the repository at this point in the history
  • Loading branch information
lichuang committed Oct 3, 2024
1 parent ee1f2d1 commit 0536047
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/log/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub fn init_log(log_config: LogConfig) -> Result<()> {
let level: LevelFilter = log_config.level.into();
let log_line_pattern = "[{d(%Y-%m-%d %H:%M:%s)} {({l}):5.5}] {m}{n}";

let log_file = format!("{}/replited_log", log_config.dir);
let log_file_pattern = format!("{}/replited_log_{{}}", log_config.dir);
let log_file = format!("{}/replited.log", log_config.dir);
let log_file_pattern = format!("{}/replited_{{}}.log", log_config.dir);
let roller_count = 9;
let roller_base = 1;
let fixed_window_roller = FixedWindowRoller::builder()
Expand Down
46 changes: 32 additions & 14 deletions tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import os, sys
import random
import string
from tempfile import TemporaryDirectory
import subprocess
import time
import shutil

class Test:
def __init__(self, root, max_records):
Expand All @@ -28,15 +28,24 @@ def insert_random_data(self, start):
name = ''.join(random.choices(string.ascii_letters, k=5))
value = i + start
records.append((name, value))
self.conn.executemany('INSERT INTO random_data (name, value) VALUES (?, ?)', records)
self.cursor.executemany('INSERT INTO random_data (name, value) VALUES (?, ?)', records)
self.conn.commit()

return num_records

def insert(self):
num_records = 0
sleep_num = 0
while num_records < self.max_records:
num_records += self.insert_random_data(num_records)
num = self.insert_random_data(num_records)
num_records += num
sleep_num += num
if sleep_num > 500:
print("after insert ", sleep_num, " data, total: ", num_records, ", go to sleep(1)")
time.sleep(1)
sleep_num = 0

print("finish insert test data, total: ", num_records)

def query_data(self):
cursor = self.conn.cursor()
Expand All @@ -45,11 +54,12 @@ def query_data(self):

class ConfigGenerator:
def __init__(self):
#self.root = TemporaryDirectory().name
self.cwd = os.getcwd()
self.root = self.cwd + "/.test"
# clean test dir
try:
os.rmdir(self.root)
shutil.rmtree(self.root)
pass
except:
pass
print("root: ", self.root)
Expand Down Expand Up @@ -87,12 +97,19 @@ def do_generate(self):
self.config_file = config_file

def start_replicate(p, config_file):
cmd = p + " --config " + config_file + " replicate"
subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
cmd = p + " --config " + config_file + " replicate &"
print("replicate cmd: ", cmd)
#cmds = [p, "--config", config_file, "replicate", "&"]
#pipe = subprocess.Popen(cmds, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#ret = os.popen(cmds)
os.system(cmd)
#print("after replicate")
#print("after replicate: ", pipe.stdout.read())

def stop_replicate():
cmd = "killall replicate"
subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
os.system(cmd)
#subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

def test_restore(p, config_file, root, exp_data):
db = root + "/test.db"
Expand All @@ -102,23 +119,25 @@ def test_restore(p, config_file, root, exp_data):
except:
pass
cmd = p + " --config " + config_file + " restore --db " + db + " --output " + output
#cmds = [p, "--config", config_file,"restore", "--db", db, "--output", output]
print("restore: ", cmd)
pipe = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("restore result: ", pipe.stdout.read())
#pipe = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#pipe = subprocess.Popen(cmds, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
os.system(cmd)
#print("after restore")

conn = sqlite3.connect(output)
cursor = conn.cursor()
cursor.execute('SELECT * FROM random_data order by value')
data = cursor.fetchall()
print("data: ", len(data))
print("exp_data: ", len(exp_data))
print("data len: ", len(data), ", exp_data len: ", len(exp_data))
assert data == exp_data

if __name__ == '__main__':
config = FsConfigGenerator()
config.generate()

test = Test(config.root, 2)
test = Test(config.root, 20000)
test.create_table()

bin = "/Users/codedump/source/replited/target/debug/replited"
Expand All @@ -127,7 +146,6 @@ def test_restore(p, config_file, root, exp_data):
test.insert()

time.sleep(3)
print('sleep')
data = test.query_data()

stop_replicate()
Expand Down

0 comments on commit 0536047

Please sign in to comment.