Skip to content

Commit

Permalink
optimize unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
Cathy0908 committed Dec 24, 2024
1 parent 4f65b09 commit 949d534
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions tests/tools/test_process_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@
from data_juicer.utils.unittest_utils import DataJuicerTestCaseBase


def run_in_subprocess(cmd):
result = subprocess.run(
cmd,
shell=True,
capture_output=True,
text=True
)

if result.returncode != 0:
print(f"Command failed with return code {result.returncode}")
print(f"Standard Output: {result.stdout}")
print(f"Standard Error: {result.stderr}")
raise subprocess.CalledProcessError(result, cmd)

return result


class ProcessDataTest(DataJuicerTestCaseBase):

def setUp(self):
Expand Down Expand Up @@ -78,10 +95,13 @@ def setUp(self):
os.makedirs(self.tmp_dir)

def _auto_create_ray_cluster(self):
if not subprocess.call('ray status', shell=True):
try:
# ray cluster already exists, return
run_in_subprocess('ray status')
self.tmp_ray_cluster = False
return
except:
pass

self.tmp_ray_cluster = True
head_port = '6379'
Expand All @@ -95,12 +115,10 @@ def _auto_create_ray_cluster(self):

print(f"current rank: {rank}; execute cmd: {cmd}")

result = subprocess.call(cmd, shell=True)
if result != 0:
raise subprocess.CalledProcessError(result, cmd)
run_in_subprocess(cmd)

def _close_ray_cluster(self):
subprocess.call('ray stop', shell=True)
run_in_subprocess('ray stop')

def tearDown(self):
super().tearDown()
Expand Down Expand Up @@ -148,10 +166,8 @@ def test_ray_image(self):
with open(tmp_yaml_file, 'w') as file:
yaml.dump(yaml_config, file)

status_code = subprocess.call(
f'python tools/process_data.py --config {tmp_yaml_file}', shell=True)
run_in_subprocess(f'python tools/process_data.py --config {tmp_yaml_file}')

self.assertEqual(status_code, 0)
self.assertTrue(osp.exists(tmp_out_path))

import ray
Expand Down

0 comments on commit 949d534

Please sign in to comment.