compass_app: failure to connect to remote/mock auth server in staging mode
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.
https://github.com/flutter/samples/blob/42c081f219c617a2010452c45c9af26279ffc861/compass_app/app/lib/data/services/api/auth_api_client.dart#L28
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.
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.
https://github.com/flutter/samples/blob/d62c784789683f09b92b81f964968251d5d630b7/compass_app/app/lib/data/services/api/api_client.dart#L19-L25
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.
https://github.com/flutter/samples/blob/d62c784789683f09b92b81f964968251d5d630b7/compass_app/app/integration_test/app_server_data_test.dart#L36-L45