• 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

83.33
/temporal-sdk/src/main/java/io/temporal/internal/sync/WorkflowThreadLocalInternal.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.internal.sync;
22

23
import java.util.Optional;
24
import java.util.function.Supplier;
25

26
public final class WorkflowThreadLocalInternal<T> {
27

28
  private final boolean useCaching;
29

30
  public WorkflowThreadLocalInternal() {
31
    this(false);
×
32
  }
×
33

34
  public WorkflowThreadLocalInternal(boolean useCaching) {
1✔
35
    this.useCaching = useCaching;
1✔
36
  }
1✔
37

38
  public T get(Supplier<? extends T> supplier) {
39
    Optional<Optional<T>> result =
40
        DeterministicRunnerImpl.currentThreadInternal().getThreadLocal(this);
1✔
41
    T out = result.orElseGet(() -> Optional.ofNullable(supplier.get())).orElse(null);
1✔
42
    if (!result.isPresent() && useCaching) {
1✔
43
      // This is the first time we've tried fetching this, and caching is enabled. Store it.
44
      set(out);
1✔
45
    }
46
    return out;
1✔
47
  }
48

49
  public void set(T value) {
50
    DeterministicRunnerImpl.currentThreadInternal().setThreadLocal(this, value);
1✔
51
  }
1✔
52
}
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