|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import os |
| 5 | + |
| 6 | +from durabletask import ConcurrencyOptions |
| 7 | +from durabletask.worker import TaskHubGrpcWorker |
| 8 | + |
| 9 | + |
| 10 | +def test_default_concurrency_options(): |
| 11 | + """Test that default concurrency options work correctly.""" |
| 12 | + options = ConcurrencyOptions() |
| 13 | + processor_count = os.cpu_count() or 1 |
| 14 | + expected_default = 100 * processor_count |
| 15 | + |
| 16 | + assert options.maximum_concurrent_activity_work_items == expected_default |
| 17 | + assert options.maximum_concurrent_orchestration_work_items == expected_default |
| 18 | + assert options.max_total_workers == expected_default |
| 19 | + |
| 20 | + |
| 21 | +def test_custom_concurrency_options(): |
| 22 | + """Test that custom concurrency options work correctly.""" |
| 23 | + options = ConcurrencyOptions( |
| 24 | + maximum_concurrent_activity_work_items=50, |
| 25 | + maximum_concurrent_orchestration_work_items=25, |
| 26 | + ) |
| 27 | + |
| 28 | + assert options.maximum_concurrent_activity_work_items == 50 |
| 29 | + assert options.maximum_concurrent_orchestration_work_items == 25 |
| 30 | + assert options.max_total_workers == 50 # Max of both values |
| 31 | + |
| 32 | + |
| 33 | +def test_partial_custom_options(): |
| 34 | + """Test that partially specified options use defaults for unspecified values.""" |
| 35 | + processor_count = os.cpu_count() or 1 |
| 36 | + expected_default = 100 * processor_count |
| 37 | + |
| 38 | + options = ConcurrencyOptions( |
| 39 | + maximum_concurrent_activity_work_items=30 |
| 40 | + # Leave other options as default |
| 41 | + ) |
| 42 | + |
| 43 | + assert options.maximum_concurrent_activity_work_items == 30 |
| 44 | + assert options.maximum_concurrent_orchestration_work_items == expected_default |
| 45 | + assert ( |
| 46 | + options.max_total_workers == expected_default |
| 47 | + ) # Should be the default since it's larger |
| 48 | + |
| 49 | + |
| 50 | +def test_max_total_workers_calculation(): |
| 51 | + """Test that max_total_workers returns the maximum of all concurrency limits.""" |
| 52 | + # Case 1: Activity is highest |
| 53 | + options1 = ConcurrencyOptions( |
| 54 | + maximum_concurrent_activity_work_items=100, |
| 55 | + maximum_concurrent_orchestration_work_items=50, |
| 56 | + ) |
| 57 | + assert options1.max_total_workers == 100 |
| 58 | + |
| 59 | + # Case 2: Orchestration is highest |
| 60 | + options2 = ConcurrencyOptions( |
| 61 | + maximum_concurrent_activity_work_items=25, |
| 62 | + maximum_concurrent_orchestration_work_items=100, |
| 63 | + ) |
| 64 | + assert options2.max_total_workers == 100 |
| 65 | + |
| 66 | + |
| 67 | +def test_worker_with_concurrency_options(): |
| 68 | + """Test that TaskHubGrpcWorker accepts concurrency options.""" |
| 69 | + options = ConcurrencyOptions( |
| 70 | + maximum_concurrent_activity_work_items=10, |
| 71 | + maximum_concurrent_orchestration_work_items=20, |
| 72 | + ) |
| 73 | + |
| 74 | + worker = TaskHubGrpcWorker(concurrency_options=options) |
| 75 | + |
| 76 | + assert worker.concurrency_options == options |
| 77 | + |
| 78 | + |
| 79 | +def test_worker_default_options(): |
| 80 | + """Test that TaskHubGrpcWorker uses default options when no parameters are provided.""" |
| 81 | + worker = TaskHubGrpcWorker() |
| 82 | + |
| 83 | + processor_count = os.cpu_count() or 1 |
| 84 | + expected_default = 100 * processor_count |
| 85 | + |
| 86 | + assert ( |
| 87 | + worker.concurrency_options.maximum_concurrent_activity_work_items == expected_default |
| 88 | + ) |
| 89 | + assert ( |
| 90 | + worker.concurrency_options.maximum_concurrent_orchestration_work_items == expected_default |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +def test_concurrency_options_property_access(): |
| 95 | + """Test that the concurrency_options property works correctly.""" |
| 96 | + options = ConcurrencyOptions( |
| 97 | + maximum_concurrent_activity_work_items=15, |
| 98 | + maximum_concurrent_orchestration_work_items=25, |
| 99 | + ) |
| 100 | + |
| 101 | + worker = TaskHubGrpcWorker(concurrency_options=options) |
| 102 | + retrieved_options = worker.concurrency_options |
| 103 | + |
| 104 | + # Should be the same object |
| 105 | + assert retrieved_options is options |
| 106 | + |
| 107 | + # Should have correct values |
| 108 | + assert retrieved_options.maximum_concurrent_activity_work_items == 15 |
| 109 | + assert retrieved_options.maximum_concurrent_orchestration_work_items == 25 |
| 110 | + |
| 111 | + |
| 112 | +def test_edge_cases(): |
| 113 | + """Test edge cases like zero or very large values.""" |
| 114 | + # Test with zeros (should still work) |
| 115 | + options_zero = ConcurrencyOptions( |
| 116 | + maximum_concurrent_activity_work_items=0, |
| 117 | + maximum_concurrent_orchestration_work_items=0, |
| 118 | + ) |
| 119 | + assert options_zero.max_total_workers == 0 |
| 120 | + |
| 121 | + # Test with very large values |
| 122 | + options_large = ConcurrencyOptions( |
| 123 | + maximum_concurrent_activity_work_items=999999, |
| 124 | + maximum_concurrent_orchestration_work_items=1, |
| 125 | + ) |
| 126 | + assert options_large.max_total_workers == 999999 |
| 127 | + assert options_large.max_total_workers == 999999 |
0 commit comments