From aaccfb407d51621cdd49af47b425e0ebf40f352a Mon Sep 17 00:00:00 2001 From: danieldisu Date: Tue, 11 May 2021 13:54:48 +0200 Subject: [PATCH 1/6] Implemented repeat attempts by default --- .../flaky/internal/FlakyStatementBuilder.java | 12 +++++ .../rule/flaky/FlakyStatementBuilderTest.java | 46 ++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/schibsted/spain/barista/rule/flaky/internal/FlakyStatementBuilder.java b/library/src/main/java/com/schibsted/spain/barista/rule/flaky/internal/FlakyStatementBuilder.java index 51d83759e..1fd6d3fef 100644 --- a/library/src/main/java/com/schibsted/spain/barista/rule/flaky/internal/FlakyStatementBuilder.java +++ b/library/src/main/java/com/schibsted/spain/barista/rule/flaky/internal/FlakyStatementBuilder.java @@ -11,6 +11,8 @@ public class FlakyStatementBuilder { private Description description; private boolean useAllowFlakyByDefault = false; private int defaultAllowFlakyAttempts = 0; + private int defaultRepeatAttempts = 1; + private boolean useRepeatByDefault = false; public FlakyStatementBuilder setBase(Statement base) { this.base = base; @@ -22,8 +24,16 @@ public FlakyStatementBuilder setDescription(Description description) { return this; } + public FlakyStatementBuilder setRepeatAttemptsByDefault(int attempts) { + useAllowFlakyByDefault = false; + useRepeatByDefault = true; + defaultRepeatAttempts = attempts; + return this; + } + public FlakyStatementBuilder allowFlakyAttemptsByDefault(int attempts) { useAllowFlakyByDefault = true; + useRepeatByDefault = false; defaultAllowFlakyAttempts = attempts; return this; } @@ -46,6 +56,8 @@ public Statement build() { return new AllowFlakyStatement(attempts, base); } else if (useAllowFlakyByDefault) { return new AllowFlakyStatement(defaultAllowFlakyAttempts, base); + } else if (useRepeatByDefault) { + return new RepeatStatement(defaultRepeatAttempts, base); } else { return base; } diff --git a/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java b/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java index ade35e4f5..86acfb777 100644 --- a/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java +++ b/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java @@ -44,6 +44,20 @@ public void repeatStatementReturnedWhenRepeatAnnotationFound() throws Exception assertTrue(resultStatement instanceof RepeatStatement); } + @Test + public void repeatStatementReturnedWhenRepeatSetRepeatAttemptsByDefault() throws Exception { + Statement baseStatement = new SomeStatement(); + Description description = Description.EMPTY; + + Statement resultStatement = new FlakyStatementBuilder() + .setBase(baseStatement) + .setDescription(description) + .setRepeatAttemptsByDefault(3) + .build(); + + assertTrue(resultStatement instanceof RepeatStatement); + } + @Test public void allowFlakyStatementReturnedWhenAllowFlakyAnnotationFound() throws Exception { Statement baseStatement = new SomeStatement(); @@ -68,6 +82,36 @@ public void allowFlakyStatementReturnedWhenNoAnnotationsFoundButUsesDefault() th assertTrue(resultStatement instanceof AllowFlakyStatement); } + @Test + public void lastStatementReturnedWhenRepeatSetRepeatAttemptsByDefaultAndAllowFlakyStatementAtTheSameTime() throws Exception { + Statement baseStatement = new SomeStatement(); + Description description = Description.EMPTY; + + Statement resultStatement = new FlakyStatementBuilder() + .setBase(baseStatement) + .setDescription(description) + .allowFlakyAttemptsByDefault(5) + .setRepeatAttemptsByDefault(3) + .build(); + + assertTrue(resultStatement instanceof RepeatStatement); + } + + @Test + public void allowFlakyStatementReturnedWhenAllowFlakyAnnotationFoundEvenRepeatStatement() throws Exception { + Statement baseStatement = new SomeStatement(); + Description description = Description.EMPTY; + + Statement resultStatement = new FlakyStatementBuilder() + .setBase(baseStatement) + .setDescription(description) + .allowFlakyAttemptsByDefault(5) + .setRepeatAttemptsByDefault(3) + .build(); + + assertTrue(resultStatement instanceof RepeatStatement); + } + //region Shortcut methods private Statement createStatement(Statement baseStatement, Description description) { return new FlakyStatementBuilder() @@ -115,4 +159,4 @@ public void evaluate() throws Throwable { } } //endregion -} \ No newline at end of file +} From 7a3904494b33b87a44f01f66f49d699a52bd0e97 Mon Sep 17 00:00:00 2001 From: danieldisu Date: Tue, 11 May 2021 14:01:00 +0200 Subject: [PATCH 2/6] Expose method in FlakyTestRule --- .../spain/barista/rule/flaky/FlakyTestRule.kt | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/schibsted/spain/barista/rule/flaky/FlakyTestRule.kt b/library/src/main/java/com/schibsted/spain/barista/rule/flaky/FlakyTestRule.kt index 635382782..71f028c33 100644 --- a/library/src/main/java/com/schibsted/spain/barista/rule/flaky/FlakyTestRule.kt +++ b/library/src/main/java/com/schibsted/spain/barista/rule/flaky/FlakyTestRule.kt @@ -29,10 +29,23 @@ class FlakyTestRule : TestRule { return this } + /** + * Utility method to use @[Repeat] by default in all test methods. + *

+ * Use this method when constructing the Rule to apply a default behavior of @[Repeat] without having to add the annotation to + * each test. This can help you to find flaky tests. + *

+ * The default behavior can be overridden with [Repeat] or [Repeat]. + */ + fun repeatAttemptsByDefault(defaultAttempts: Int): FlakyTestRule { + flakyStatementBuilder.setRepeatAttemptsByDefault(defaultAttempts) + return this + } + override fun apply(base: Statement, description: Description): Statement { return flakyStatementBuilder .setBase(base) .setDescription(description) .build() } -} \ No newline at end of file +} From e3ac3d31b76fef6fdb4906000d70edb628fe42f7 Mon Sep 17 00:00:00 2001 From: danieldisu Date: Tue, 11 May 2021 15:57:51 +0200 Subject: [PATCH 3/6] trigger GitHub actions From c60f9f34bd24cb68c1edb9f1ae639183f1c212b4 Mon Sep 17 00:00:00 2001 From: danieldisu Date: Wed, 12 May 2021 08:28:03 +0200 Subject: [PATCH 4/6] Removed bad test and extracted build statement to method --- .../rule/flaky/FlakyStatementBuilderTest.java | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java b/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java index 86acfb777..d32bcb474 100644 --- a/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java +++ b/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java @@ -49,11 +49,7 @@ public void repeatStatementReturnedWhenRepeatSetRepeatAttemptsByDefault() throws Statement baseStatement = new SomeStatement(); Description description = Description.EMPTY; - Statement resultStatement = new FlakyStatementBuilder() - .setBase(baseStatement) - .setDescription(description) - .setRepeatAttemptsByDefault(3) - .build(); + Statement resultStatement = createStatementWithRepeatAttemptsByDefault(baseStatement, description); assertTrue(resultStatement instanceof RepeatStatement); } @@ -73,43 +69,36 @@ public void allowFlakyStatementReturnedWhenNoAnnotationsFoundButUsesDefault() th Statement baseStatement = new SomeStatement(); Description description = Description.EMPTY; - Statement resultStatement = new FlakyStatementBuilder() - .setBase(baseStatement) - .setDescription(description) - .allowFlakyAttemptsByDefault(5) - .build(); + Statement resultStatement = createStatementWithAllowFlakyByDefault(baseStatement, description); assertTrue(resultStatement instanceof AllowFlakyStatement); } @Test - public void lastStatementReturnedWhenRepeatSetRepeatAttemptsByDefaultAndAllowFlakyStatementAtTheSameTime() throws Exception { + public void lastStatementReturnedWhenRepeatAttemptsByDefaultAndAllowFlakyStatementUsedAtTheSameTime() throws Exception { Statement baseStatement = new SomeStatement(); Description description = Description.EMPTY; - Statement resultStatement = new FlakyStatementBuilder() + Statement resultStatement = createStatementWithFlakyAndReturn(baseStatement, description); + + assertTrue(resultStatement instanceof RepeatStatement); + } + + private Statement createStatementWithFlakyAndReturn(Statement baseStatement, Description description) { + return new FlakyStatementBuilder() .setBase(baseStatement) .setDescription(description) .allowFlakyAttemptsByDefault(5) .setRepeatAttemptsByDefault(3) .build(); - - assertTrue(resultStatement instanceof RepeatStatement); } - @Test - public void allowFlakyStatementReturnedWhenAllowFlakyAnnotationFoundEvenRepeatStatement() throws Exception { - Statement baseStatement = new SomeStatement(); - Description description = Description.EMPTY; - - Statement resultStatement = new FlakyStatementBuilder() + private Statement createStatementWithAllowFlakyByDefault(Statement baseStatement, Description description) { + return new FlakyStatementBuilder() .setBase(baseStatement) .setDescription(description) .allowFlakyAttemptsByDefault(5) - .setRepeatAttemptsByDefault(3) .build(); - - assertTrue(resultStatement instanceof RepeatStatement); } //region Shortcut methods @@ -120,6 +109,14 @@ private Statement createStatement(Statement baseStatement, Description descripti .build(); } + private Statement createStatementWithRepeatAttemptsByDefault(Statement baseStatement, Description description) { + return new FlakyStatementBuilder() + .setBase(baseStatement) + .setDescription(description) + .setRepeatAttemptsByDefault(3) + .build(); + } + private Description withAnnotations(Annotation... annotations) { return Description.createTestDescription(CLASS, TEST, annotations); } From 8e7463ea89cd518a81c7db2c26a6aa03a9c6b13e Mon Sep 17 00:00:00 2001 From: danieldisu Date: Wed, 12 May 2021 10:01:55 +0200 Subject: [PATCH 5/6] trigger GitHub actions From 917c205f3834336f1a7553d80e02fe7d4c9c795f Mon Sep 17 00:00:00 2001 From: danieldisu Date: Mon, 17 May 2021 08:28:57 +0200 Subject: [PATCH 6/6] Implemented PR suggestions --- .../com/schibsted/spain/barista/rule/flaky/FlakyTestRule.kt | 2 +- .../spain/barista/rule/flaky/FlakyStatementBuilderTest.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/schibsted/spain/barista/rule/flaky/FlakyTestRule.kt b/library/src/main/java/com/schibsted/spain/barista/rule/flaky/FlakyTestRule.kt index 71f028c33..335afbaab 100644 --- a/library/src/main/java/com/schibsted/spain/barista/rule/flaky/FlakyTestRule.kt +++ b/library/src/main/java/com/schibsted/spain/barista/rule/flaky/FlakyTestRule.kt @@ -35,7 +35,7 @@ class FlakyTestRule : TestRule { * Use this method when constructing the Rule to apply a default behavior of @[Repeat] without having to add the annotation to * each test. This can help you to find flaky tests. *

- * The default behavior can be overridden with [Repeat] or [Repeat]. + * The default behavior can be overridden with [Repeat] or [AllowFlaky]. */ fun repeatAttemptsByDefault(defaultAttempts: Int): FlakyTestRule { flakyStatementBuilder.setRepeatAttemptsByDefault(defaultAttempts) diff --git a/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java b/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java index d32bcb474..1e7816bae 100644 --- a/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java +++ b/library/src/test/java/com/schibsted/spain/barista/rule/flaky/FlakyStatementBuilderTest.java @@ -45,7 +45,7 @@ public void repeatStatementReturnedWhenRepeatAnnotationFound() throws Exception } @Test - public void repeatStatementReturnedWhenRepeatSetRepeatAttemptsByDefault() throws Exception { + public void repeatStatementReturnedWhenSettingDefaultRepeatAttempts() throws Exception { Statement baseStatement = new SomeStatement(); Description description = Description.EMPTY; @@ -75,7 +75,7 @@ public void allowFlakyStatementReturnedWhenNoAnnotationsFoundButUsesDefault() th } @Test - public void lastStatementReturnedWhenRepeatAttemptsByDefaultAndAllowFlakyStatementUsedAtTheSameTime() throws Exception { + public void lastStatementReturnedWhenDefaultRepeatAttemptsAndAllowFlakyStatementUsedAtTheSameTime() throws Exception { Statement baseStatement = new SomeStatement(); Description description = Description.EMPTY;