-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #998 from ithinkicancode/fix/thread-safe-cyclic-re…
…sponses Made thenRespondCyclic* functions thread-safe
- Loading branch information
Showing
6 changed files
with
88 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
core/src/main/scala/sttp/client3/testing/AtomicCyclicIterator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package sttp.client3.testing | ||
|
||
import java.util.concurrent.atomic.AtomicInteger | ||
import java.util.function.IntUnaryOperator | ||
import scala.util.{Failure, Success, Try} | ||
|
||
final class AtomicCyclicIterator[+T] private(val elements: Seq[T]) { | ||
private val vector = elements.toVector | ||
private val lastIndex = elements.length - 1 | ||
private val currentIndex = new AtomicInteger(0) | ||
|
||
private val toNextIndex = new IntUnaryOperator { | ||
final override def applyAsInt(i: Int): Int = | ||
if (i == lastIndex) 0 else i + 1 | ||
} | ||
|
||
def next(): T = { | ||
val index = currentIndex.getAndUpdate(toNextIndex) | ||
vector(index) | ||
} | ||
} | ||
|
||
object AtomicCyclicIterator { | ||
|
||
def tryFrom[T](elements: Seq[T]): Try[AtomicCyclicIterator[T]] = { | ||
if (elements.nonEmpty) | ||
Success(new AtomicCyclicIterator(elements)) | ||
else | ||
Failure(new IllegalArgumentException("Argument must be a non-empty collection.")) | ||
} | ||
|
||
def unsafeFrom[T](elements: Seq[T]): AtomicCyclicIterator[T] = tryFrom(elements).get | ||
|
||
def apply[T](head: T, tail: Seq[T]): AtomicCyclicIterator[T] = unsafeFrom(head +: tail) | ||
|
||
def of[T](head: T, tail: T*): AtomicCyclicIterator[T] = apply(head, tail) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters