You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RetryOnFail.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.tb3;
  17. import java.util.logging.Logger;
  18. import org.junit.rules.TestRule;
  19. import org.junit.runner.Description;
  20. import org.junit.runners.model.Statement;
  21. public class RetryOnFail implements TestRule {
  22. @Override
  23. public Statement apply(Statement base, Description description) {
  24. return statement(base, description);
  25. }
  26. private Statement statement(final Statement base,
  27. final Description description) {
  28. return new Statement() {
  29. @Override
  30. public void evaluate() throws Throwable {
  31. Throwable caughtThrowable = null;
  32. int retryCount = getRetryCount();
  33. for (int i = 0; i <= retryCount; i++) {
  34. try {
  35. base.evaluate();
  36. return;
  37. } catch (Throwable t) {
  38. caughtThrowable = t;
  39. System.err
  40. .println(String.format("%s: run %s/%s failed.",
  41. description.getDisplayName(), i + 1,
  42. retryCount + 1));
  43. System.err.println(t.getMessage());
  44. }
  45. }
  46. throw caughtThrowable;
  47. }
  48. private int getRetryCount() {
  49. String retryCount = System
  50. .getProperty("com.vaadin.testbench.max.retries");
  51. if (retryCount != null && !retryCount.trim().isEmpty()) {
  52. try {
  53. return Integer.parseInt(retryCount);
  54. } catch (NumberFormatException e) {
  55. // TODO: See how this was implemented in TestBench
  56. Logger.getLogger(RetryOnFail.class.getName()).warning(
  57. "Could not parse max retry count. Retry count set to 0. Failed value: "
  58. + retryCount);
  59. }
  60. }
  61. return 0;
  62. }
  63. };
  64. }
  65. }