diff options
author | Stas Negara <snegara@google.com> | 2015-07-28 15:58:38 -0700 |
---|---|---|
committer | Stas Negara <snegara@google.com> | 2015-07-28 15:58:38 -0700 |
commit | 5ada7137abdea5c5e659f613078ee38dfa315a03 (patch) | |
tree | 48e508ee9f0d03f3c2979d8b138174cf0ee006c3 | |
parent | 7f7d4675da8a4f8b42adb085e5589604a543373d (diff) | |
download | testing-5ada7137abdea5c5e659f613078ee38dfa315a03.tar.gz |
Close TightVNC Viewer when cloud device is malfunctioning.
Also, make the keep alive thread more robust.
Change-Id: Ic0774a0cf14d7f0787953ed9e26cad89826df4fe
-rw-r--r-- | src/com/google/gct/testing/CloudConfigurationProviderImpl.java | 6 | ||||
-rw-r--r-- | src/com/google/gct/testing/vnc/VncKeepAliveThreadImpl.java | 64 |
2 files changed, 50 insertions, 20 deletions
diff --git a/src/com/google/gct/testing/CloudConfigurationProviderImpl.java b/src/com/google/gct/testing/CloudConfigurationProviderImpl.java index f0f04e7..4b4cfb1 100644 --- a/src/com/google/gct/testing/CloudConfigurationProviderImpl.java +++ b/src/com/google/gct/testing/CloudConfigurationProviderImpl.java @@ -399,7 +399,11 @@ public class CloudConfigurationProviderImpl extends CloudConfigurationProvider { synchronized (ghostCloudDevices) { ghostCloudDevices.remove(ghostCloudDevice); } - blankVncViewer.closeWindow(); + try { // Use try just in case something goes wrong. + blankVncViewer.closeWindow(); + } catch (Exception e) { + e.printStackTrace(); + } // Make sure the device is unlocked. Process unlock = rt.exec("./adb -s " + deviceAddress + " wait-for-device shell input keyevent 82" , null, workingDir); unlock.waitFor(); diff --git a/src/com/google/gct/testing/vnc/VncKeepAliveThreadImpl.java b/src/com/google/gct/testing/vnc/VncKeepAliveThreadImpl.java index 8838237..e565199 100644 --- a/src/com/google/gct/testing/vnc/VncKeepAliveThreadImpl.java +++ b/src/com/google/gct/testing/vnc/VncKeepAliveThreadImpl.java @@ -32,6 +32,7 @@ public class VncKeepAliveThreadImpl extends VncKeepAliveThread { private final String deviceAddress; private final File workingDir; private volatile boolean hasCrashed = false; + private Viewer currentViewer; public static void startVnc(String[] args, String configurationName, String cloudProjectId, String cloudDeviceId, String deviceAddress, @@ -55,34 +56,59 @@ public class VncKeepAliveThreadImpl extends VncKeepAliveThread { @Override public void run() { - SwingUtilities.invokeLater(new Viewer(this, parser, configurationName)); + try { + currentViewer = new Viewer(this, parser, configurationName); + SwingUtilities.invokeLater(currentViewer); - while (!Thread.currentThread().isInterrupted() && deviceIsReady()) { - try { - getTest().projects().devices().keepalive(cloudProjectId, cloudDeviceId).execute(); - } catch (Exception e) { - e.printStackTrace(); - } - // Restart the viewer if it accidentally crashed. - if (hasCrashed) { - hasCrashed = false; - System.out.println("Restarting TightVNC Viewer"); - SwingUtilities.invokeLater(new Viewer(this, parser, configurationName)); - } - try { - Thread.sleep(1 * 1000); // 1 second - } catch (InterruptedException e) { - break; + while (!Thread.currentThread().isInterrupted() && deviceIsReady()) { + try { + getTest().projects().devices().keepalive(cloudProjectId, cloudDeviceId).execute(); + } + catch (Exception e) { + e.printStackTrace(); + } + // Restart the viewer if it accidentally crashed. + if (hasCrashed) { + hasCrashed = false; + System.out.println("Restarting TightVNC Viewer"); + currentViewer = new Viewer(this, parser, configurationName); + SwingUtilities.invokeLater(currentViewer); + } + try { + Thread.sleep(1 * 1000); // 1 second + } + catch (InterruptedException e) { + break; + } } + } finally { + tearDown(); } + } + + private void tearDown() { + // Perform tearing down in separate try blocks to make sure that every step gets a chance to be executed. try { - // Delete the cloud device after the viewer is closed. - getTest().projects().devices().delete(cloudProjectId, cloudDeviceId).execute(); // Disconnect adb from the deleted device (otherwise, it will keep showing the stale cloud device). Runtime.getRuntime().exec("./adb disconnect " + deviceAddress, null, workingDir); } catch (Exception exception) { exception.printStackTrace(); } + + try { + // Delete the cloud device after the viewer is closed. + getTest().projects().devices().delete(cloudProjectId, cloudDeviceId).execute(); + } catch (Exception exception) { + exception.printStackTrace(); + } + + try { + //Stop the viewer in case we are tearing down because of the device not being ready. Do it as the last step since it can preclude + //other steps from being executed if the viewer was closed manually. + currentViewer.stopViewer(); + } catch (Exception exception) { + exception.printStackTrace(); + } } private boolean deviceIsReady() { |