Skip to content

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

Closed
@chrisbobbe

Description

@chrisbobbe

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).

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions