Skip to content

Commit 82d2cad

Browse files
committed
Improve tests in part1.2
1 parent 0975ca7 commit 82d2cad

File tree

5 files changed

+27
-32
lines changed

5 files changed

+27
-32
lines changed

part1.2-from-sequential-to-parallel/src/main/groovy/migration/FileService.groovy

+2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ class FileService {
55
static void setText(File parent, String name, String text) {
66
MetricService.incrementInvocationsCount()
77
new File(parent, name).text = text
8+
sleep text.length() * 20
89
}
910

1011
static void appendText(File parent, String name, String text) {
1112
MetricService.incrementInvocationsCount()
1213
new File(parent, name).append(text)
14+
sleep text.length() * 10
1315
}
1416

1517
}

part1.2-from-sequential-to-parallel/src/test/groovy/migration/AppendTextSpec.groovy

+10-12
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,26 @@ package migration
22

33
class AppendTextSpec extends BaseSpec {
44

5-
def "append text"() {
6-
given:
7-
def name = "test"
5+
def fileName = "test"
6+
7+
def setup() {
8+
new File(rootDir, fileName).text = "initial text"
9+
}
810

11+
def "append text"() {
912
when:
10-
FileService.appendText(tempDir, name, "some text")
11-
sleep 100
13+
FileService.appendText(rootDir, fileName, "additional text")
1214

1315
then:
14-
new File(tempDir, name).text == 'some text'
16+
new File(rootDir, fileName).text == 'initial textadditional text'
1517
}
1618

1719
def "append empty text"() {
18-
given:
19-
def name = "test"
20-
2120
when:
22-
FileService.appendText(tempDir, name, '')
23-
sleep 100
21+
FileService.appendText(rootDir, fileName, '')
2422

2523
then:
26-
new File(tempDir, name).text == ''
24+
new File(rootDir, fileName).text == 'initial text'
2725
}
2826

2927
}

part1.2-from-sequential-to-parallel/src/test/groovy/migration/BaseSpec.groovy

+1-8
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ import java.nio.file.Files
77

88
class BaseSpec extends Specification {
99

10-
static File tempDir = Files.createTempDirectory("parallel-test-execution").toFile()
11-
12-
def cleanup() {
13-
tempDir.listFiles().each {
14-
println "Removing $it"
15-
it.delete()
16-
}
17-
}
10+
static File rootDir = Files.createTempDirectory("parallel-test-execution").toFile()
1811

1912
}

part1.2-from-sequential-to-parallel/src/test/groovy/migration/MetricsSpec.groovy

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class MetricsSpec extends BaseSpec {
77
def count = MetricService.getInvocationsCount()
88

99
when:
10-
FileService.appendText(tempDir, "test", "some text")
11-
sleep 100
10+
FileService.appendText(rootDir, "test", "some text")
1211

1312
then:
1413
MetricService.getInvocationsCount() == count + 1
@@ -19,8 +18,7 @@ class MetricsSpec extends BaseSpec {
1918
def count = MetricService.getInvocationsCount()
2019

2120
when:
22-
FileService.setText(tempDir, "test", "some text")
23-
sleep 100
21+
FileService.setText(rootDir, "test", "some text")
2422

2523
then:
2624
MetricService.getInvocationsCount() == count + 1

part1.2-from-sequential-to-parallel/src/test/groovy/migration/SetTextSpec.groovy

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,32 @@ package migration
22

33
class SetTextSpec extends BaseSpec {
44

5+
def cleanup() {
6+
rootDir.listFiles().each {
7+
it.delete()
8+
}
9+
}
10+
511
def "create file with text"() {
612
given:
7-
def name = "test"
13+
def fileName = "test 1"
814

915
when:
10-
FileService.setText(tempDir, name, "some text")
11-
sleep 200
16+
FileService.setText(rootDir, fileName, "some text")
1217

1318
then:
14-
new File(tempDir, name).text == 'some text'
19+
new File(rootDir, fileName).text == 'some text'
1520
}
1621

1722
def "create empty file"() {
1823
given:
19-
def name = "test"
24+
def fileName = "test 2"
2025

2126
when:
22-
FileService.setText(tempDir, name, "")
23-
sleep 200
27+
FileService.setText(rootDir, fileName, "")
2428

2529
then:
26-
new File(tempDir, name).text == ''
30+
new File(rootDir, fileName).text == ''
2731
}
2832

2933
}

0 commit comments

Comments
 (0)