From 27b79e7189fa30fec60a049b8c8c4360fc6eaa2b Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Sat, 5 Aug 2017 11:10:11 -0700 Subject: [PATCH] populate_db: Fix duplicate test data when run multithreaded. The previous implementation resulted in each of the 10 threads using the same random seed and thus same messages. Fixes #6028. --- zilencer/management/commands/populate_db.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index 00fdbad103..21497439ed 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -240,13 +240,16 @@ class Command(BaseCommand): personals_pairs = [random.sample(user_profiles_ids, 2) for i in range(options["num_personals"])] + # Generate a new set of test data. + create_test_data() + threads = options["threads"] - jobs = [] # type: List[Tuple[int, List[List[int]], Dict[str, Any], Callable[[str], int]]] + jobs = [] # type: List[Tuple[int, List[List[int]], Dict[str, Any], Callable[[str], int], int]] for i in range(threads): count = options["num_messages"] // threads if i < options["num_messages"] % threads: count += 1 - jobs.append((count, personals_pairs, options, self.stdout.write)) + jobs.append((count, personals_pairs, options, self.stdout.write, random.randint(0, 10**10))) for job in jobs: send_messages(job) @@ -402,15 +405,14 @@ def get_recipient_by_id(rid): # - multiple messages per subject # - both single and multi-line content def send_messages(data): - # type: (Tuple[int, Sequence[Sequence[int]], Mapping[str, Any], Callable[[str], Any]]) -> int - (tot_messages, personals_pairs, options, output) = data - random.seed(os.getpid()) - - # Generate a new set of test data. - create_test_data() + # type: (Tuple[int, Sequence[Sequence[int]], Mapping[str, Any], Callable[[str], Any], int]) -> int + (tot_messages, personals_pairs, options, output, random_seed) = data + print(random_seed) + random.seed(random_seed) with open("var/test_messages.json", "r") as infile: dialog = ujson.load(infile) + random.shuffle(dialog) texts = itertools.cycle(dialog) recipient_streams = [klass.id for klass in