Skip to content

make the log file test less flaky by retrying deletes and reads #180

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

Merged
merged 1 commit into from
Jun 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions pkgs/dart_mcp_server/test/dart_tooling_mcp_server_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:test/test.dart';
Expand All @@ -23,16 +24,43 @@ void main() {
});

test('logs traffic to a file', () async {
expect(
await File(logDescriptor.io.path).readAsLines(),
containsAll([
allOf(startsWith('<<<'), contains('"method":"initialize"')),
allOf(startsWith('>>>'), contains('"serverInfo"')),
allOf(startsWith('<<<'), contains('"notifications/initialized"')),
]),
// It may take a bit for all the lines to show up in the log.
Copy link
Contributor

Choose a reason for hiding this comment

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

general question, are we writing to file with flush: true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are calling flush on it after writing (we are opening the file and adding to the sink, then calling flush). In my experience on windows flush doesn't do anything though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also the API says NOTE: This is not necessarily the same as the data being flushed by the operating system.

await doWithRetries(
() async => expect(
await File(logDescriptor.io.path).readAsLines(),
containsAll([
allOf(startsWith('<<<'), contains('"method":"initialize"')),
allOf(startsWith('>>>'), contains('"serverInfo"')),
allOf(startsWith('<<<'), contains('"notifications/initialized"')),
]),
),
);

// Ensure the file handle is released before the file is cleaned up.
await testHarness.serverConnectionPair.serverConnection.shutdown();

// Wait for the process to release the file.
await doWithRetries(() => File(logDescriptor.io.path).delete());
});
});
}

/// Performs [action] up to [maxRetries] times, backing off an extra 50ms
/// between each attempt.
FutureOr<T> doWithRetries<T>(
FutureOr<T> Function() action, {
int maxRetries = 5,
}) async {
var count = 0;
while (true) {
try {
return await action();
} catch (_) {
if (count == maxRetries) {
rethrow;
}
}
count++;
await Future<void>.delayed(Duration(milliseconds: 50 * count));
}
}
Loading