gojuno / composer

Reactive Android Instrumentation Test Runner. Archived. Marathon is recommended as an alternative (https://github.com/Malinskiy/marathon).
Apache License 2.0
546 stars 45 forks source link

Intermittent APK install failure after starting emulators with swarmer #171

Open ssaqua opened 5 years ago

ssaqua commented 5 years ago

This might be a silly question, how exactly should composer be called after starting emulators with swarmer?

I'm finding that composer may fail to install my test APKs because the emulators are not given sufficient time for the package manager to become available:

adb: failed to install path/to/test-apk: cmd: Can't find service: package

My buildscript simply executes the swarmer and composer commands one after each other.

Here are the relevant parts from the command outputs:

[Thu Jun 13 15:54:08 2019]: Swarmer: - "My job is done here, took 35.873 seconds, startedEmulators: [Emulator(id=emulator-5556, name=test_emulator_1), Emulator(id=emulator-5554, name=test_emulator_2), Emulator(id=emulator-5558, name=test_emulator_3), Emulator(id=emulator-5560, name=test_emulator_4)], bye bye."
[Thu Jun 13 15:54:09 2019]: 4 connected adb device(s): [AdbDevice(id=emulator-5560, model=Android SDK built for x86, online=true), AdbDevice(id=emulator-5554, model=Android SDK built for x86, online=true), AdbDevice(id=emulator-5556, model=Android SDK built for x86, online=true), AdbDevice(id=emulator-5558, model=Android SDK built for x86, online=true)]
[Thu Jun 13 15:54:09 2019]: [emulator-5560] Installing apk... pathToApk = path/to/file.apk
[Thu Jun 13 15:54:09 2019]: [emulator-5556] Installing apk... pathToApk = path/to/file.apk
[Thu Jun 13 15:54:09 2019]: [emulator-5554] Installing apk... pathToApk = path/to/file.apk
[Thu Jun 13 15:54:09 2019]: [emulator-5558] Installing apk... pathToApk = path/to/file.apk
[Thu Jun 13 15:54:10 2019]: [emulator-5560] Error during installing apk: java.lang.IllegalStateException: Process [script, -F, /Users/_/AndroidStudioProjects/myproject/222639283763292.output, /Users/_/Library/Android/sdk/platform-tools/adb, -s, emulator-5560, install, -r, path/to/file.apk] exited with non-zero code 1 Script started on Thu Jun 13 15:54:09 2019
command: /Users/_/Library/Android/sdk/platform-tools/adb -s emulator-5560 install -r path/to/file.apk
Performing Streamed Install
adb: failed to install path/to/file.apk: cmd: Can't find service: package

Script done on Thu Jun 13 15:54:10 2019

Everything works fine if I simply wait to run composer manually.

I'm currently doing something like this as a workaround, would rather not do this if it's not necessary.

java -jar swarmer.jar start ...

service() {
  adb -s $1 shell service check $2
}

for serial in $(adb devices | tail -n +2 | cut -sf 1); do
  echo "[$(date +'%a %b %d %H:%M:%S %Z %Y')]: Waiting for package service to be available for emulator Emulator(id=$serial)"
  while [ "$(service $serial package)" != "Service package: found" ]; do
    sleep 1
  done
  while [ "$(service $serial mount)" != "Service mount: found" ]; do
    sleep 1
  done
done

java -jar composer.jar ...

update - the mount service is also required because package installation can also fail due to the storage manager not being available:

2019-06-17T01:11:07.8665540Z java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List android.os.storage.StorageManager.getVolumes()' on a null object reference
2019-06-17T01:11:07.8765600Z    at com.android.internal.content.PackageHelper.resolveInstallVolume(PackageHelper.java:184)
2019-06-17T01:11:07.8865640Z    at com.android.internal.content.PackageHelper.resolveInstallVolume(PackageHelper.java:148)
2019-06-17T01:11:07.8965290Z    at com.android.internal.content.PackageHelper.resolveInstallVolume(PackageHelper.java:163)
2019-06-17T01:11:07.9066730Z    at com.android.server.pm.PackageInstallerService.createSessionInternal(PackageInstallerService.java:501)
2019-06-17T01:11:07.9161410Z    at com.android.server.pm.PackageInstallerService.createSession(PackageInstallerService.java:404)
2019-06-17T01:11:07.9261400Z    at com.android.server.pm.PackageManagerShellCommand.doCreateSession(PackageManagerShellCommand.java:2415)
2019-06-17T01:11:07.9362160Z    at com.android.server.pm.PackageManagerShellCommand.runInstall(PackageManagerShellCommand.java:907)
2019-06-17T01:11:07.9462890Z    at com.android.server.pm.PackageManagerShellCommand.onCommand(PackageManagerShellCommand.java:158)
2019-06-17T01:11:07.9562360Z    at android.os.ShellCommand.exec(ShellCommPerforming Streamed Install
plastiv commented 4 years ago

👋 Are you starting emulators with -no-boot-anim option?

Currently, swarmer waits for animation to be completed. take a look here https://github.com/gojuno/swarmer/pull/49

ssaqua commented 4 years ago

Actually I'm not working on the same project anymore as when I opened this issue 😅

I do recall playing with both -no-boot-anim and without it, and I did see your PR before and thought that it would help.

We moved away from using swarmer eventually because we needed something with better support for snapshots, but still using composer for the test execution.

If I get some time I might see if I can make a minimal sample that reproduces the original issue.