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

temporalio / sdk-java / #288

30 Jul 2024 05:58PM UTC coverage: 77.6% (+0.04%) from 77.557%
#288

push

github

web-flow
Workflow-friendly concurrency primitives (#2133)

Workflow-friendly concurrency primitives

60 of 64 new or added lines in 4 files covered. (93.75%)

3 existing lines in 1 file now uncovered.

19719 of 25411 relevant lines covered (77.6%)

0.78 hits per line

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

93.94
/temporal-sdk/src/main/java/io/temporal/internal/sync/WorkflowSemaphoreImpl.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 static io.temporal.internal.sync.WorkflowInternal.assertNotReadOnly;
24

25
import com.google.common.base.Preconditions;
26
import io.temporal.workflow.CancellationScope;
27
import io.temporal.workflow.WorkflowSemaphore;
28
import java.time.Duration;
29

30
class WorkflowSemaphoreImpl implements WorkflowSemaphore {
31
  private int currentPermits;
32

33
  public WorkflowSemaphoreImpl(int permits) {
1✔
34
    this.currentPermits = permits;
1✔
35
  }
1✔
36

37
  @Override
38
  public void acquire() {
NEW
39
    acquire(1);
×
NEW
40
  }
×
41

42
  @Override
43
  public void acquire(int permits) {
44
    Preconditions.checkArgument(
1✔
45
        permits >= 0, "WorkflowSemaphore.acquire called with negative permits");
46
    WorkflowInternal.await(
1✔
47
        "WorkflowSemaphore.acquire",
48
        () -> {
49
          CancellationScope.throwCanceled();
1✔
50
          return currentPermits >= permits;
1✔
51
        });
52
    currentPermits -= permits;
1✔
53
  }
1✔
54

55
  @Override
56
  public boolean tryAcquire() {
57
    return tryAcquire(1);
1✔
58
  }
59

60
  @Override
61
  public boolean tryAcquire(Duration timeout) {
62
    return tryAcquire(1, timeout);
1✔
63
  }
64

65
  @Override
66
  public boolean tryAcquire(int permits) {
67
    assertNotReadOnly("WorkflowSemaphore.tryAcquire");
1✔
68
    Preconditions.checkArgument(
1✔
69
        permits >= 0, "WorkflowSemaphore.tryAcquire called with negative permits");
70
    if (currentPermits >= permits) {
1✔
71
      currentPermits -= permits;
1✔
72
      return true;
1✔
73
    }
74
    return false;
1✔
75
  }
76

77
  @Override
78
  public boolean tryAcquire(int permits, Duration timeout) {
79
    Preconditions.checkArgument(
1✔
80
        permits >= 0, "WorkflowSemaphore.tryAcquire called with negative permits");
81
    boolean acquired =
1✔
82
        WorkflowInternal.await(
1✔
83
            timeout,
84
            "WorkflowSemaphore.tryAcquire",
85
            () -> {
86
              CancellationScope.throwCanceled();
1✔
87
              return currentPermits >= permits;
1✔
88
            });
89
    if (acquired) {
1✔
90
      currentPermits -= permits;
1✔
91
    }
92
    return acquired;
1✔
93
  }
94

95
  @Override
96
  public void release() {
97
    release(1);
1✔
98
  }
1✔
99

100
  @Override
101
  public void release(int permits) {
102
    assertNotReadOnly("WorkflowSemaphore.release");
1✔
103
    Preconditions.checkArgument(
1✔
104
        permits >= 0, "WorkflowSemaphore.release called with negative permits");
105
    currentPermits += permits;
1✔
106
  }
1✔
107
}
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

© 2026 Coveralls, Inc