Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache persistence doesn't work #464

Closed
Enelar opened this issue Dec 30, 2022 · 4 comments
Closed

Cache persistence doesn't work #464

Enelar opened this issue Dec 30, 2022 · 4 comments
Labels
question Further information is requested

Comments

@Enelar
Copy link

Enelar commented Dec 30, 2022

Initially I thought this is my configuration issue, but then I found this topic: https://community.cloudflare.com/t/testing-workers-with-the-cache-api/181346

However solution from that discussion wont help me. My caching results already had caching reply status. I've noticed that sometimes the caching behavior is unexpected to me, and decided to write tests to address that issue. However, it seems that even with correct wrangler.toml, cache wont be persistent if miniflare was created as part of a jest test.

It seems that between tests the cache being reset, and I don't see any files created in my selected cache folder. I have 500 lines worker with complicated caching logic, and I would like to have tests cover all the corner cases.

https://github.com/cloudflare/miniflare/blob/master/packages/shared-test-environment/src/storage.ts#L43

module.exports = {
    presets: [
        ['@babel/preset-env', {targets: {node: 'current'}}],
        '@babel/preset-typescript',
    ],
};
export default {
  testEnvironment: "miniflare",
  // Configuration is automatically loaded from `.env`, `package.json` and
  // `wrangler.toml` files by default, but you can pass any additional Miniflare
  // API options here:
  testEnvironmentOptions: {
    bindings: { KEY: "value" },
    kvNamespaces: ["TEST_NAMESPACE"],
  },
};
name = "cloudflare-proxy"
main = "src/index.ts"
compatibility_date = "2022-12-30"
send_metrics = false

[miniflare]
cache_persist = "./cache/"
@Enelar
Copy link
Author

Enelar commented Dec 30, 2022

I've managed to overcome this limitation using nasty hack, but this is a clearly not the best workaround.
I'm exposing reference #storage field and moving it across instances, but I would love to see this fixed.
https://github.com/cloudflare/miniflare/blob/master/packages/cache/src/cache.ts#L184

  afterEach(() => {
    cache_persist = caches.default.hack_storage.map;
  })
  beforeEach(() => {
    caches.default.hack_storage.map = cache_persist;
  })

On a side not I would love to see more time-manipulation available for tests. What happens when cache just expired, etc. I would like to include that into integration tests.

@mrbbot
Copy link
Contributor

mrbbot commented Jan 3, 2023

Hey! 👋 Happy new year! Just catching up with issues now. 🙂

When using the "miniflare" testEnvironment for Jest, all storage operations performed in a test are intentionally undone at the end. This means updates from one test aren't visible in another, and tests can be reordered. See https://miniflare.dev/testing/jest#isolated-storage for more details. If you're trying to seed data for your tests, you should do so in beforeAll/beforeEach hooks. I might be misunderstanding what you're trying to do here though.

On a side not I would love to see more time-manipulation available for tests.

With the Miniflare Jest environment, you should be able to use Jest's standard timer mocks (https://jestjs.io/docs/timer-mocks), specifically with https://jestjs.io/docs/jest-object#jestsetsystemtimenow-number--date to test cache expiry.

@mrbbot mrbbot added the question Further information is requested label Jan 9, 2023
@admah
Copy link
Collaborator

admah commented Jan 23, 2023

@Enelar I'm going to close this out due to inactivity. If the explanation from @mrbbot doesn't solve the issue, feel free to reopen.

@admah admah closed this as completed Jan 23, 2023
@Enelar
Copy link
Author

Enelar commented Feb 13, 2023

@mrbbot, I see the logic behind it. However this is somewhat unexpected. When I create an instance of some object, and use in my test I won't expect it to have same fancy logic around tests. Could you please add this into documentation?

Also, there is no way to save-restore cache state, which bloats tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
Archived in project
Development

No branches or pull requests

3 participants