1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
|
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import pytest
import datetime
from devtools_testutils.aio import recorded_by_proxy_async
from azure.core.serialization import _datetime_as_isostr # pylint:disable=protected-access
from _router_test_case_async import AsyncRouterRecordedTestCase
from _validators import RouterJobValidator
from azure.communication.jobrouter._model_base import _deserialize_datetime as _convert_str_to_datetime
from _decorators_async import RouterPreparersAsync
from azure.communication.jobrouter._shared.utils import parse_connection_str
from azure.core.exceptions import ResourceNotFoundError
from azure.communication.jobrouter.aio import (
JobRouterClient,
JobRouterAdministrationClient,
)
from azure.communication.jobrouter.models import (
RoundRobinMode,
RouterWorkerSelector,
LabelOperator,
RouterQueueSelector,
StaticQueueSelectorAttachment,
StaticRouterRule,
StaticWorkerSelectorAttachment,
RouterJobStatus,
DistributionPolicy,
RouterQueue,
ClassificationPolicy,
RouterJob,
ScheduleAndSuspendMode,
RouterJobNote,
)
job_labels = {"key1": "JobKey", "key2": 10, "key3": True}
job_tags = {
"tag1": "WorkerGenericInfo",
}
job_channel_references = ["fakeChannelRef1", "fakeChannelRef2"]
job_channel_ids = ["fakeChannelId1", "fakeChannelId2"]
job_priority = 10
job_disposition_code = "JobCancelledByUser"
job_requested_worker_selectors = [
RouterWorkerSelector(key="FakeKey1", label_operator=LabelOperator.EQUAL, value=True),
RouterWorkerSelector(key="FakeKey2", label_operator=LabelOperator.NOT_EQUAL, value=False),
RouterWorkerSelector(key="FakeKey3", label_operator=LabelOperator.LESS_THAN, value=10),
RouterWorkerSelector(key="FakeKey4", label_operator=LabelOperator.LESS_THAN_OR_EQUAL, value=10.01),
RouterWorkerSelector(key="FakeKey5", label_operator=LabelOperator.GREATER_THAN, value=10),
RouterWorkerSelector(key="FakeKey6", label_operator=LabelOperator.GREATER_THAN_OR_EQUAL, value=10),
]
prioritization_rules = [
StaticRouterRule(value=10),
]
cp_worker_selectors = [
StaticWorkerSelectorAttachment(
worker_selector=RouterWorkerSelector(
key="FakeKeyFromCp",
label_operator=LabelOperator.EQUAL,
value="FakeValue",
)
),
]
expected_attached_worker_selectors = [
RouterWorkerSelector(
key="FakeKeyFromCp",
label_operator=LabelOperator.EQUAL,
value="FakeValue",
),
]
test_timestamp = _convert_str_to_datetime("2022-05-13T23:59:04.5311999+07:00")
job_notes = [RouterJobNote(added_at=test_timestamp, message="Fake notes attached to job")]
# The test class name needs to start with "Test" to get collected by pytest
class TestRouterJobAsync(AsyncRouterRecordedTestCase):
async def clean_up(self):
# delete in live mode
if not self.is_playback():
router_client: JobRouterClient = self.create_client()
router_admin_client: JobRouterAdministrationClient = self.create_admin_client()
async with router_client:
async with router_admin_client:
if self._testMethodName in self.job_ids and any(self.job_ids[self._testMethodName]):
for _id in set(self.job_ids[self._testMethodName]):
await self.clean_up_job(job_id=_id)
if self._testMethodName in self.classification_policy_ids and any(
self.classification_policy_ids[self._testMethodName]
):
for policy_id in set(self.classification_policy_ids[self._testMethodName]):
await router_admin_client.delete_classification_policy(policy_id)
if self._testMethodName in self.queue_ids and any(self.queue_ids[self._testMethodName]):
for _id in set(self.queue_ids[self._testMethodName]):
await router_admin_client.delete_queue(_id)
if self._testMethodName in self.distribution_policy_ids and any(
self.distribution_policy_ids[self._testMethodName]
):
for policy_id in set(self.distribution_policy_ids[self._testMethodName]):
await router_admin_client.delete_distribution_policy(policy_id)
def get_distribution_policy_id(self):
return "_" + self._testMethodName + "_tst_dp_async"
async def setup_distribution_policy(self):
client: JobRouterAdministrationClient = self.create_admin_client()
async with client:
distribution_policy_id = self.get_distribution_policy_id()
policy: DistributionPolicy = DistributionPolicy(
name=distribution_policy_id,
offer_expires_after_seconds=10.0,
mode=RoundRobinMode(min_concurrent_offers=1, max_concurrent_offers=1),
)
distribution_policy = await client.upsert_distribution_policy(distribution_policy_id, policy)
# add for cleanup later
if self._testMethodName in self.distribution_policy_ids:
self.distribution_policy_ids[self._testMethodName] = self.distribution_policy_ids[
self._testMethodName
].append(distribution_policy_id)
else:
self.distribution_policy_ids[self._testMethodName] = [distribution_policy_id]
def get_job_queue_id(self):
return "_" + self._testMethodName + "_tst_q_async"
async def setup_job_queue(self):
client: JobRouterAdministrationClient = self.create_admin_client()
async with client:
job_queue_id = self.get_job_queue_id()
job_queue: RouterQueue = RouterQueue(
name=job_queue_id, labels=job_labels, distribution_policy_id=self.get_distribution_policy_id()
)
job_queue = await client.upsert_queue(job_queue_id, job_queue)
# add for cleanup later
if self._testMethodName in self.queue_ids:
self.queue_ids[self._testMethodName].append(job_queue_id)
else:
self.queue_ids[self._testMethodName] = [job_queue_id]
def get_fallback_queue_id(self):
return "_" + self._testMethodName + "_tst_flbk_q_async" # cspell:disable-line
async def setup_fallback_queue(self):
client: JobRouterAdministrationClient = self.create_admin_client()
async with client:
job_queue_id = self.get_fallback_queue_id()
job_queue: RouterQueue = RouterQueue(
name=job_queue_id, labels=job_labels, distribution_policy_id=self.get_distribution_policy_id()
)
job_queue = await client.upsert_queue(job_queue_id, job_queue)
# add for cleanup later
if self._testMethodName in self.queue_ids:
self.queue_ids[self._testMethodName].append(job_queue_id)
else:
self.queue_ids[self._testMethodName] = [job_queue_id]
def get_classification_policy_id(self):
return "_" + self._testMethodName + "_tst_cp"
async def setup_classification_policy(self):
client: JobRouterAdministrationClient = self.create_admin_client()
async with client:
cp_queue_selectors = [
StaticQueueSelectorAttachment(
queue_selector=RouterQueueSelector(
key="Id", label_operator=LabelOperator.EQUAL, value=self.get_job_queue_id()
)
),
]
cp_id = self.get_classification_policy_id()
classification_policy: ClassificationPolicy = ClassificationPolicy(
name=cp_id,
fallback_queue_id=self.get_fallback_queue_id(),
queue_selector_attachments=cp_queue_selectors,
prioritization_rule=prioritization_rules[0],
worker_selector_attachments=cp_worker_selectors,
)
cp = await client.upsert_classification_policy(cp_id, classification_policy)
# add for cleanup later
if self._testMethodName in self.classification_policy_ids:
self.classification_policy_ids[self._testMethodName].append(cp_id)
else:
self.classification_policy_ids[self._testMethodName] = [cp_id]
async def validate_job_is_queued(
self,
identifier, # type: str
**kwargs, # type: Any
):
router_client: JobRouterClient = self.create_client()
async with router_client:
router_job = await router_client.get_job(identifier)
assert router_job.status == RouterJobStatus.QUEUED
async def validate_job_is_scheduled(self, identifier, **kwargs):
router_client: JobRouterClient = self.create_client()
async with router_client:
router_job = await router_client.get_job(identifier)
assert router_job.status == RouterJobStatus.SCHEDULED
async def validate_job_is_cancelled(self, identifier, **kwargs):
router_client: JobRouterClient = self.create_client()
async with router_client:
router_job = await router_client.get_job(identifier)
assert router_job.status == RouterJobStatus.CANCELLED
@RouterPreparersAsync.router_test_decorator_async
@recorded_by_proxy_async
@RouterPreparersAsync.before_test_execute_async("setup_distribution_policy")
@RouterPreparersAsync.before_test_execute_async("setup_job_queue")
@RouterPreparersAsync.after_test_execute_async("clean_up")
async def test_create_job_direct_q(self):
job_identifier = "tst_create_job_man_async"
router_client: JobRouterClient = self.create_client()
async with router_client:
router_job: RouterJob = RouterJob(
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
router_job = await router_client.upsert_job(job_identifier, router_job)
# add for cleanup
self.job_ids[self._testMethodName] = [job_identifier]
assert router_job is not None
RouterJobValidator.validate_job(
router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
assert router_job.status == RouterJobStatus.CREATED
await self._poll_until_no_exception(self.validate_job_is_queued, Exception, job_identifier)
@RouterPreparersAsync.router_test_decorator_async
@recorded_by_proxy_async
@RouterPreparersAsync.before_test_execute_async("setup_distribution_policy")
@RouterPreparersAsync.before_test_execute_async("setup_job_queue")
@RouterPreparersAsync.after_test_execute_async("clean_up")
async def test_create_scheduled_job(self, **kwargs):
recorded_variables = kwargs.pop("variables", {})
scheduled_time = datetime.datetime.utcnow() + datetime.timedelta(0, 30)
scheduled_time_utc = recorded_variables.setdefault("scheduled_time_utc", _datetime_as_isostr(scheduled_time))
matching_mode = ScheduleAndSuspendMode(schedule_at=recorded_variables["scheduled_time_utc"])
job_identifier = "_tst_create_sch_job_async"
router_client: JobRouterClient = self.create_client()
async with router_client:
router_job: RouterJob = RouterJob(
channel_id=job_channel_ids[0],
channel_reference=job_channel_references[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
matching_mode=matching_mode,
)
router_job = await router_client.upsert_job(job_identifier, router_job)
# add for cleanup
self.job_ids[self._testMethodName] = [job_identifier]
assert router_job is not None
RouterJobValidator.validate_job(
router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
matching_mode=matching_mode,
)
assert router_job.status == RouterJobStatus.PENDING_SCHEDULE
await self._poll_until_no_exception(self.validate_job_is_scheduled, Exception, job_identifier)
return recorded_variables
@RouterPreparersAsync.router_test_decorator_async
@recorded_by_proxy_async
@RouterPreparersAsync.before_test_execute_async("setup_distribution_policy")
@RouterPreparersAsync.before_test_execute_async("setup_job_queue")
@RouterPreparersAsync.after_test_execute_async("clean_up")
async def test_update_job_direct_q(self):
job_identifier = "tst_update_job_man_async"
router_client: JobRouterClient = self.create_client()
async with router_client:
router_job: RouterJob = RouterJob(
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
router_job = await router_client.upsert_job(job_identifier, router_job)
# add for cleanup
self.job_ids[self._testMethodName] = [job_identifier]
assert router_job is not None
RouterJobValidator.validate_job(
router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
await self._poll_until_no_exception(self.validate_job_is_queued, Exception, job_identifier)
# Act
router_job.labels["FakeKey"] = "FakeWorkerValue"
updated_job_labels = router_job.labels
update_router_job = await router_client.upsert_job(job_identifier, router_job)
assert update_router_job is not None
RouterJobValidator.validate_job(
update_router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=updated_job_labels,
tags=job_tags,
notes=job_notes,
)
# updating labels does not change job status
assert update_router_job.status == RouterJobStatus.QUEUED
@RouterPreparersAsync.router_test_decorator_async
@recorded_by_proxy_async
@RouterPreparersAsync.before_test_execute_async("setup_distribution_policy")
@RouterPreparersAsync.before_test_execute_async("setup_job_queue")
@RouterPreparersAsync.after_test_execute_async("clean_up")
async def test_update_job_direct_q_w_kwargs(self):
job_identifier = "tst_update_job_man_w_kwargs_async"
router_client: JobRouterClient = self.create_client()
async with router_client:
router_job: RouterJob = RouterJob(
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
router_job = await router_client.upsert_job(job_identifier, router_job)
# add for cleanup
self.job_ids[self._testMethodName] = [job_identifier]
assert router_job is not None
RouterJobValidator.validate_job(
router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
await self._poll_until_no_exception(self.validate_job_is_queued, Exception, job_identifier)
# Act
updated_job_labels = {k: v for k, v in router_job.labels.items()}
updated_job_labels["FakeKey"] = "FakeWorkerValue"
update_router_job = await router_client.upsert_job(job_identifier, labels=updated_job_labels)
assert update_router_job is not None
RouterJobValidator.validate_job(
update_router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=updated_job_labels,
tags=job_tags,
notes=job_notes,
)
# updating labels does not change job status
assert update_router_job.status == RouterJobStatus.QUEUED
@RouterPreparersAsync.router_test_decorator_async
@recorded_by_proxy_async
@RouterPreparersAsync.before_test_execute_async("setup_distribution_policy")
@RouterPreparersAsync.before_test_execute_async("setup_job_queue")
@RouterPreparersAsync.after_test_execute_async("clean_up")
async def test_get_job_direct_q(self):
job_identifier = "tst_get_job_man_async"
router_client: JobRouterClient = self.create_client()
async with router_client:
router_job: RouterJob = RouterJob(
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
router_job = await router_client.upsert_job(job_identifier, router_job)
# add for cleanup
self.job_ids[self._testMethodName] = [job_identifier]
assert router_job is not None
RouterJobValidator.validate_job(
router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
assert router_job.status == RouterJobStatus.CREATED
await self._poll_until_no_exception(self.validate_job_is_queued, Exception, job_identifier)
queried_router_job = await router_client.get_job(job_identifier)
RouterJobValidator.validate_job(
queried_router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
@RouterPreparersAsync.router_test_decorator_async
@recorded_by_proxy_async
@RouterPreparersAsync.before_test_execute_async("setup_distribution_policy")
@RouterPreparersAsync.before_test_execute_async("setup_job_queue")
@RouterPreparersAsync.before_test_execute_async("setup_fallback_queue")
@RouterPreparersAsync.before_test_execute_async("setup_classification_policy")
@RouterPreparersAsync.after_test_execute_async("clean_up")
async def test_create_job_w_cp(self):
job_identifier = "tst_create_job_cp_async"
router_client: JobRouterClient = self.create_client()
async with router_client:
router_job: RouterJob = RouterJob(
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
classification_policy_id=self.get_classification_policy_id(),
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
router_job = await router_client.upsert_job(job_identifier, router_job)
# add for cleanup
self.job_ids[self._testMethodName] = [job_identifier]
assert router_job is not None
RouterJobValidator.validate_job(
router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
classification_policy_id=self.get_classification_policy_id(),
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
assert router_job.status == RouterJobStatus.PENDING_CLASSIFICATION
await self._poll_until_no_exception(self.validate_job_is_queued, Exception, job_identifier)
@RouterPreparersAsync.router_test_decorator_async
@recorded_by_proxy_async
@RouterPreparersAsync.before_test_execute_async("setup_distribution_policy")
@RouterPreparersAsync.before_test_execute_async("setup_job_queue")
@RouterPreparersAsync.before_test_execute_async("setup_fallback_queue")
@RouterPreparersAsync.before_test_execute_async("setup_classification_policy")
@RouterPreparersAsync.after_test_execute_async("clean_up")
async def test_update_job_w_cp(self):
job_identifier = "tst_update_job_cp_async"
router_client: JobRouterClient = self.create_client()
async with router_client:
router_job: RouterJob = RouterJob(
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
classification_policy_id=self.get_classification_policy_id(),
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
router_job = await router_client.upsert_job(job_identifier, router_job)
# add for cleanup
self.job_ids[self._testMethodName] = [job_identifier]
assert router_job is not None
RouterJobValidator.validate_job(
router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
classification_policy_id=self.get_classification_policy_id(),
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
assert router_job.status == RouterJobStatus.PENDING_CLASSIFICATION
await self._poll_until_no_exception(self.validate_job_is_queued, Exception, job_identifier)
# Act
updated_job_labels = {k: v for k, v in router_job.labels.items()}
updated_job_labels["FakeKey"] = "FakeWorkerValue"
router_job.labels = updated_job_labels
update_router_job = await router_client.upsert_job(job_identifier, router_job)
assert update_router_job is not None
RouterJobValidator.validate_job(
update_router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
classification_policy_id=self.get_classification_policy_id(),
requested_worker_selectors=job_requested_worker_selectors,
labels=updated_job_labels,
tags=job_tags,
notes=job_notes,
)
# updating labels reverts job status to pending classification
assert update_router_job.status == RouterJobStatus.PENDING_CLASSIFICATION
await self._poll_until_no_exception(self.validate_job_is_queued, Exception, job_identifier)
@RouterPreparersAsync.router_test_decorator_async
@recorded_by_proxy_async
@RouterPreparersAsync.before_test_execute_async("setup_distribution_policy")
@RouterPreparersAsync.before_test_execute_async("setup_job_queue")
@RouterPreparersAsync.before_test_execute_async("setup_fallback_queue")
@RouterPreparersAsync.before_test_execute_async("setup_classification_policy")
@RouterPreparersAsync.after_test_execute_async("clean_up")
async def test_update_job_w_cp_w_kwargs(self):
job_identifier = "tst_update_job_cp_w_kwargs_async"
router_client: JobRouterClient = self.create_client()
async with router_client:
router_job: RouterJob = RouterJob(
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
classification_policy_id=self.get_classification_policy_id(),
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
router_job = await router_client.upsert_job(job_identifier, router_job)
# add for cleanup
self.job_ids[self._testMethodName] = [job_identifier]
assert router_job is not None
RouterJobValidator.validate_job(
router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
classification_policy_id=self.get_classification_policy_id(),
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
assert router_job.status == RouterJobStatus.PENDING_CLASSIFICATION
await self._poll_until_no_exception(self.validate_job_is_queued, Exception, job_identifier)
# Act
updated_job_labels = {k: v for k, v in router_job.labels.items()}
updated_job_labels["FakeKey"] = "FakeWorkerValue"
update_router_job = await router_client.upsert_job(job_identifier, labels=updated_job_labels)
assert update_router_job is not None
RouterJobValidator.validate_job(
update_router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
classification_policy_id=self.get_classification_policy_id(),
requested_worker_selectors=job_requested_worker_selectors,
labels=updated_job_labels,
tags=job_tags,
notes=job_notes,
)
# updating labels reverts job status to pending classification
assert update_router_job.status == RouterJobStatus.PENDING_CLASSIFICATION
await self._poll_until_no_exception(self.validate_job_is_queued, Exception, job_identifier)
@RouterPreparersAsync.router_test_decorator_async
@recorded_by_proxy_async
@RouterPreparersAsync.before_test_execute_async("setup_distribution_policy")
@RouterPreparersAsync.before_test_execute_async("setup_job_queue")
@RouterPreparersAsync.before_test_execute_async("setup_fallback_queue")
@RouterPreparersAsync.before_test_execute_async("setup_classification_policy")
@RouterPreparersAsync.after_test_execute_async("clean_up")
async def test_get_job_w_cp(self):
job_identifier = "tst_get_job_cp_async"
router_client: JobRouterClient = self.create_client()
async with router_client:
router_job: RouterJob = RouterJob(
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
classification_policy_id=self.get_classification_policy_id(),
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
router_job = await router_client.upsert_job(job_identifier, router_job)
# add for cleanup
self.job_ids[self._testMethodName] = [job_identifier]
assert router_job is not None
RouterJobValidator.validate_job(
router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
classification_policy_id=self.get_classification_policy_id(),
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
assert router_job.status == RouterJobStatus.PENDING_CLASSIFICATION
await self._poll_until_no_exception(self.validate_job_is_queued, Exception, job_identifier)
queried_router_job = await router_client.get_job(job_identifier)
RouterJobValidator.validate_job(
queried_router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
attached_worker_selectors=expected_attached_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
assert queried_router_job.status == RouterJobStatus.QUEUED
@RouterPreparersAsync.router_test_decorator_async
@recorded_by_proxy_async
@RouterPreparersAsync.before_test_execute_async("setup_distribution_policy")
@RouterPreparersAsync.before_test_execute_async("setup_job_queue")
@RouterPreparersAsync.after_test_execute_async("clean_up")
async def test_delete_job(self):
job_identifier = "_tst_del_job_man_async"
router_client: JobRouterClient = self.create_client()
async with router_client:
router_job: RouterJob = RouterJob(
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
router_job = await router_client.upsert_job(job_identifier, router_job)
# add for cleanup
self.job_ids[self._testMethodName] = [job_identifier]
assert router_job is not None
RouterJobValidator.validate_job(
router_job,
identifier=job_identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
assert router_job.status == RouterJobStatus.CREATED
await self._poll_until_no_exception(self.validate_job_is_queued, Exception, job_identifier)
# job needs to be in a termination state before it can be deleted
await router_client.cancel_job(job_identifier)
await router_client.delete_job(job_identifier)
with pytest.raises(ResourceNotFoundError) as nfe:
await router_client.get_job(job_identifier)
self.job_ids.pop(self._testMethodName, None)
assert nfe.value.reason == "Not Found"
assert nfe.value.status_code == 404
@RouterPreparersAsync.router_test_decorator_async
@recorded_by_proxy_async
@RouterPreparersAsync.before_test_execute_async("setup_distribution_policy")
@RouterPreparersAsync.before_test_execute_async("setup_job_queue")
@RouterPreparersAsync.after_test_execute_async("clean_up")
async def test_list_jobs(self):
router_client: JobRouterClient = self.create_client()
job_identifiers = ["tst_list_job_1_async", "tst_list_job_2_async", "tst_list_job_3_async"]
created_job_response = {}
job_count = len(job_identifiers)
self.job_ids[self._testMethodName] = []
async with router_client:
for identifier in job_identifiers:
router_job: RouterJob = RouterJob(
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
router_job = await router_client.upsert_job(identifier, router_job)
# add for cleanup
self.job_ids[self._testMethodName].append(identifier)
assert router_job is not None
RouterJobValidator.validate_job(
router_job,
identifier=identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
)
await self._poll_until_no_exception(self.validate_job_is_queued, Exception, identifier)
created_job_response[router_job.id] = router_job
router_jobs = router_client.list_jobs(
results_per_page=3,
status=RouterJobStatus.QUEUED,
queue_id=self.get_job_queue_id(),
channel_id=job_channel_ids[0],
)
async for router_job_page in router_jobs.by_page():
list_of_jobs = [i async for i in router_job_page]
assert len(list_of_jobs) <= 3
for j_item in list_of_jobs:
response_at_creation = created_job_response.get(j_item.id, None)
if not response_at_creation:
continue
RouterJobValidator.validate_job(
j_item,
identifier=response_at_creation.id,
channel_reference=response_at_creation.channel_reference,
channel_id=response_at_creation.channel_id,
queue_id=response_at_creation.queue_id,
priority=response_at_creation.priority,
requested_worker_selectors=response_at_creation.requested_worker_selectors,
labels=response_at_creation.labels,
tags=response_at_creation.tags,
notes=response_at_creation.notes,
)
job_count -= 1
# all job_queues created were listed
assert job_count == 0
@RouterPreparersAsync.router_test_decorator_async
@recorded_by_proxy_async
@RouterPreparersAsync.before_test_execute_async("setup_distribution_policy")
@RouterPreparersAsync.before_test_execute_async("setup_job_queue")
@RouterPreparersAsync.after_test_execute_async("clean_up")
async def test_list_sch_jobs(self, **kwargs):
recorded_variables = kwargs.pop("variables", {})
scheduled_time = datetime.datetime.utcnow() + datetime.timedelta(0, 30)
scheduled_time_utc = recorded_variables.setdefault("scheduled_time_utc", _datetime_as_isostr(scheduled_time))
matching_mode = ScheduleAndSuspendMode(schedule_at=recorded_variables["scheduled_time_utc"])
router_client: JobRouterClient = self.create_client()
job_identifiers = ["_tst_list_sch_job_1_async", "_tst_list_sch_job_2_async"]
created_job_response = {}
job_count = len(job_identifiers)
self.job_ids[self._testMethodName] = []
async with router_client:
for identifier in job_identifiers:
router_job: RouterJob = RouterJob(
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
matching_mode=matching_mode,
)
router_job = await router_client.upsert_job(identifier, router_job)
# add for cleanup
self.job_ids[self._testMethodName].append(identifier)
assert router_job is not None
RouterJobValidator.validate_job(
router_job,
identifier=identifier,
channel_reference=job_channel_references[0],
channel_id=job_channel_ids[0],
queue_id=self.get_job_queue_id(),
priority=job_priority,
requested_worker_selectors=job_requested_worker_selectors,
labels=job_labels,
tags=job_tags,
notes=job_notes,
matching_mode=matching_mode,
)
await self._poll_until_no_exception(self.validate_job_is_scheduled, Exception, identifier)
created_job_response[router_job.id] = router_job
router_jobs = router_client.list_jobs(
results_per_page=2,
status=RouterJobStatus.SCHEDULED,
queue_id=self.get_job_queue_id(),
scheduled_before=recorded_variables["scheduled_time_utc"],
channel_id=job_channel_ids[0],
)
async for router_job_page in router_jobs.by_page():
list_of_jobs = [i async for i in router_job_page]
assert len(list_of_jobs) <= 2
for j_item in list_of_jobs:
response_at_creation = created_job_response.get(j_item.id, None)
if not response_at_creation:
continue
RouterJobValidator.validate_job(
j_item,
identifier=response_at_creation.id,
channel_reference=response_at_creation.channel_reference,
channel_id=response_at_creation.channel_id,
queue_id=response_at_creation.queue_id,
priority=response_at_creation.priority,
requested_worker_selectors=response_at_creation.requested_worker_selectors,
labels=response_at_creation.labels,
tags=response_at_creation.tags,
notes=response_at_creation.notes,
matching_mode=response_at_creation.matching_mode,
)
job_count -= 1
# all job_queues created were listed
assert job_count == 0
return recorded_variables
|