mitata
mitata copied to clipboard
Executing bench blocks when external objects asynchronously initialized
I have a complex case to get working, and I think I understand what's going on. The test is below.
The issue is akasha.filecache.documents
. This object is initialized asynchronously, and the code here is how I've successfully used this object in many other files. The cacheSetup
and fileCachesReady
functions are where this object is set up, and calling isReady
ensures that it is set up. THen calling console.log(documents)
prints out the object.
Hence - the object is set up before the bench blocks are executed. Unless run
executes the bench
blocks right away somehow.
In the plugin.findBlogDoc
function, akasha.filecache.documents
is found to be undefined
. But, as just said, the code which executes before the bench
blocks has ensured that this object is set up.
Is run
executing in-line with the other code? That is, the condition of akasha.filecache.documents
being undefined
could happen if run
were to magically execute right away, before fileCachesReady
exits. But that should not be happening because Node.js handles top-level async/await correctly, I believe.
import { bench, run } from "mitata";
import { createRequire } from 'module';
import * as akasha from 'akasharender';
const require = createRequire(import.meta.url);
const config = require('../config.js');
// const akasha = config.akasha;
await akasha.cacheSetup(config);
await akasha.fileCachesReady(config);
const documents = (await akasha.filecache).documents;
await documents.isReady();
const plugin = config.plugin("@akashacms/plugins-blog-podcast");
const info1 = documents.find('blog/2017/11/test-post-2.html.md');
bench('find-blog-vpath', () => {
plugin.findBlogForVPInfo(info1);
});
bench('find-blog-docs', async () => {
await plugin.findBlogDocs(config, plugin.blogcfg('news'), 'news');
});
// await filecache.close();
await akasha.closeCaches();
try {
await run({
percentiles: false
});
} catch (err) { console.error(err); }
it sounds like you are clearing it in await akasha.closeCaches();
?
benchmarks are only run when the run()
function is called