Skip to content

Commit 9773bdb

Browse files
committed
Update based on real tests with [email protected]
1 parent fd62c22 commit 9773bdb

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

src/apify/_actor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ async def __aenter__(self) -> Self:
199199
# Mark initialization as complete and update global state.
200200
self._is_initialized = True
201201
_ActorType._is_any_instance_initialized = True
202+
203+
if not Actor.is_at_home():
204+
# Make sure that the input related KVS is initialized to ensure that the input aware client is used
205+
await self.open_key_value_store()
202206
return self
203207

204208
async def __aexit__(

src/apify/storage_clients/_file_system/_key_value_store_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def _get_input_key_file_name(path_to_kvs: Path, configuration: ApifyConfiguratio
130130
for file_path in chain(
131131
path_to_kvs.glob(f'{configuration.input_key}.*'), path_to_kvs.glob(f'{configuration.input_key}')
132132
):
133-
if file_path.suffix == f'.{METADATA_FILENAME}':
133+
if str(file_path).endswith(METADATA_FILENAME):
134134
# Ignore metadata files
135135
continue
136136
found_input_files.add(file_path.name)

tests/unit/test_apify_storages.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,8 @@ async def test_multiple_input_file_formats_cause_error(input_test_configuration:
119119

120120
# Create two input files in the KVS directory
121121
kvs_path = Path(input_test_configuration.storage_dir) / 'key_value_stores' / 'default'
122-
with open(kvs_path / f'{input_test_configuration.input_key}', 'wb') as f: # noqa: ASYNC230 # It is ok for a test to use sync I/O
123-
f.write(EXAMPLE_BYTES_INPUT)
124-
125-
with open(kvs_path / f'{input_test_configuration.input_key}.json', 'w', encoding='utf-8') as f: # noqa: ASYNC230 # It is ok for a test to use sync I/O
126-
f.write(EXAMPLE_JSON_INPUT)
122+
(kvs_path / f'{input_test_configuration.input_key}').write_bytes(EXAMPLE_BYTES_INPUT)
123+
(kvs_path / f'{input_test_configuration.input_key}.json').write_text(EXAMPLE_JSON_INPUT)
127124

128125
with pytest.raises(RuntimeError, match=r'Only one input file is allowed. Following input files found: .*'):
129126
await KeyValueStore.open(
@@ -138,8 +135,7 @@ async def test_txt_input_missing_metadata(input_test_configuration: Configuratio
138135
# Create custom key file without metadata in the KVS directory
139136
kvs_path = Path(input_test_configuration.storage_dir) / 'key_value_stores' / 'default'
140137
input_file = kvs_path / f'{input_test_configuration.input_key}.txt'
141-
with open(input_file, 'w', encoding='utf-8') as f: # noqa: ASYNC230 # It is ok for a test to use sync I/O
142-
f.write(EXAMPLE_TXT_INPUT)
138+
input_file.write_text(EXAMPLE_TXT_INPUT)
143139
last_modified = input_file.stat().st_mtime
144140

145141
# Make sure that filesystem has enough time to detect changes
@@ -159,8 +155,7 @@ async def test_json_input_missing_metadata(input_test_configuration: Configurati
159155
# Create custom key file without metadata in the KVS directory
160156
kvs_path = Path(input_test_configuration.storage_dir) / 'key_value_stores' / 'default'
161157
input_file = kvs_path / f'{input_test_configuration.input_key}{suffix}'
162-
with open(input_file, 'w', encoding='utf-8') as f: # noqa: ASYNC230 # It is ok for a test to use sync I/O
163-
f.write(EXAMPLE_JSON_INPUT)
158+
input_file.write_text(EXAMPLE_JSON_INPUT)
164159
last_modified = input_file.stat().st_mtime
165160

166161
# Make sure that filesystem has enough time to detect changes
@@ -180,8 +175,7 @@ async def test_bytes_input_missing_metadata(input_test_configuration: Configurat
180175
# Create custom key file without metadata in the KVS directory
181176
kvs_path = Path(input_test_configuration.storage_dir) / 'key_value_stores' / 'default'
182177
input_file = kvs_path / f'{input_test_configuration.input_key}{suffix}'
183-
with open(input_file, 'wb') as f: # noqa: ASYNC230 # It is ok for a test to use sync I/O
184-
f.write(EXAMPLE_BYTES_INPUT)
178+
input_file.write_bytes(EXAMPLE_BYTES_INPUT)
185179
last_modified = input_file.stat().st_mtime
186180

187181
# Make sure that filesystem has enough time to detect changes
@@ -192,3 +186,19 @@ async def test_bytes_input_missing_metadata(input_test_configuration: Configurat
192186
)
193187
assert await kvs.get_value(input_test_configuration.input_key) == EXAMPLE_BYTES_INPUT
194188
assert last_modified == input_file.stat().st_mtime, 'File was modified or recreated.'
189+
190+
191+
async def test_pre_existing_input_not_deleted_in_actor_context(input_test_configuration: Configuration) -> None:
192+
"""Test that pre-existing INPUT file is never deleted as long as the Actor context was started first."""
193+
194+
# Create custom key file without metadata in the KVS directory
195+
kvs_path = Path(input_test_configuration.storage_dir) / 'key_value_stores' / 'default'
196+
input_file = kvs_path / f'{input_test_configuration.input_key}'
197+
input_file.write_bytes(EXAMPLE_BYTES_INPUT)
198+
199+
async with Actor(configuration=input_test_configuration):
200+
# Storage client that is not aware of the input file and could delete it during purge.
201+
storage_client = FileSystemStorageClient()
202+
# Unless already implicitly opened by Actor, the input file would be deleted.
203+
await KeyValueStore.open(storage_client=storage_client, configuration=input_test_configuration)
204+
assert await Actor.get_input() == EXAMPLE_BYTES_INPUT

0 commit comments

Comments
 (0)