Skip to content

Commit a25f977

Browse files
authored
Merge pull request #700 from openml/fix_bugs
Fixes a bug which makes sure that uploading a task does not fail due to the task already existing on the test server.
2 parents bed8652 + 4adb83f commit a25f977

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

tests/test_tasks/test_task.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
from random import randint
33

4+
from openml.exceptions import OpenMLServerException
45
from openml.testing import TestBase
56
from openml.datasets import (
67
get_dataset,
@@ -42,16 +43,37 @@ def test_download_task(self):
4243

4344
def test_upload_task(self):
4445

45-
dataset_id = self._get_compatible_rand_dataset()
46-
# TODO consider implementing on the diff task types.
47-
task = create_task(
48-
task_type_id=self.task_type_id,
49-
dataset_id=dataset_id,
50-
target_name=self._get_random_feature(dataset_id),
51-
estimation_procedure_id=self.estimation_procedure
52-
)
46+
# We don't know if the task in question already exists, so we try a few times. Checking
47+
# beforehand would not be an option because a concurrent unit test could potentially
48+
# create the same task and make this unit test fail (i.e. getting a dataset and creating
49+
# a task for it is not atomic).
50+
for i in range(100):
51+
try:
52+
dataset_id = self._get_compatible_rand_dataset()
53+
# TODO consider implementing on the diff task types.
54+
task = create_task(
55+
task_type_id=self.task_type_id,
56+
dataset_id=dataset_id,
57+
target_name=self._get_random_feature(dataset_id),
58+
estimation_procedure_id=self.estimation_procedure
59+
)
60+
61+
task_id = task.publish()
62+
# success
63+
break
64+
except OpenMLServerException as e:
65+
# Error code for 'task already exists'
66+
# Should be 533 according to the docs
67+
# (# https://www.openml.org/api_docs#!/task/post_task)
68+
if e.code == 614:
69+
continue
70+
else:
71+
raise e
72+
else:
73+
raise ValueError(
74+
'Could not create a valid task for task type ID {}'.format(self.task_type_id)
75+
)
5376

54-
task_id = task.publish()
5577
_delete_entity('task', task_id)
5678

5779
def _get_compatible_rand_dataset(self) -> int:

0 commit comments

Comments
 (0)