Skip to content

Commit 5b1a54f

Browse files
committed
Rework local test
1 parent 48046b6 commit 5b1a54f

3 files changed

Lines changed: 97 additions & 92 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Actor, ApifyClient, Dataset } from 'apify';
2+
3+
const client = new ApifyClient({
4+
token: process.env.APIFY_TOKEN,
5+
});
6+
7+
// Simulate local environment by removing platform env vars
8+
delete process.env.APIFY_IS_AT_HOME;
9+
delete process.env.ACTOR_STORAGES_JSON;
10+
11+
const actor = new Actor({
12+
isAtHome: false,
13+
logLevel: 'DEBUG',
14+
});
15+
16+
await actor.init();
17+
18+
// Open storages by alias — locally, this should use the alias as the storage name
19+
const resultsDataset = await actor.openDataset({ alias: 'results' });
20+
21+
// Write data to the aliased storages
22+
await resultsDataset.pushData([
23+
{ url: 'https://example.com', title: 'Example' },
24+
{ url: 'https://example.org', title: 'Example Org' },
25+
]);
26+
27+
// Verify purge-on-first-open: open the same alias again and write more data.
28+
// The previously written data should still be there (no second purge).
29+
const resultsDatasetAgain = await actor.openDataset({ alias: 'results' });
30+
await resultsDatasetAgain.pushData([
31+
{ url: 'https://example.net', title: 'Example Net' },
32+
]);
33+
34+
// Read back all data from the aliased dataset
35+
const allData = await resultsDatasetAgain.getData();
36+
37+
// Transfer results to the platform's default dataset so the test script can verify
38+
const run = await client.run(process.env.ACTOR_RUN_ID).get();
39+
const platformDataset = await Dataset.open(run.defaultDatasetId, {
40+
storageClient: client,
41+
});
42+
43+
await platformDataset.pushData([
44+
{
45+
datasetItemCount: allData.count,
46+
datasetItems: allData.items,
47+
},
48+
]);
49+
50+
await actor.exit();
Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,23 @@
1-
import { Actor, ApifyClient, Dataset } from 'apify';
2-
3-
const client = new ApifyClient({
4-
token: process.env.APIFY_TOKEN,
5-
});
6-
7-
// Simulate local environment by removing platform env vars
8-
delete process.env.APIFY_IS_AT_HOME;
9-
delete process.env.ACTOR_STORAGES_JSON;
10-
11-
const actor = new Actor({
12-
isAtHome: false,
13-
logLevel: 'DEBUG',
14-
});
15-
16-
await actor.init();
17-
18-
// Open storages by alias — locally, this should use the alias as the storage name
19-
const resultsDataset = await actor.openDataset({ alias: 'results' });
20-
21-
// Write data to the aliased storages
22-
await resultsDataset.pushData([
23-
{ url: 'https://example.com', title: 'Example' },
24-
{ url: 'https://example.org', title: 'Example Org' },
25-
]);
26-
27-
// Verify purge-on-first-open: open the same alias again and write more data.
28-
// The previously written data should still be there (no second purge).
29-
const resultsDatasetAgain = await actor.openDataset({ alias: 'results' });
30-
await resultsDatasetAgain.pushData([
31-
{ url: 'https://example.net', title: 'Example Net' },
32-
]);
33-
34-
// Read back all data from the aliased dataset
35-
const allData = await resultsDatasetAgain.getData();
36-
37-
// Transfer results to the platform's default dataset so the test script can verify
38-
const run = await client.run(process.env.ACTOR_RUN_ID).get();
39-
const platformDataset = await Dataset.open(run.defaultDatasetId, {
40-
storageClient: client,
41-
});
42-
43-
await platformDataset.pushData([
44-
{
45-
datasetItemCount: allData.count,
46-
datasetItems: allData.items,
47-
},
48-
]);
49-
50-
await actor.exit();
1+
import { execFileSync } from 'node:child_process';
2+
import { dirname, join } from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
5+
import { log } from '@apify/log';
6+
7+
const dir = dirname(fileURLToPath(import.meta.url));
8+
const lifecyclePath = join(dir, 'lifecycle.mjs');
9+
10+
// Run two separate "lifecycle" processes that share the same filesystem.
11+
// The second process should purge the aliased dataset on first open,
12+
// proving that purge-on-first-open works across Actor restarts.
13+
for (const phase of ['first', 'second']) {
14+
log.info(`--- Running ${phase} lifecycle ---`);
15+
execFileSync('node', [lifecyclePath], {
16+
stdio: 'inherit',
17+
env: process.env,
18+
});
19+
}
20+
21+
// Both lifecycle processes pushed a summary to the platform default dataset.
22+
// The test script will verify both summaries.
23+
log.info('Both lifecycles completed successfully');

