Skip to content

test flake: Consecutive autogenerated email addresses are identical 0.1% of the time #1026

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

Closed
chrisbobbe opened this issue Oct 25, 2024 · 0 comments · Fixed by #1027
Closed
Assignees
Labels

Comments

@chrisbobbe
Copy link
Collaborator

I just got this test failure when working on unrelated code (#683):

Running test...
00:10 +1123 ~6: /Users/chrisbobbe/dev/zulip-flutter/test/notifications/display_test.dart: NotificationDisplayManager open find account among several
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following AccountAlreadyExistsException was thrown running a test:
Instance of 'AccountAlreadyExistsException'

When the exception was thrown, this was the stack:
#0      TestGlobalStore.doInsertAccount (file:///Users/chrisbobbe/dev/zulip-flutter/test/model/test_store.dart:101:7)
#1      GlobalStore.insertAccount (package:zulip/model/store.dart:180:27)
#2      TestGlobalStore.add (file:///Users/chrisbobbe/dev/zulip-flutter/test/model/test_store.dart:84:11)
#3      main.<anonymous closure>.<anonymous closure> (file:///Users/chrisbobbe/dev/zulip-flutter/test/notifications/display_test.dart:900:39)
<asynchronous suspension>
#4      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:189:15)
<asynchronous suspension>
#5      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1027:5)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)

The test description was:
  find account among several
════════════════════════════════════════════════════════════════════════════════════════════════════
00:10 +1123 ~6 -1: /Users/chrisbobbe/dev/zulip-flutter/test/notifications/display_test.dart: NotificationDisplayManager open find account among several [E]
  Test failed. See exception logs above.
  The test description was: find account among several
  

To run this test again: /Users/chrisbobbe/.local/lib/flutter/bin/cache/dart-sdk/bin/dart test /Users/chrisbobbe/dev/zulip-flutter/test/notifications/display_test.dart -p vm --plain-name 'NotificationDisplayManager open find account among several'

The bug is in the way we auto-generate email addresses for example User objects:

/// A random email address, different from previously generated ones.
String _nextEmail() => 'mail${_lastEmailSuffix += Random().nextInt(1000)}@example.com';
int _lastEmailSuffix = 1000;

If that Random().nextInt(1000) comes up with zero, then this email address will match the previous email address. That's what happened in the notifications test code, which failed on trying to add accounts for two users with the same email address:

      final user1 = eg.user();
      final user2 = eg.user();
      final accounts = [
        eg.account(id: 1001, realmUrl: realmUrlA, user: user1),
        eg.account(id: 1002, realmUrl: realmUrlA, user: user2),
        eg.account(id: 1003, realmUrl: realmUrlB, user: user1),
        eg.account(id: 1004, realmUrl: realmUrlB, user: user2),
      ];
      for (final account in accounts) {
        await testBinding.globalStore.add(account, eg.initialSnapshot());
      }

Here's the fix:

- String _nextEmail() => 'mail${_lastEmailSuffix += Random().nextInt(1000)}@example.com';
+ String _nextEmail() => 'mail${_lastEmailSuffix += 1 + Random().nextInt(1000)}@example.com';

(which I see is already what _nextUserId, _nextStreamId, and _nextMessageId do).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant