• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #333

16 Oct 2024 07:28PM UTC coverage: 78.65% (+0.6%) from 78.085%
#333

push

github

web-flow
Fix code coverage (#2275)

22670 of 28824 relevant lines covered (78.65%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.15
/temporal-testing/src/main/java/io/temporal/testing/TimeLockingInterceptor.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.testing;
22

23
import io.temporal.api.common.v1.WorkflowExecution;
24
import io.temporal.client.*;
25
import io.temporal.common.interceptors.WorkflowClientInterceptorBase;
26
import io.temporal.serviceclient.TestServiceStubs;
27
import java.lang.reflect.Type;
28
import java.util.Optional;
29
import java.util.concurrent.CompletableFuture;
30
import java.util.concurrent.ExecutionException;
31
import java.util.concurrent.TimeUnit;
32
import java.util.concurrent.TimeoutException;
33
import javax.annotation.Nullable;
34

35
class TimeLockingInterceptor extends WorkflowClientInterceptorBase {
36

37
  private final IdempotentTimeLocker locker;
38

39
  TimeLockingInterceptor(TestServiceStubs testServiceStubs) {
1✔
40
    this.locker = new IdempotentTimeLocker(testServiceStubs);
1✔
41
  }
1✔
42

43
  @Deprecated
44
  @Override
45
  public WorkflowStub newUntypedWorkflowStub(
46
      String workflowType, WorkflowOptions options, WorkflowStub next) {
47
    return new TimeLockingWorkflowStub(locker, next);
1✔
48
  }
49

50
  @Deprecated
51
  @Override
52
  public WorkflowStub newUntypedWorkflowStub(
53
      WorkflowExecution execution, Optional<String> workflowType, WorkflowStub next) {
54
    return new TimeLockingWorkflowStub(locker, next);
1✔
55
  }
56

57
  static class TimeLockingWorkflowStub implements WorkflowStub {
58

59
    private final IdempotentTimeLocker locker;
60
    private final WorkflowStub next;
61

62
    TimeLockingWorkflowStub(IdempotentTimeLocker locker, WorkflowStub next) {
1✔
63
      this.locker = locker;
1✔
64
      this.next = next;
1✔
65
    }
1✔
66

67
    @Override
68
    public void signal(String signalName, Object... args) {
69
      next.signal(signalName, args);
1✔
70
    }
1✔
71

72
    @Override
73
    public WorkflowExecution start(Object... args) {
74
      return next.start(args);
1✔
75
    }
76

77
    @Override
78
    public <R> WorkflowUpdateHandle<R> updateWithStart(
79
        UpdateWithStartWorkflowOperation<R> updateOperation, Object... args) {
80
      return next.updateWithStart(updateOperation, args);
1✔
81
    }
82

83
    @Override
84
    public WorkflowExecution signalWithStart(
85
        String signalName, Object[] signalArgs, Object[] startArgs) {
86
      return next.signalWithStart(signalName, signalArgs, startArgs);
1✔
87
    }
88

89
    @Override
90
    public Optional<String> getWorkflowType() {
91
      return next.getWorkflowType();
1✔
92
    }
93

94
    @Override
95
    public WorkflowExecution getExecution() {
96
      return next.getExecution();
1✔
97
    }
98

99
    @Override
100
    public <R> R getResult(Class<R> resultClass, Type resultType) {
101
      locker.unlockTimeSkipping();
1✔
102
      try {
103
        return next.getResult(resultClass, resultType);
1✔
104
      } finally {
105
        locker.lockTimeSkipping();
1✔
106
      }
107
    }
108

109
    @Override
110
    public <R> R getResult(Class<R> resultClass) {
111
      locker.unlockTimeSkipping();
1✔
112
      try {
113
        return next.getResult(resultClass);
1✔
114
      } finally {
115
        locker.lockTimeSkipping();
1✔
116
      }
117
    }
118

119
    @Override
120
    public <R> CompletableFuture<R> getResultAsync(Class<R> resultClass, Type resultType) {
121
      return new TimeLockingWorkflowStub.TimeLockingFuture<>(
1✔
122
          next.getResultAsync(resultClass, resultType));
1✔
123
    }
124

125
    @Override
126
    public <R> CompletableFuture<R> getResultAsync(Class<R> resultClass) {
127
      return new TimeLockingWorkflowStub.TimeLockingFuture<>(next.getResultAsync(resultClass));
1✔
128
    }
129

130
    @Override
131
    public <R> R getResult(long timeout, TimeUnit unit, Class<R> resultClass, Type resultType)
132
        throws TimeoutException {
133
      locker.unlockTimeSkipping();
×
134
      try {
135
        return next.getResult(timeout, unit, resultClass, resultType);
×
136
      } finally {
137
        locker.lockTimeSkipping();
×
138
      }
139
    }
140

141
    @Override
142
    public <R> R getResult(long timeout, TimeUnit unit, Class<R> resultClass)
143
        throws TimeoutException {
144
      locker.unlockTimeSkipping();
1✔
145
      try {
146
        return next.getResult(timeout, unit, resultClass);
×
147
      } finally {
148
        locker.lockTimeSkipping();
1✔
149
      }
150
    }
151

152
    @Override
153
    public <R> CompletableFuture<R> getResultAsync(
154
        long timeout, TimeUnit unit, Class<R> resultClass, Type resultType) {
155
      return new TimeLockingWorkflowStub.TimeLockingFuture<>(
×
156
          next.getResultAsync(timeout, unit, resultClass, resultType));
×
157
    }
158

159
    @Override
160
    public <R> CompletableFuture<R> getResultAsync(
161
        long timeout, TimeUnit unit, Class<R> resultClass) {
162
      return new TimeLockingWorkflowStub.TimeLockingFuture<>(
1✔
163
          next.getResultAsync(timeout, unit, resultClass));
1✔
164
    }
165

166
    @Override
167
    public <R> R query(String queryType, Class<R> resultClass, Object... args) {
168
      return next.query(queryType, resultClass, args);
1✔
169
    }
170

171
    @Override
172
    public <R> R query(String queryType, Class<R> resultClass, Type resultType, Object... args) {
173
      return next.query(queryType, resultClass, resultType, args);
1✔
174
    }
175

176
    @Override
177
    public void cancel() {
178
      next.cancel();
1✔
179
    }
1✔
180

181
    @Override
182
    public void terminate(@Nullable String reason, Object... details) {
183
      next.terminate(reason, details);
1✔
184
    }
1✔
185

186
    @Override
187
    public Optional<WorkflowOptions> getOptions() {
188
      return next.getOptions();
1✔
189
    }
190

191
    @Override
192
    public WorkflowStub newInstance(WorkflowOptions options) {
193
      return new TimeLockingWorkflowStub(locker, next.newInstance(options));
×
194
    }
195

196
    /** Unlocks time skipping before blocking calls and locks back after completion. */
197
    private class TimeLockingFuture<R> extends CompletableFuture<R> {
198

199
      public TimeLockingFuture(CompletableFuture<R> resultAsync) {
1✔
200
        @SuppressWarnings({"FutureReturnValueIgnored", "unused"})
201
        CompletableFuture<R> ignored =
1✔
202
            resultAsync.whenComplete(
1✔
203
                (r, e) -> {
204
                  if (e == null) {
1✔
205
                    this.complete(r);
1✔
206
                  } else {
207
                    this.completeExceptionally(e);
1✔
208
                  }
209
                });
1✔
210
      }
1✔
211

212
      @Override
213
      public R get() throws InterruptedException, ExecutionException {
214
        locker.unlockTimeSkipping();
1✔
215
        try {
216
          return super.get();
1✔
217
        } finally {
218
          locker.lockTimeSkipping();
1✔
219
        }
220
      }
221

222
      @Override
223
      public R get(long timeout, TimeUnit unit)
224
          throws InterruptedException, ExecutionException, TimeoutException {
225
        locker.unlockTimeSkipping();
1✔
226
        try {
227
          return super.get(timeout, unit);
×
228
        } finally {
229
          locker.lockTimeSkipping();
1✔
230
        }
231
      }
232

233
      @Override
234
      public R join() {
235
        locker.unlockTimeSkipping();
1✔
236
        try {
237
          return super.join();
1✔
238
        } finally {
239
          locker.lockTimeSkipping();
1✔
240
        }
241
      }
242
    }
243

244
    @Override
245
    public <R> R update(String updateName, Class<R> resultClass, Object... args) {
246
      return next.update(updateName, resultClass, args);
1✔
247
    }
248

249
    @Override
250
    public <R> WorkflowUpdateHandle<R> startUpdate(
251
        String updateName, WorkflowUpdateStage waitForStage, Class<R> resultClass, Object... args) {
252
      return next.startUpdate(updateName, waitForStage, resultClass, args);
1✔
253
    }
254

255
    @Override
256
    public <R> WorkflowUpdateHandle<R> startUpdate(UpdateOptions<R> options, Object... args) {
257
      return next.startUpdate(options, args);
1✔
258
    }
259

260
    @Override
261
    public <R> WorkflowUpdateHandle<R> getUpdateHandle(String updateId, Class<R> resultClass) {
262
      return next.getUpdateHandle(updateId, resultClass);
1✔
263
    }
264

265
    @Override
266
    public <R> WorkflowUpdateHandle<R> getUpdateHandle(
267
        String updateId, Class<R> resultClass, Type resultType) {
268
      return next.getUpdateHandle(updateId, resultClass, resultType);
×
269
    }
270
  }
271
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc