forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockCracContext.java
More file actions
56 lines (47 loc) · 1.75 KB
/
MockCracContext.java
File metadata and controls
56 lines (47 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry;
import java.util.ArrayList;
import java.util.List;
import org.crac.Context;
import org.crac.Resource;
/**
* A test-only {@link Context} that allows simulating CRaC checkpoint and restore lifecycle events
* without requiring a CRaC-enabled JDK. Register resources with {@link #register(Resource)}, then
* call {@link #simulateCheckpoint()} and {@link #simulateRestore()} to drive the lifecycle.
*
* <p>Notification order follows the CRaC specification: checkpoint callbacks fire in reverse
* registration order; restore callbacks fire in forward registration order.
*/
final class MockCracContext extends Context<Resource> {
private final List<Resource> resources = new ArrayList<>();
@Override
public void register(Resource resource) {
resources.add(resource);
}
/**
* Simulates a CRaC checkpoint by invoking {@link Resource#beforeCheckpoint} on all registered
* resources in reverse registration order, as the CRaC spec requires.
*/
void simulateCheckpoint() throws Exception {
for (int i = resources.size() - 1; i >= 0; i--) {
resources.get(i).beforeCheckpoint(this);
}
}
/**
* Simulates a CRaC restore by invoking {@link Resource#afterRestore} on all registered resources
* in forward registration order, as the CRaC spec requires.
*/
void simulateRestore() throws Exception {
for (Resource resource : resources) {
resource.afterRestore(this);
}
}
// Not used: this context is not itself registered with a parent context.
@Override
public void beforeCheckpoint(Context<? extends Resource> context) {}
@Override
public void afterRestore(Context<? extends Resource> context) {}
}