test/e2e/sdk/multiStorageLocal/test.mjs

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ const runActor = async (input = {}, options = {}) => {
1717
return await client.run(runId).get();
1818
};
1919

20-
test('aliased storages work locally with alias as name', async () => {
20+
test('aliased storages work locally with purge-on-first-open across restarts', async () => {
21+
// The actor runs two lifecycle processes in sequence, sharing the same filesystem.
22+
// Each lifecycle creates a fresh Actor instance (resetting purgedStorageAliases),
23+
// opens the aliased dataset (triggering purge on first open), writes data, and
24+
// pushes a summary to the platform default dataset.
2125
const run = await runActor();
2226

2327
assert.strictEqual(run.status, 'SUCCEEDED');
@@ -29,53 +33,31 @@ test('aliased storages work locally with alias as name', async () => {
2933

3034
assert.strictEqual(
3135
data.count,
32-
1,
33-
'There must be exactly one summary item in the dataset',
36+
2,
37+
'There must be exactly two summary items (one per lifecycle)',
3438
);
3539

36-
const summary = data.items[0];
37-
38-
// The dataset should have 3 items total (2 from first open + 1 from second open),
39-
// proving that the second open did NOT purge the data
40+
// First lifecycle: fresh local storage, writes 3 items total (2 + 1 from second open)
41+
const firstSummary = data.items[0];
4042
assert.strictEqual(
41-
summary.datasetItemCount,
43+
firstSummary.datasetItemCount,
4244
3,
43-
'Aliased dataset should have 3 items (purge only happened on first open)',
44-
);
45-
46-
// Verify the actual items
47-
assert.strictEqual(summary.datasetItems[0].url, 'https://example.com');
48-
assert.strictEqual(summary.datasetItems[0].title, 'Example');
49-
assert.strictEqual(summary.datasetItems[1].url, 'https://example.org');
50-
assert.strictEqual(summary.datasetItems[1].title, 'Example Org');
51-
assert.strictEqual(summary.datasetItems[2].url, 'https://example.net');
52-
assert.strictEqual(summary.datasetItems[2].title, 'Example Net');
53-
});
54-
55-
test('purge-on-first-open works across runs', async () => {
56-
// Run the actor a second time — the aliased storages should be purged again
57-
// at the start of this new run (since purgedStorageAliases resets per Actor instance)
58-
const run = await runActor();
59-
60-
assert.strictEqual(run.status, 'SUCCEEDED');
61-
62-
const dataset = await Dataset.open(run.defaultDatasetId, {
63-
storageClient: client,
64-
});
65-
const data = await dataset.getData();
66-
67-
assert.strictEqual(
68-
data.count,
69-
1,
70-
'There must be exactly one summary item in the dataset',
45+
'First lifecycle: aliased dataset should have 3 items (purge only happened on first open)',
7146
);
72-
73-
const summary = data.items[0];
74-
75-
// Should again be 3 items, not 6 — because the first open purged the stale data from the previous run
47+
assert.strictEqual(firstSummary.datasetItems[0].url, 'https://example.com');
48+
assert.strictEqual(firstSummary.datasetItems[0].title, 'Example');
49+
assert.strictEqual(firstSummary.datasetItems[1].url, 'https://example.org');
50+
assert.strictEqual(firstSummary.datasetItems[1].title, 'Example Org');
51+
assert.strictEqual(firstSummary.datasetItems[2].url, 'https://example.net');
52+
assert.strictEqual(firstSummary.datasetItems[2].title, 'Example Net');
53+
54+
// Second lifecycle: the aliased dataset from the first lifecycle is still on disk.
55+
// The second lifecycle's first openDataset({ alias: 'results' }) should purge it,
56+
// then write 3 fresh items. If purge didn't work, there would be 6 items.
57+
const secondSummary = data.items[1];
7658
assert.strictEqual(
77-
summary.datasetItemCount,
59+
secondSummary.datasetItemCount,
7860
3,
79-
'Aliased dataset should have 3 items (previous run data was purged)',
61+
'Second lifecycle: aliased dataset should have 3 items (stale data from first lifecycle was purged)',
8062
);
8163
});

0 commit comments

Comments
 (0)