Skip to content

Commit 525d698

Browse files
author
Jonathan Esterhazy
committed
fix flaky tests
1 parent 6c8ce56 commit 525d698

File tree

6 files changed

+293
-259
lines changed

6 files changed

+293
-259
lines changed

src/sagemaker/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import errno
1616
import os
17+
import random
1718
import re
1819
import sys
1920
import tarfile
@@ -64,6 +65,14 @@ def name_from_base(base, max_length=63, short=False):
6465
return '{}-{}'.format(trimmed_base, timestamp)
6566

6667

68+
def unique_name_from_base(base, max_length=63):
69+
unique = '%04x' % random.randrange(16**4) # 4-digit hex
70+
ts = str(int(time.time()))
71+
available_length = max_length - 2 - len(ts) - len(unique)
72+
trimmed = base[:available_length]
73+
return '{}-{}-{}'.format(trimmed, ts, unique)
74+
75+
6776
def airflow_name_from_base(base, max_length=63, short=False):
6877
"""Append airflow execution_date macro (https://airflow.apache.org/code.html?#macros)
6978
to the provided string. The macro will beevaluated in Airflow operator runtime.

tests/integ/local_mode_utils.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
from __future__ import absolute_import
14+
15+
import fcntl
16+
import os
17+
import time
18+
from contextlib import contextmanager
19+
20+
import tests.integ
21+
22+
LOCK_PATH = os.path.join(tests.integ.DATA_DIR, 'local_mode_lock')
23+
24+
25+
@contextmanager
26+
def lock():
27+
# Since Local Mode uses the same port for serving, we need a lock in order
28+
# to allow concurrent test execution.
29+
local_mode_lock_fd = open(LOCK_PATH, 'w')
30+
local_mode_lock = local_mode_lock_fd.fileno()
31+
32+
fcntl.lockf(local_mode_lock, fcntl.LOCK_EX)
33+
34+
try:
35+
yield
36+
finally:
37+
time.sleep(5)
38+
fcntl.lockf(local_mode_lock, fcntl.LOCK_UN)

0 commit comments

Comments
 (0)