Skip to content

Concurrency issue with use find client#423

Closed
PedroMarianoAlmeida wants to merge 3 commits intometeor:tests-3.xfrom
PedroMarianoAlmeida:concurrency-issue-with-useFindClient
Closed

Concurrency issue with use find client#423
PedroMarianoAlmeida wants to merge 3 commits intometeor:tests-3.xfrom
PedroMarianoAlmeida:concurrency-issue-with-useFindClient

Conversation

@PedroMarianoAlmeida
Copy link
Contributor

Possible fix for #418

I created a new test that failed for the current useFind and passed for the new one

I basically removed the useRef and move the Initial Fetch into the Effect, this broke the Verify reference stability between rerenders test so I needed to update the refresh action on useFindReducer to compare the values properly (I feel that there is room from improvement on this part).

@CLAassistant
Copy link

CLAassistant commented Mar 13, 2025

CLA assistant check
All committers have signed the CLA.

@PedroMarianoAlmeida PedroMarianoAlmeida force-pushed the concurrency-issue-with-useFindClient branch from 268e6c6 to 3743be2 Compare March 13, 2025 02:49
@PedroMarianoAlmeida
Copy link
Contributor Author

Test coverage:
Screenshot 2025-03-13 at 12 01 00 AM

My new test (Immediate update before effect registration (race condition test) with current useFind
Screenshot 2025-03-13 at 12 03 11 AM

Test code:

  Tinytest.addAsync(
    'useFind - Immediate update before effect registration (race condition test)',
    async function (test, completed) {
      const container = document.createElement('div');
      document.body.appendChild(container);

      const TestDocs = new Mongo.Collection(null);
      // Insert a single document.
      TestDocs.insert({ id: 1, val: 'initial' });

      const Test = () => {
        const docs = useFind(() => TestDocs.find(), []);
        return (
          <div data-testid="doc-value">
            {docs && docs[0] && docs[0].val}
          </div>
        );
      };

      // Render the component.
      ReactDOM.render(<Test />, container);

      // Immediately update the document (this should occur
      // after the synchronous fetch in the old code but before the effect attaches).
      TestDocs.update({ id: 1 }, { $set: { val: 'updated' } });

      // Wait until the rendered output reflects the update.
      await waitFor(() => {
        const node = container.querySelector('[data-testid="doc-value"]');
        if (!node || !node.textContent.includes('updated')) {
          throw new Error('Updated value not rendered yet');
        }
      }, { container, timeout: 500 });

      test.ok(
        container.innerHTML.includes('updated'),
        'Document should display updated value; the old code would fail to capture this update.'
      );

      document.body.removeChild(container);
      completed();
    }
  );

@nachocodoner nachocodoner deleted the branch meteor:tests-3.x March 13, 2025 14:15
@nachocodoner
Copy link
Member

nachocodoner commented Mar 13, 2025

@PedroMarianoAlmeida: Sorry, 🙏. When I merged and deleted tests-3.x branch your PR based on that branch closed automatically.

Could you reopen the PR with the same contents and ensure to get latest master on it? We have also included CI checks #424

@PedroMarianoAlmeida
Copy link
Contributor Author

Sure! I will!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants