Skip to content

Commit e84fe02

Browse files
refactor: use crc32 instead of sha1 to compute hash
To compute a hash to identify container across runs use crc32 instead of sha1 which is faster while still being fit-for-purpose.
1 parent 35e0599 commit e84fe02

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

core/testcontainers/core/container.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import contextlib
2-
import hashlib
32
import logging
3+
import pickle
44
import sys
5+
import zlib
56
from os import PathLike
67
from socket import socket
78
from types import TracebackType
@@ -217,7 +218,7 @@ def start(self) -> Self:
217218
self.volumes,
218219
str(tuple(sorted(self._kwargs.values()))),
219220
]
220-
hash_ = hashlib.sha256(bytes(str(args), encoding="utf-8")).hexdigest()
221+
hash_ = str(zlib.crc32(pickle.dumps(args)))
221222
docker_client = self.get_docker_client()
222223
container = docker_client.find_container_by_hash(hash_)
223224
if container is not None:

core/tests/test_reusable_containers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def test_docker_container_labels_hash_default():
114114
def test_docker_container_labels_hash(monkeypatch):
115115
tc_properties_mock = testcontainers_config.tc_properties | {"testcontainers.reuse.enable": "true"}
116116
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock)
117-
expected_hash = "1bade17a9d8236ba71ffbb676f2ece3fb419ea0e6adb5f82b5a026213c431d8e"
117+
expected_hash = "1296172062"
118118
with DockerContainer("hello-world").with_reuse() as container:
119119
assert container._container is not None
120120
assert container._container.labels["hash"] == expected_hash

0 commit comments

Comments
 (0)