Skip to content

Commit

Permalink
SNOW-1362666: improve random string generation (#2034)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-aling committed Aug 23, 2024
1 parent 2b44cd3 commit 050a1ac
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions DESCRIPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne

# Release Notes

- v3.12.2 (TBD)
- Improved implementation of `snowflake.connector.util_text.random_string` to avoid collisions.

- v3.12.1(August 20,2024)
- Fixed a bug that logged the session token when renewing a session.
- Fixed a bug where disabling client telemetry did not work.
Expand Down
2 changes: 1 addition & 1 deletion src/snowflake/connector/util_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,5 @@ def random_string(
suffix: Suffix to add to random string generated.
choices: A generator of things to choose from.
"""
random_part = "".join([random.choice(choices) for _ in range(length)])
random_part = "".join([random.Random().choice(choices) for _ in range(length)])
return "".join([prefix, random_part, suffix])
37 changes: 37 additions & 0 deletions test/unit/test_text_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
#

import concurrent.futures
import random

import pytest

try:
from snowflake.connector.util_text import random_string
except ImportError:
pass

pytestmark = pytest.mark.skipolddriver # old test driver tests won't run this module


def test_random_string_generation_with_same_global_seed():
random.seed(42)
random_string1 = random_string()
random.seed(42)
random_string2 = random_string()
assert (
isinstance(random_string1, str)
and isinstance(random_string2, str)
and random_string1 != random_string2
)

def get_random_string():
random.seed(42)
return random_string()

with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
# Submit tasks to the pool and get future objects
futures = [executor.submit(get_random_string) for _ in range(5)]
res = [f.result() for f in futures]
assert len(set(res)) == 5 # no duplicate string

0 comments on commit 050a1ac

Please sign in to comment.