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

temporalio / sdk-java / #192

02 Oct 2023 11:52PM UTC coverage: 77.37% (-0.05%) from 77.421%
#192

push

github-actions

web-flow
Fix caching in WorkflowLocal/WorkflowThreadLocal (#1876) (#1878)

Reverted caching changes made to WorkflowLocal/WorkflowThreadLocal,
which broke backwards compatibility and accidentally shared values
between Workflows/Threads. Re-implemented caching as an optional
feature, and deprecated the factory methods that created non-caching
instances.

28 of 28 new or added lines in 5 files covered. (100.0%)

18684 of 24149 relevant lines covered (77.37%)

0.77 hits per line

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

69.23
/temporal-sdk/src/main/java/io/temporal/workflow/WorkflowThreadLocal.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.workflow;
22

23
import io.temporal.internal.sync.WorkflowThreadLocalInternal;
24
import java.util.Objects;
25
import java.util.function.Supplier;
26

27
/** {@link ThreadLocal} analog for workflow code. */
28
public final class WorkflowThreadLocal<T> {
29

30
  private final WorkflowThreadLocalInternal<T> impl;
31
  private final Supplier<? extends T> supplier;
32

33
  private WorkflowThreadLocal(Supplier<? extends T> supplier, boolean useCaching) {
1✔
34
    this.supplier = Objects.requireNonNull(supplier);
1✔
35
    this.impl = new WorkflowThreadLocalInternal<>(useCaching);
1✔
36
  }
1✔
37

38
  public WorkflowThreadLocal() {
×
39
    this.supplier = () -> null;
×
40
    this.impl = new WorkflowThreadLocalInternal<>(false);
×
41
  }
×
42

43
  /**
44
   * Create an instance that returns the value returned by the given {@code Supplier} when {@link
45
   * #set(S)} has not yet been called in the thread. Note that the value returned by the {@code
46
   * Supplier} is not stored in the {@code WorkflowThreadLocal} implicitly; repeatedly calling
47
   * {@link #get()} will always re-execute the {@code Supplier} until you call {@link #set(S)} for
48
   * the first time. This differs from the behavior of {@code ThreadLocal}. If you want the value
49
   * returned by the {@code Supplier} to be stored in the {@code WorkflowThreadLocal}, which matches
50
   * the behavior of {@code ThreadLocal}, use {@link #withCachedInitial(Supplier)} instead.
51
   *
52
   * @param supplier Callback that will be executed whenever {@link #get()} is called, until {@link
53
   *     #set(S)} is called for the first time.
54
   * @return A {@code WorkflowThreadLocal} instance.
55
   * @param <S> The type stored in the {@code WorkflowThreadLocal}.
56
   * @deprecated Because the non-caching behavior of this API is typically not desirable, it's
57
   *     recommend to use {@link #withCachedInitial(Supplier)} instead.
58
   */
59
  @Deprecated
60
  public static <S> WorkflowThreadLocal<S> withInitial(Supplier<? extends S> supplier) {
61
    return new WorkflowThreadLocal<>(supplier, false);
1✔
62
  }
63

64
  /**
65
   * Create an instance that returns the value returned by the given {@code Supplier} when {@link
66
   * #set(S)} has not yet been called in the Workflow, and then stores the returned value inside the
67
   * {@code WorkflowThreadLocal}.
68
   *
69
   * @param supplier Callback that will be executed when {@link #get()} is called for the first
70
   *     time, if {@link #set(S)} has not already been called.
71
   * @return A {@code WorkflowThreadLocal} instance.
72
   * @param <S> The type stored in the {@code WorkflowThreadLocal}.
73
   */
74
  public static <S> WorkflowThreadLocal<S> withCachedInitial(Supplier<? extends S> supplier) {
75
    return new WorkflowThreadLocal<>(supplier, true);
1✔
76
  }
77

78
  public T get() {
79
    return impl.get(supplier);
1✔
80
  }
81

82
  public void set(T value) {
83
    impl.set(value);
1✔
84
  }
1✔
85
}
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