Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into display-suites
Browse files Browse the repository at this point in the history
  • Loading branch information
3750 committed Nov 17, 2023
2 parents a13375e + 8d4aaab commit b771578
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 29 deletions.
34 changes: 32 additions & 2 deletions part1.0-introduction/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ runner {
- for features (tests methods) by `runner.parallel.defaultExecutionMode`

Sample configuration:

```groovy
import org.spockframework.runtime.model.parallel.ExecutionMode
Expand All @@ -61,6 +62,7 @@ runner {
}
}
```

- or

```groovy
Expand All @@ -87,6 +89,18 @@ runner {
```groovy
import org.spockframework.runtime.model.parallel.ExecutionMode
runner {
parallel {
enabled true
}
}
```

- or

```groovy
import org.spockframework.runtime.model.parallel.ExecutionMode
runner {
parallel {
enabled true
Expand Down Expand Up @@ -166,12 +180,29 @@ Supported options:

Default: `dynamicWithReservedProcessors(1.0, 2)` (`Runtime.getRuntime().availableProcessors() * 1.0 - 2`)

Example thread pool configuration:

```groovy
runner {
parallel {
enabled true
fixed(4)
}
}
```

Check number of available processors:

```sh
jshell print-available-processors.jsh
```

Sample output:

```
Runtime.getRuntime().availableProcessors() = 10
```

- Add test case to class `A`

```groovy
Expand All @@ -187,13 +218,12 @@ def "test 3"() {
```

- Remove class `C`

- Run
tests `./gradlew --rerun-tasks :part1.0-introduction:test :part1.0-introduction:createTestsExecutionReport -PtotalTimeOfAllTests=false`
- Check [reports](build/reports/tests-execution/html/test.html)
- Configure a thread pool of your choice, run tests and check reports
- Randomize duration of tests. In test `test 3` replace `SLEEP_DURATION`
with `org.apache.commons.lang3.RandomUtils.nextInt(50, 250)`. Run tests and check reports.
with `new Random().nextLong(50, 250)`. Run tests and check reports.

---

Expand Down
1 change: 0 additions & 1 deletion part1.0-introduction/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ dependencies {
testRuntimeOnly("org.junit.platform:junit-platform-launcher")

testImplementation 'org.spockframework:spock-core:2.4-M1-groovy-4.0'
testImplementation 'org.apache.commons:commons-lang3:3.13.0'
}

test {
Expand Down
8 changes: 2 additions & 6 deletions part1.1-shared-state/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ Some tests failed. Eliminate shared state by using different `name` in test case
Sample output:

```text
name = testName 3 readable: false
name = testName 3 readable: true
name = testName 3
name = testName 1
name = testName 2
```
Expand All @@ -136,7 +135,6 @@ Sample output:
name = BM8LX
name = Kbc28
name = boR1b
name = 7Oc3i
```

### Approach no. 3
Expand All @@ -153,7 +151,6 @@ Sample output:
name = 3
name = 1
name = 2
name = 4
```

### Approach no. 4
Expand All @@ -167,8 +164,7 @@ name = 4
Sample output:

```text
name = pl.allegro.tech.workshops.testsparallelexecution:SharedStateExampleTest:should create readable - non-readable file [readable: true, #0]
name = pl.allegro.tech.workshops.testsparallelexecution:SharedStateExampleTest:should create readable - non-readable file [readable: false, #1]
name = pl.allegro.tech.workshops.testsparallelexecution:SharedStateExampleTest:should remove file
name = pl.allegro.tech.workshops.testsparallelexecution:SharedStateExampleTest:should create dir
name = pl.allegro.tech.workshops.testsparallelexecution:SharedStateExampleTest:should create file
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class SharedStateExampleTest extends Specification {
println "name = $name"

when:
def fileCreated = file.createNewFile()
def fileWasCreated = file.createNewFile()

then:
fileCreated
fileWasCreated
file.exists()
}

Expand All @@ -41,31 +41,26 @@ class SharedStateExampleTest extends Specification {
println "name = $name"

when:
def fileCreated = file.mkdir()
def directoryWasCreated = file.mkdir()

then:
fileCreated
directoryWasCreated
file.exists()
}

def "should create readable or non-readable file"() {
def "should remove file"() {
given:
name = "testName"
file = new File(tempDir, name)
file.setReadable(readable)
assert file.createNewFile()
println "name = $name"

when:
def fileCreated = file.createNewFile()
def fileWasDeleted = file.delete()

then:
fileCreated
file.exists()

where:
readable | _
true | _
false | _
fileWasDeleted
!file.exists()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class RemoveFileSpec extends BaseSpec {
def dirName = "test 2"
def file = new File(rootDir, dirName)
file.mkdir()
def filesBeforeRemove = rootDir.list()
def numberOfFilesBeforeRemove = rootDir.list().length

when:
FileService.removeFile(rootDir, dirName)


then:
def filesAfterRemove = rootDir.list()
filesAfterRemove.length == filesBeforeRemove.length - 1
filesAfterRemove.length == numberOfFilesBeforeRemove - 1
}

}
5 changes: 4 additions & 1 deletion part2.1-database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

## Familiarize yourself with this service


<img alt="sequence diagram" src=".readme/sequence.svg" width="75%">

Check [`tests`](src/test/groovy).
Expand Down Expand Up @@ -30,5 +29,9 @@ What to check?
- modify parallel thread pool e.g. `fixed(4)`
- add randomization using `@RandomizedOrder` class annotation (add it to `*Test` class)

#### Result

- there should be no usages of `removeAll()` and `count()` in tests.

---
[home](../README.md)
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class BooksDeleteResourceTest extends BaseBookResourceTest {
given:
def book = Book.of(title, "Davis Stephen R.")
def createdBook = store(book)
// you can access id by `createdBook.id()`

when:
def result = restClient.delete("/books/${createdBook.id()}")
Expand All @@ -37,6 +38,7 @@ class BooksDeleteResourceTest extends BaseBookResourceTest {
def createdBook = store(book)
def otherBook = Book.of("$title (other)", "Davis Stephen R.")
def otherCreatedBook = store(otherBook)
// you can access id by `otherCreatedBook.id()`

when:
def result = restClient.delete("/books/${createdBook.id()}")
Expand Down
9 changes: 8 additions & 1 deletion part2.2-rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Check [`tests`](src/test/groovy).
2. Check [reports](build/reports/tests-execution/html/test.html)
3. Enable parallel execution (in [SpockConfig.groovy](src/test/resources/SpockConfig.groovy))
4. Run tests `./gradlew --rerun-tasks :part2.2-rest:test :part2.2-rest:createTestsExecutionReport --continue`
5. Determine and remove shared state.
5. Temporarily disable test `retry email sending after error response ...` - add `@Ignore` annotation to method
containing this test.
6. Determine and remove shared state.

#### Shared state

Expand All @@ -25,5 +27,10 @@ What to check?
for [stateful behaviour](https://wiremock.org/docs/stateful-behaviour/))
- assertions

#### Stateful Behaviour

One test is ignored. It uses scenarios. Read
about [stateful behaviour and scenarios](https://wiremock.org/docs/stateful-behaviour/). Enable this test and fix

---
[home](../README.md)
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ import static org.springframework.http.HttpStatus.OK
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE

/**
* Hint:
* Hints:
* - https://wiremock.org/docs/request-matching/#json-path
* and https://javadoc.io/doc/com.github.tomakehurst/wiremock/latest/com/github/tomakehurst/wiremock/client/WireMock.html#equalTo(java.lang.String)
* can be used to match request body in stubs and in verifications
*
* - An example payload send to /external-api-service/emails resource:
{
"subject": "New workshops!",
"sender": "from@example.com",
"recipient": "to@example.com"
}
*/
class EmailsByRestResourceTest extends BaseTestWithRest {

Expand Down Expand Up @@ -76,6 +83,8 @@ class EmailsByRestResourceTest extends BaseTestWithRest {
.withStatus(200)
)
)
// sleep to simulate long response
sleep 1000

when:
def result = restClient.post("/emails", email, ProblemDetail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class EmailsByMessageBrokerResourceTest extends BaseTestWithRest {
given:
def email = EmailRequest.of(subject, "[email protected]", "[email protected]")
/**
* Hint:
* Hints:
* - Replace `jsonTopic` with {@link pl.allegro.tech.hermes.mock.HermesMockDefine#jsonTopic(java.lang.String, pl.allegro.tech.hermes.mock.exchange.Response, java.lang.Class, java.util.function.Predicate)}
* - {@link EmailServiceEvent} is as an event class
* - You can define predicate using Groovy closure e.g. { it.name() == name }
Expand All @@ -54,6 +54,8 @@ class EmailsByMessageBrokerResourceTest extends BaseTestWithRest {
given:
def email = EmailRequest.of(subject, sender, "[email protected]")
hermesMock.define().jsonTopic(topic, aResponse().build())
// sleep to simulate long response
sleep 500

when:
def result = restClient.post("/emails", email, EmailRequest)
Expand Down

0 comments on commit b771578

Please sign in to comment.