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

compass_app: failure to connect to remote/mock auth server in staging mode #2563

Open
githubmonkey opened this issue Jan 14, 2025 · 2 comments
Assignees

Comments

@githubmonkey
Copy link

When running flutter run --target lib/main_staging.dart the app doesn't authenticate the mock user against the mock auth server which is running on localhost:8080

From the log:

[LoginViewModel] Login failed! SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 35158
[AuthRepositoryRemote] Error logging in: SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 52772
[LoginViewModel] Login failed! SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 52772
[AuthRepositoryRemote] Error logging in: SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 52782
[LoginViewModel] Login failed! SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 52782
[AuthRepositoryRemote] Error logging in: SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 55032
[LoginViewModel] Login failed! SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 55032
D/EGL_emulation( 3946): app_time_stats: avg=75.52ms min=2.63ms max=2734.61ms count=38

I verified that in AuthApiClient the correct host/port gets passed to HttpClient but it appears that later the port is ignored and HttpClient attempts to post to a random port.

final request = await client.post(_host, _port, '/login');

@ericwindmill ericwindmill self-assigned this Jan 27, 2025
@githubmonkey
Copy link
Author

I just noticed that my integration tests with remote server also fail (and probably have always failed for me.) It seems that the root cause is also a problem with authentication against the mock server.

➜  ~/StudioProjects/samples/compass_app/app (main) ✗ flutter test integration_test/app_server_data_test.dart
00:00 +0: ... /Users/sylvia/StudioProjects/samples/compass_app/app/integration_test/app_server_data_test.dart           R00:15 +0: ... /Users/sylvia/StudioProjects/samples/compass_app/app/integration_test/app_server_data_test.dart      15.2s
✓ Built build/app/outputs/flutter-apk/app-debug.apk
00:16 +0: ... /Users/sylvia/StudioProjects/samples/compass_app/app/integration_test/app_server_data_test.dart           I00:17 +0: ... /Users/sylvia/StudioProjects/samples/compass_app/app/integration_test/app_server_data_test.dart      803ms
00:19 +0 -1: end-to-end test with remote data (setUpAll) [E]                                                            
  ProcessException: No such file or directory
    Command: dart run bin/compass_server.dart
  dart:io                                                                                                                Process.start
  Users/sylvia/StudioProjects/samples/compass_app/app/integration_test/app_server_data_test.dart 37:25                   main.<fn>.<fn>
  ===== asynchronous gap ===========================
  dart:io                                                                                                                Process.start
  Users/sylvia/StudioProjects/samples/compass_app/app/integration_test/app_server_data_test.dart 37:25                   main.<fn>.<fn>
  ===== asynchronous gap ===========================
  dart:async                                                                                                             _CustomZone.registerUnaryCallback
  Users/sylvia/StudioProjects/samples/compass_app/app/integration_test/app_server_data_test.dart 33:33                   main.<fn>.<fn>
  ===== asynchronous gap ===========================
  package:stream_channel                                                                                                 _GuaranteeSink.add
  var/folders/wj/fm1gq6p90ss8k8s_8tvprjmm0000gn/T/flutter_tools.paupVZ/flutter_test_listener.s1GsC9/listener.dart 55:22  main.<fn>
  

To run this test again: /Users/sylvia/flutter/bin/cache/dart-sdk/bin/dart test /Users/sylvia/StudioProjects/samples/compass_app/app/integration_test/app_server_data_test.dart -p vm --plain-name 'end-to-end test with remote data (setUpAll)'
00:19 +0 -2: end-to-end test with remote data (tearDownAll) [E]                                                         
  LateInitializationError: Local 'p' has not been initialized.
  dart:_internal                                                                                                         LateError._throwLocalNotInitialized
  Users/sylvia/StudioProjects/samples/compass_app/app/integration_test/app_server_data_test.dart                         main.<fn>.<fn>
  ===== asynchronous gap ===========================
  package:stream_channel                                                                                                 _GuaranteeSink.add
  var/folders/wj/fm1gq6p90ss8k8s_8tvprjmm0000gn/T/flutter_tools.paupVZ/flutter_test_listener.s1GsC9/listener.dart 55:22  main.<fn>
  

To run this test again: /Users/sylvia/flutter/bin/cache/dart-sdk/bin/dart test /Users/sylvia/StudioProjects/samples/compass_app/app/integration_test/app_server_data_test.dart -p vm --plain-name 'end-to-end test with remote data (tearDownAll)'
00:19 +0 -2: Some tests failed.                                                                         

@githubmonkey
Copy link
Author

I finally had a chance to dig deeper and found that the two issues are not related.

The initial problem (can't connect to mock auth server in staging mode) is because ApiClient() and AuthApiClient() both default to localhost as hostname.

ApiClient({
String? host,
int? port,
HttpClient Function()? clientFactory,
}) : _host = host ?? 'localhost',
_port = port ?? 8080,
_clientFactory = clientFactory ?? HttpClient.new;

This would be fine if server and client ran on the same machine but since I had started my client on an Android emulator, my client's localhost was different than that of the server.

There is currently no way to pass in a different host name but when I hardcoded both providers to use my machine's actual IP address rather than localhost, auth worked as expected.

List<SingleChildWidget> get providersRemote {
  return [
    Provider(
      create: (context) => AuthApiClient(host: '192.168.178.57'),
    ),
    Provider(
      create: (context) => ApiClient(host: '192.168.178.57'),
    ),

If it's ok with the maintainers I'll file a PR to provide for configurable port and hostnames similar to https://github.com/flutter/samples/blob/main/code_sharing/client/lib/main.dart

The second problem described in the comment (integration test fails) is unrelated. Here, the issue is that during setUpAll() Process.start("dart",...) fails for me, allegedly because the executable 'dart' is not found. The command itself is legit so the problem must be related to my setup.

// Start the dart server
p = await Process.start(
'dart',
['run', 'bin/compass_server.dart'],
environment: {'PORT': port},
// Relative to the app/ folder
workingDirectory: '../server',
);
// Wait for server to start and print to stdout.
await p.stdout.first;

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

No branches or pull requests

2 participants