From 180f814487f67904fff0325350b33ff5f8ddcd54 Mon Sep 17 00:00:00 2001
From: Stefan Behnel <stefan_ml@behnel.de>
Date: Wed, 27 Sep 2017 17:02:58 +0200
Subject: [PATCH] Make test runner shards deterministic with a non-forking
 multiprocessing pool (i.e. on Windows) that may use different hash seeds.

---
 runtests.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/runtests.py b/runtests.py
index 5fcdc584e..65504c44d 100755
--- a/runtests.py
+++ b/runtests.py
@@ -15,6 +15,7 @@ import subprocess
 import tempfile
 import traceback
 import warnings
+import zlib
 
 try:
     import platform
@@ -1649,8 +1650,11 @@ class ShardExcludeSelector(object):
         self.shard_num = shard_num
         self.shard_count = shard_count
 
-    def __call__(self, testname, tags=None):
-        return abs(hash(testname)) % self.shard_count != self.shard_num
+    def __call__(self, testname, tags=None, _hash=zlib.crc32, _is_py2=sys.version_info[0] < 3):
+        # Cannot use simple hash() here as shard processes might use different hash seeds.
+        # CRC32 is fast and simple.
+        hashval = _hash(testname if _is_py2 else testname.encode())
+        return hashval % self.shard_count != self.shard_num
 
 
 class PendingThreadsError(RuntimeError):
-- 
2.30.9