Skip to content

Commit 2171da1

Browse files
cache: cache.value + docs (#806)
1 parent 664a647 commit 2171da1

16 files changed

Lines changed: 204 additions & 18 deletions

File tree

webtau-cache/src/main/java/org/testingisdocumenting/webtau/cache/Cache.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2021 webtau maintainers
23
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,6 +18,12 @@
1718
package org.testingisdocumenting.webtau.cache;
1819

1920
import org.testingisdocumenting.webtau.cfg.WebTauConfig;
21+
import org.testingisdocumenting.webtau.reporter.StepReportOptions;
22+
import org.testingisdocumenting.webtau.reporter.WebTauStep;
23+
24+
import static org.testingisdocumenting.webtau.reporter.IntegrationTestsMessageBuilder.*;
25+
import static org.testingisdocumenting.webtau.reporter.IntegrationTestsMessageBuilder.stringValue;
26+
import static org.testingisdocumenting.webtau.reporter.TokenizedMessage.tokenizedMessage;
2027

2128
public class Cache {
2229
public static final Cache cache = new Cache();
@@ -27,15 +34,29 @@ private Cache() {
2734
fileBasedCache = new FileBasedCache(() -> WebTauConfig.getCfg().getCachePath());
2835
}
2936

37+
public <E> CachedValue<E> value(String id) {
38+
return new CachedValue<>(cache, id);
39+
}
40+
3041
public <E> E get(String key) {
31-
return fileBasedCache.get(key);
42+
WebTauStep step = WebTauStep.createStep(null,
43+
tokenizedMessage(action("getting cached value"), FROM, id(key)),
44+
(r) -> tokenizedMessage(action("got cached value"), FROM, id(key), COLON, stringValue(r)),
45+
() -> fileBasedCache.get(key));
46+
47+
return step.execute(StepReportOptions.SKIP_START);
3248
}
3349

3450
public void put(String key, Object value, long expirationTime) {
35-
fileBasedCache.put(key, value, expirationTime);
51+
WebTauStep step = WebTauStep.createStep(null,
52+
tokenizedMessage(action("caching value"), AS, id(key), COLON, stringValue(value)),
53+
() -> tokenizedMessage(action("cached value"), AS, id(key), COLON, stringValue(value)),
54+
() -> fileBasedCache.put(key, value, expirationTime));
55+
56+
step.execute(StepReportOptions.SKIP_START);
3657
}
3758

3859
public void put(String key, Object value) {
39-
fileBasedCache.put(key, value);
60+
put(key, value, Long.MAX_VALUE);
4061
}
4162
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2021 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.testingisdocumenting.webtau.cache;
18+
19+
public class CachedValue<E> {
20+
private final Cache cache;
21+
private final String id;
22+
23+
public CachedValue(Cache cache, String id) {
24+
this.cache = cache;
25+
this.id = id;
26+
}
27+
28+
public E get() {
29+
return cache.get(id);
30+
}
31+
32+
public void set(E value) {
33+
cache.put(id, value);
34+
}
35+
}

webtau-cache/src/main/java/org/testingisdocumenting/webtau/cache/FileBasedCache.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import java.util.concurrent.atomic.AtomicLong;
3131
import java.util.function.Supplier;
3232

33-
public class FileBasedCache {
33+
class FileBasedCache {
3434
private static final String VALUE_KEY = "value";
3535
private static final String EXPIRATION_TIME_KEY = "expirationTime";
3636

@@ -81,7 +81,6 @@ public void put(String key, Object value, long expirationTime) {
8181

8282
public void put(String key, Object value) {
8383
put(key, value, Long.MAX_VALUE);
84-
flushCacheToDiskIfRequired();
8584
}
8685

8786
private Map<String, Object> createValueWithMeta(Object value, long expirationTime) {

webtau-cache/src/test/groovy/org/testingisdocumenting/webtau/cache/FileBasedCacheTest.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2021 webtau maintainers
23
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -92,8 +93,8 @@ class FileBasedCacheTest {
9293
void "should create non expiring values if no expiration time is provided"() {
9394
def cacheFile = createTempCacheFile([:])
9495

95-
def fileBasedCache = new FileBasedCache({ -> cacheFile })
9696
Time.timeProvider = new DummyTimeProvider([0, 11_000])
97+
def fileBasedCache = new FileBasedCache({ -> cacheFile })
9798
fileBasedCache.put('accessToken', 'abc')
9899

99100
Assert.assertEquals(String.format('{%n' +

webtau-core/src/main/java/org/testingisdocumenting/webtau/reporter/IntegrationTestsMessageBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public MessageToken token(Object value) {
6767
public static final MessageToken OF = TokenTypes.PREPOSITION.token("of");
6868
public static final MessageToken FOR = TokenTypes.PREPOSITION.token("for");
6969
public static final MessageToken FROM = TokenTypes.PREPOSITION.token("from");
70+
public static final MessageToken AS = TokenTypes.PREPOSITION.token("as");
7071
public static final MessageToken USING = TokenTypes.PREPOSITION.token("using");
7172
public static final MessageToken INTO = TokenTypes.PREPOSITION.token("into");
7273
public static final MessageToken ON = TokenTypes.PREPOSITION.token("on");

webtau-docs/znai/toc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ cli
4949
introduction
5050
foreground-command
5151
background-command
52+
REPL
53+
experiments
54+
test-runs
5255
utilities
5356
introduction
5457
data
5558
file-system
59+
cache
5660
groovy-standalone-runner
5761
introduction
5862
data-driven-scenarios
@@ -62,9 +66,6 @@ groovy-standalone-runner
6266
test-metadata
6367
test-listeners
6468
test-execution
65-
REPL
66-
experiments
67-
test-runs
6869
configuration
6970
environments
7071
options
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Cached Value
2+
3+
When you develop tests, you don't have to restart the whole flow from the beginning.
4+
5+
Imagine you have a multi step test that includes running Command Line tool and then validating REST API response, and
6+
then opening a browser to assert UI values.
7+
8+
Cache long steps results like created entities `ids` to speed up tests development.
9+
10+
In the example below, you create first scenario that runs a heavy command line tool that generates an id.
11+
We then cache the value and you can write a second scenario and keep re-running it, without the need to re-run the first one.
12+
13+
:include-file: scenarios/cache/cachedValue.groovy {
14+
title: "cached value",
15+
startLine: "example",
16+
endLine: "example",
17+
excludeStartEnd: true,
18+
commentsType: "inline"
19+
}
20+
21+
Note: Use [Selective Run](groovy-standalone-runner/selective-run) or [REPL mode](REPL/test-runs)
22+
to run one scenario at a time.

webtau-docs/znai/utilities/introduction.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ Webtau has several modules to help you simplify automation processes. All operat
22
detailed information on steps executed and details are included in web report as well as console output.
33

44
* [data](utilities/data) - module to read structured data like CSV and JSON into data structures like `TableData`, lists and maps
5-
* [file system](utilities/file-system) - module to create files, directories, reading data, unzipping, etc
5+
* [file system](utilities/file-system) - module to create files, directories, reading data, unzipping, etc
6+
* [cache](utilities/cache) - module to cache results of test steps to be able to skip them during test development and exploration
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2021 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package scenarios.cache
18+
19+
// example
20+
import static org.testingisdocumenting.webtau.WebTauGroovyDsl.*
21+
22+
def createdId = cache.value("cli-heavy-created-id") // declare cached value with distinct id
23+
24+
scenario("heavy setup operation") {
25+
def cliResult = cli.run("scripts/cli-heavy-process") // long running process that you don't want to re-run as you write your tests
26+
createdId.set(cliResult.extractFromOutputByRegexp("id=(\\S+)")) // caching the extracted id from CLI run
27+
}
28+
29+
scenario("using previous setup id even after restart") {
30+
def id = createdId.get() // using cached value from previous test run. value will be preserved between restarts and re-compile
31+
http.get("/resource/${id}") {
32+
message.should == "hello"
33+
}
34+
}
35+
// example
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
echo heavy process start
3+
echo id=generated-id-123

0 commit comments

Comments
 (0)