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

fix(cache): Store memory cache in global #26634

Closed
wants to merge 1 commit into from

Conversation

zharinov
Copy link
Collaborator

@zharinov zharinov commented Jan 14, 2024

Changes

There is annoying behavior of module-level variables being reset during testing.

Sometimes, proper testing requires manipulating memory cache, so we need to initialize and use it, but then we may realize it's always undefined no matter what (probably due to some Jest behavior).

This PR moves the variable to the global level, which seems to fix the problem.

Context

Documentation (please check one with an [x])

  • I have updated the documentation, or
  • No documentation update is required

How I've tested my work (please select one)

I have verified these changes via:

  • Code inspection only, or
  • Newly added/modified unit tests, or
  • No unit tests but ran on a real repository, or
  • Both unit tests + ran on a real repository

@zharinov zharinov requested a review from viceice January 14, 2024 19:11
@zharinov zharinov mentioned this pull request Jan 14, 2024
6 tasks
@rarkins rarkins requested a review from secustor January 15, 2024 06:33
Copy link
Member

@viceice viceice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this workaround. will investigate what happens

@zharinov
Copy link
Collaborator Author

Thank you. I think you could reproduce it on the gitea branch.

What I've tried is to adding following to the test/setup.ts:

jest.mock('../lib/util/cache/memory', () => {
  let repoCache: Record<string, any> | undefined = undefined;

  return {
    init(): void {
      repoCache = {};
    },

    reset(): void {
      repoCache = undefined;
    },

    get<T = any>(key: string): T {
      return repoCache?.[key];
    },

    set(key: string, value: unknown): void {
      if (repoCache) {
        repoCache[key] = value;
      }
    },
  };
});

But this didn't work 🤷‍♂️

@viceice
Copy link
Member

viceice commented Jan 15, 2024

closing, i've fixed your tests on #26451 😉

@viceice viceice closed this Jan 15, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants