|
| 1 | +import uuid |
| 2 | +from datetime import datetime |
| 3 | +from datetime import timedelta |
| 4 | +from datetime import timezone |
| 5 | + |
| 6 | +import httpx |
| 7 | + |
| 8 | +from onyx.configs.app_configs import POLL_CONNECTOR_OFFSET |
| 9 | +from onyx.configs.constants import DocumentSource |
| 10 | +from onyx.connectors.mock_connector.connector import MockConnectorCheckpoint |
| 11 | +from onyx.connectors.models import InputType |
| 12 | +from onyx.db.enums import IndexingStatus |
| 13 | +from tests.integration.common_utils.constants import MOCK_CONNECTOR_SERVER_HOST |
| 14 | +from tests.integration.common_utils.constants import MOCK_CONNECTOR_SERVER_PORT |
| 15 | +from tests.integration.common_utils.managers.cc_pair import CCPairManager |
| 16 | +from tests.integration.common_utils.managers.index_attempt import IndexAttemptManager |
| 17 | +from tests.integration.common_utils.test_document_utils import create_test_document |
| 18 | +from tests.integration.common_utils.test_models import DATestUser |
| 19 | + |
| 20 | + |
| 21 | +def _setup_mock_connector( |
| 22 | + mock_server_client: httpx.Client, |
| 23 | + admin_user: DATestUser, |
| 24 | +) -> None: |
| 25 | + test_doc = create_test_document() |
| 26 | + successful_response = { |
| 27 | + "documents": [test_doc.model_dump(mode="json")], |
| 28 | + "checkpoint": MockConnectorCheckpoint(has_more=False).model_dump(mode="json"), |
| 29 | + "failures": [], |
| 30 | + } |
| 31 | + response = mock_server_client.post( |
| 32 | + "/set-behavior", |
| 33 | + json=[successful_response, successful_response], # For two attempts |
| 34 | + ) |
| 35 | + assert response.status_code == 200 |
| 36 | + |
| 37 | + |
| 38 | +def test_poll_connector_time_ranges( |
| 39 | + mock_server_client: httpx.Client, |
| 40 | + admin_user: DATestUser, |
| 41 | +) -> None: |
| 42 | + """ |
| 43 | + Tests that poll connectors correctly set their poll_range_start and poll_range_end |
| 44 | + across multiple indexing attempts. |
| 45 | + """ |
| 46 | + # Set up mock server behavior - a simple successful response |
| 47 | + _setup_mock_connector(mock_server_client, admin_user) |
| 48 | + |
| 49 | + # Create a CC Pair for the mock connector with POLL input type |
| 50 | + cc_pair_name = f"mock-poll-time-range-{uuid.uuid4()}" |
| 51 | + cc_pair = CCPairManager.create_from_scratch( |
| 52 | + name=cc_pair_name, |
| 53 | + source=DocumentSource.MOCK_CONNECTOR, |
| 54 | + input_type=InputType.POLL, |
| 55 | + connector_specific_config={ |
| 56 | + "mock_server_host": MOCK_CONNECTOR_SERVER_HOST, |
| 57 | + "mock_server_port": MOCK_CONNECTOR_SERVER_PORT, |
| 58 | + }, |
| 59 | + user_performing_action=admin_user, |
| 60 | + refresh_freq=3, # refresh often to ensure the second attempt actually runs |
| 61 | + ) |
| 62 | + |
| 63 | + # --- First Indexing Attempt --- |
| 64 | + time_before_first_attempt = datetime.now(timezone.utc) |
| 65 | + first_index_attempt = IndexAttemptManager.wait_for_index_attempt_start( |
| 66 | + cc_pair_id=cc_pair.id, |
| 67 | + user_performing_action=admin_user, |
| 68 | + ) |
| 69 | + IndexAttemptManager.wait_for_index_attempt_completion( |
| 70 | + index_attempt_id=first_index_attempt.id, |
| 71 | + cc_pair_id=cc_pair.id, |
| 72 | + user_performing_action=admin_user, |
| 73 | + ) |
| 74 | + time_after_first_attempt = datetime.now(timezone.utc) |
| 75 | + |
| 76 | + # Fetch and validate the first attempt |
| 77 | + completed_first_attempt = IndexAttemptManager.get_index_attempt_by_id( |
| 78 | + index_attempt_id=first_index_attempt.id, |
| 79 | + cc_pair_id=cc_pair.id, |
| 80 | + user_performing_action=admin_user, |
| 81 | + ) |
| 82 | + assert completed_first_attempt.status == IndexingStatus.SUCCESS |
| 83 | + assert completed_first_attempt.poll_range_start is not None |
| 84 | + assert completed_first_attempt.poll_range_end is not None |
| 85 | + |
| 86 | + # For the first run (no prior successful attempts), poll_range_start should be epoch (0) |
| 87 | + expected_first_start = datetime.fromtimestamp(0, tz=timezone.utc) |
| 88 | + assert completed_first_attempt.poll_range_start == expected_first_start |
| 89 | + |
| 90 | + # `poll_range_end` should be sometime in between the time the attempt |
| 91 | + # started and the time it finished. |
| 92 | + # no way to have a more precise assertion here since the `poll_range_end` |
| 93 | + # can really be set anytime in that range and be "correct" |
| 94 | + assert ( |
| 95 | + time_before_first_attempt |
| 96 | + <= completed_first_attempt.poll_range_end |
| 97 | + <= time_after_first_attempt |
| 98 | + ) |
| 99 | + |
| 100 | + first_attempt_poll_end = completed_first_attempt.poll_range_end |
| 101 | + |
| 102 | + # --- Second Indexing Attempt --- |
| 103 | + # Trigger another run manually (since automatic refresh might be too slow for test) |
| 104 | + # Ensure there's a slight delay so the poll window moves |
| 105 | + # In a real scenario, the scheduler would wait for the refresh frequency. |
| 106 | + # Here we manually trigger a new run. |
| 107 | + _setup_mock_connector(mock_server_client, admin_user) |
| 108 | + CCPairManager.run_once( |
| 109 | + cc_pair, from_beginning=False, user_performing_action=admin_user |
| 110 | + ) |
| 111 | + |
| 112 | + time_before_second_attempt = datetime.now(timezone.utc) |
| 113 | + second_index_attempt = IndexAttemptManager.wait_for_index_attempt_start( |
| 114 | + cc_pair_id=cc_pair.id, |
| 115 | + index_attempts_to_ignore=[first_index_attempt.id], |
| 116 | + user_performing_action=admin_user, |
| 117 | + ) |
| 118 | + IndexAttemptManager.wait_for_index_attempt_completion( |
| 119 | + index_attempt_id=second_index_attempt.id, |
| 120 | + cc_pair_id=cc_pair.id, |
| 121 | + user_performing_action=admin_user, |
| 122 | + ) |
| 123 | + time_after_second_attempt = datetime.now(timezone.utc) |
| 124 | + |
| 125 | + # Fetch and validate the second attempt |
| 126 | + completed_second_attempt = IndexAttemptManager.get_index_attempt_by_id( |
| 127 | + index_attempt_id=second_index_attempt.id, |
| 128 | + cc_pair_id=cc_pair.id, |
| 129 | + user_performing_action=admin_user, |
| 130 | + ) |
| 131 | + assert completed_second_attempt.status == IndexingStatus.SUCCESS |
| 132 | + assert completed_second_attempt.poll_range_start is not None |
| 133 | + assert completed_second_attempt.poll_range_end is not None |
| 134 | + |
| 135 | + # For the second run, poll_range_start should be the previous successful attempt's |
| 136 | + # poll_range_end minus the POLL_CONNECTOR_OFFSET |
| 137 | + expected_second_start = first_attempt_poll_end - timedelta( |
| 138 | + minutes=POLL_CONNECTOR_OFFSET |
| 139 | + ) |
| 140 | + assert completed_second_attempt.poll_range_start == expected_second_start |
| 141 | + |
| 142 | + # `poll_range_end` should be sometime in between the time the attempt |
| 143 | + # started and the time it finished. |
| 144 | + # again, no way to have a more precise assertion here since the `poll_range_end` |
| 145 | + # can really be set anytime in that range and be "correct" |
| 146 | + assert ( |
| 147 | + time_before_second_attempt |
| 148 | + <= completed_second_attempt.poll_range_end |
| 149 | + <= time_after_second_attempt |
| 150 | + ) |
0 commit comments