From 9acff95c145c3643916ba3e464946d57b99b72d2 Mon Sep 17 00:00:00 2001
From: Valentin Syrovatskiy <v.syrovatskiy@picodata.io>
Date: Wed, 27 Jul 2022 14:46:31 +0300
Subject: [PATCH] test: randomized testing

---
 test/conftest.py             |  4 ++-
 test/rand/.gitignore         |  1 -
 test/rand/__init__.py        |  0
 test/rand/params.py          | 17 +++++++++++++
 test/rand/test_randomized.py | 47 ++++++------------------------------
 5 files changed, 27 insertions(+), 42 deletions(-)
 delete mode 100644 test/rand/.gitignore
 create mode 100644 test/rand/__init__.py
 create mode 100644 test/rand/params.py

diff --git a/test/conftest.py b/test/conftest.py
index 90945ac8a2..6dd2bf4a6d 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -8,6 +8,7 @@ import funcy  # type: ignore
 import pytest
 import signal
 import subprocess
+from rand.params import generate_seed
 
 from datetime import datetime
 from shutil import rmtree
@@ -45,7 +46,8 @@ def pytest_addoption(parser):
 
 @pytest.fixture(scope="session")
 def seed(pytestconfig):
-    return pytestconfig.getoption("seed")
+    seed = pytestconfig.getoption("seed")
+    return seed if seed else generate_seed()
 
 
 @pytest.fixture(scope="session")
diff --git a/test/rand/.gitignore b/test/rand/.gitignore
deleted file mode 100644
index b5b55d57fa..0000000000
--- a/test/rand/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-seeds.txt
diff --git a/test/rand/__init__.py b/test/rand/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/test/rand/params.py b/test/rand/params.py
new file mode 100644
index 0000000000..44ffc80ccb
--- /dev/null
+++ b/test/rand/params.py
@@ -0,0 +1,17 @@
+import time
+
+SEED_CAP = 14  # 19 max
+
+
+def int_to_base_62(num: int):
+    base_62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+    BASE = len(base_62)
+    rete = ""
+    while num != 0:
+        rete = base_62[num % BASE] + rete
+        num = num // BASE
+    return rete
+
+
+def generate_seed():
+    return int_to_base_62(time.time_ns() % pow(10, SEED_CAP))
diff --git a/test/rand/test_randomized.py b/test/rand/test_randomized.py
index f6a9ad0f5d..75de31078e 100644
--- a/test/rand/test_randomized.py
+++ b/test/rand/test_randomized.py
@@ -1,22 +1,9 @@
 import math
 import time
 import random
-import os
 from conftest import Cluster, Instance
-import pathlib
 
-
-SEED_CAP = 14  # 19 max
-
-
-def int_to_base_62(num: int):
-    base_62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
-    BASE = len(base_62)
-    rete = ""
-    while num != 0:
-        rete = base_62[num % BASE] + rete
-        num = num // BASE
-    return rete
+STEP_DELAY = 500  # ms
 
 
 def create(c: Cluster, istate):
@@ -26,11 +13,8 @@ def create(c: Cluster, istate):
 
 
 def stop(i: Instance, istate):
-    # assert i.process, f"Instance {i.instance_id} expected has process"
-    # assert i.process.pid, f"Instance {i.instance_id} expected process to be alive"
     istate[i.instance_id]["started"] = False
     return i.terminate(), istate
-    # return os.kill(i.process.pid, signal.SIGTERM), istate
 
 
 def start(i: Instance, istate):
@@ -61,8 +45,6 @@ ACTIONS = {
 }
 BASE = len(ACTIONS)
 
-SEEDLOG = "seeds.txt"
-
 
 def possible_actions(c: Cluster, istate):
     actions = []
@@ -78,9 +60,9 @@ def possible_actions(c: Cluster, istate):
 
 
 def stop_allowed(istate):
-    return len(list(filter(lambda i: i[1]["started"], istate.items()))) > math.trunc(
-        len(istate) / 2 + 1
-    )
+    started_cnt = len(list(filter(lambda i: i[1]["started"], istate.items())))
+    ndiv2_plus1 = math.trunc(len(istate) / 2 + 1)
+    return started_cnt > ndiv2_plus1
 
 
 def choose_action(c: Cluster, istate):
@@ -94,16 +76,6 @@ def step_msg(step: int, action, i: Instance):
     return f"Step {step}: {msg}"
 
 
-def generate_seed():
-    return int_to_base_62(time.time_ns() % pow(10, SEED_CAP))
-
-
-def log_params(seed, delay):
-    dir = pathlib.Path(__file__).parent.absolute()
-    with open(os.path.join(dir, SEEDLOG), "a") as f:
-        f.write(f"{seed} {delay}\n")
-
-
 def initial_istate(cluster: Cluster):
     istate = {}
     for i in cluster.instances:
@@ -111,21 +83,16 @@ def initial_istate(cluster: Cluster):
     return istate
 
 
-def test_randomized(cluster: Cluster, seed: int, delay: int, capsys):
+def test_randomized(cluster: Cluster, seed: str, delay: int, capsys):
     cluster.deploy(instance_count=3)
 
-    seed = seed if seed else generate_seed()
+    random.seed(seed)
 
-    # delay should be generated by random even if given as CLI param
-    generated_delay = random.randint(100, 500)
-    delay = int(delay) if delay else generated_delay
+    delay = int(delay) if delay else STEP_DELAY
 
-    log_params(seed, delay)
     with capsys.disabled():
         print(f"Seed: {seed} , step delay: {delay} ms")
 
-    random.seed(seed)
-
     steps_count = random.randint(5, 10)
     print(f"Do {steps_count} steps...")
 
-- 
GitLab