-
Notifications
You must be signed in to change notification settings - Fork 0
VCR util for unit tests #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,6 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| cd "$(dirname "$(readlink -f "${BASH_SOURCE}")")"/.. | ||
|
|
||
| rm -rf src/test/resources/cassettes/ | ||
| mkdir -p src/test/resources/cassettes/ |
This file contains hidden or 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,6 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| cd "$(dirname "$(readlink -f "${BASH_SOURCE}")")"/.. | ||
|
|
||
| export VCR_MODE=record | ||
| ./gradlew test |
This file contains hidden or 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 hidden or 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,84 @@ | ||
| package dev.braintrust; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| import io.opentelemetry.sdk.common.CompletableResultCode; | ||
| import io.opentelemetry.sdk.trace.data.SpanData; | ||
| import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
| import java.util.*; | ||
| import java.util.concurrent.ConcurrentLinkedQueue; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.locks.Condition; | ||
| import java.util.concurrent.locks.Lock; | ||
| import java.util.concurrent.locks.ReentrantLock; | ||
| import lombok.SneakyThrows; | ||
|
|
||
| /** | ||
| * an in memory span exporter which allows for blocking until the exported span count reaches an | ||
| * expected min | ||
| */ | ||
| public class UnitTestSpanExporter implements SpanExporter { | ||
| private final Queue<SpanData> finishedSpanItems = new ConcurrentLinkedQueue<>(); | ||
| private final Lock lock = new ReentrantLock(); | ||
| private final Condition spansAdded = lock.newCondition(); | ||
| private boolean isStopped = false; | ||
|
|
||
| public UnitTestSpanExporter() {} | ||
|
|
||
| @SneakyThrows | ||
| public List<SpanData> getFinishedSpanItems(int minSpanCount) { | ||
| long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(30); | ||
| lock.lock(); | ||
| try { | ||
| while (finishedSpanItems.size() < minSpanCount) { | ||
| long remainingNanos = deadline - System.nanoTime(); | ||
| if (remainingNanos <= 0) { | ||
| fail( | ||
| String.format( | ||
| "Timeout waiting for spans: expected at least %d spans, but got" | ||
| + " %d after 30 seconds", | ||
| minSpanCount, finishedSpanItems.size())); | ||
| } | ||
| spansAdded.awaitNanos(remainingNanos); | ||
| } | ||
| return Collections.unmodifiableList(new ArrayList<>(finishedSpanItems)); | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
| } | ||
|
|
||
| public List<SpanData> getFinishedSpanItems() { | ||
| return Collections.unmodifiableList(new ArrayList<>(finishedSpanItems)); | ||
| } | ||
|
|
||
| public void reset() { | ||
| finishedSpanItems.clear(); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableResultCode export(Collection<SpanData> spans) { | ||
| if (isStopped) { | ||
| return CompletableResultCode.ofFailure(); | ||
| } | ||
| lock.lock(); | ||
| try { | ||
| finishedSpanItems.addAll(spans); | ||
| spansAdded.signalAll(); | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
| return CompletableResultCode.ofSuccess(); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableResultCode flush() { | ||
| return CompletableResultCode.ofSuccess(); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableResultCode shutdown() { | ||
| finishedSpanItems.clear(); | ||
| isStopped = true; | ||
| return CompletableResultCode.ofSuccess(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@delner -- this is follow-up from the langchain PR. I ended up making my own span exporter that allows waiting for spans without sleeping