-
Notifications
You must be signed in to change notification settings - Fork 2
Add DurableFuture#or_timeout + Restate::TimeoutError #12
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
Open
junyuanz1
wants to merge
3
commits into
restatedev:main
Choose a base branch
from
junyuanz1:add-or-timeout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
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
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
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,125 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
| require 'restate' | ||
|
|
||
| # Unit coverage for +DurableFuture#or_timeout+ and | ||
| # +DurableCallFuture#or_timeout+. Stubs the +Restate+ module-level | ||
| # helpers (+sleep+, +wait_any+) so the spec doesn't need a live VM — | ||
| # this is intentionally an SDK-shape check, not an end-to-end | ||
| # integration. The +test-services/+ suite covers the live-VM path. | ||
| RSpec.describe Restate::DurableFuture do | ||
| # Minimal fake context that just records calls. Real Server::Context | ||
| # is too heavy for this spec — we only need +resolve_handle+ and | ||
| # +completed?+ to respond. | ||
| let(:ctx) { double('ctx', resolve_handle: 'ignored', completed?: false) } | ||
|
|
||
| describe '#or_timeout' do | ||
| context 'when the future completes before the sleep' do | ||
| it "returns the future's value" do | ||
| future = described_class.new(ctx, :handle_a) | ||
| sleep_future = described_class.new(ctx, :handle_sleep) | ||
|
|
||
| allow(Restate).to receive(:sleep).with(5).and_return(sleep_future) | ||
| allow(Restate).to receive(:wait_any) do | ||
| # Race outcome: the work future wins. | ||
| allow(future).to receive(:completed?).and_return(true) | ||
| allow(future).to receive(:await).and_return('done') | ||
| nil | ||
| end | ||
|
|
||
| expect(future.or_timeout(5)).to eq('done') | ||
| end | ||
| end | ||
|
|
||
| context 'when the sleep wins the race' do | ||
| it 'raises Restate::TimeoutError' do | ||
| future = described_class.new(ctx, :handle_a) | ||
| sleep_future = described_class.new(ctx, :handle_sleep) | ||
|
|
||
| allow(Restate).to receive(:sleep).with(5).and_return(sleep_future) | ||
| allow(Restate).to receive(:wait_any) do | ||
| # Race outcome: the sleep wins; the work future stays incomplete. | ||
| allow(future).to receive(:completed?).and_return(false) | ||
| nil | ||
| end | ||
|
|
||
| expect { future.or_timeout(5) }.to raise_error(Restate::TimeoutError) | ||
| end | ||
|
|
||
| it 'attaches HTTP status 408 (Request Timeout)' do | ||
| future = described_class.new(ctx, :handle_a) | ||
| allow(Restate).to receive(:sleep).and_return(described_class.new(ctx, :sleep)) | ||
| allow(Restate).to receive(:wait_any) do | ||
| allow(future).to receive(:completed?).and_return(false) | ||
| nil | ||
| end | ||
|
|
||
| future.or_timeout(5) | ||
| rescue Restate::TimeoutError => e | ||
| expect(e.status_code).to eq(408) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| RSpec.describe Restate::DurableCallFuture do | ||
| let(:ctx) do | ||
| double('ctx', resolve_handle: 'inv_xyz', completed?: false, cancel_invocation: nil) | ||
| end | ||
|
|
||
| def build_call_future | ||
| described_class.new(ctx, :result_handle, :invocation_id_handle, output_serde: nil) | ||
| end | ||
|
|
||
| # Matches TS/Java SDKs: timeout never auto-cancels the underlying call. | ||
| # Users who want that behavior rescue +TimeoutError+ and call +#cancel+. | ||
| describe '#or_timeout' do | ||
| context 'when the call completes before the sleep' do | ||
| it "returns the call's value and does not cancel" do | ||
| future = build_call_future | ||
| sleep_future = Restate::DurableFuture.new(ctx, :handle_sleep) | ||
|
|
||
| allow(Restate).to receive(:sleep).with(5).and_return(sleep_future) | ||
| allow(Restate).to receive(:wait_any) do | ||
| allow(future).to receive(:completed?).and_return(true) | ||
| allow(future).to receive(:await).and_return({ 'ok' => true }) | ||
| nil | ||
| end | ||
|
|
||
| expect(future.or_timeout(5)).to eq({ 'ok' => true }) | ||
| expect(ctx).not_to have_received(:cancel_invocation) | ||
| end | ||
| end | ||
|
|
||
| context 'when the sleep wins the race' do | ||
| it 'raises TimeoutError without cancelling the remote invocation' do | ||
| future = build_call_future | ||
| sleep_future = Restate::DurableFuture.new(ctx, :handle_sleep) | ||
|
|
||
| allow(Restate).to receive(:sleep).with(5).and_return(sleep_future) | ||
| allow(Restate).to receive(:wait_any) do | ||
| allow(future).to receive(:completed?).and_return(false) | ||
| nil | ||
| end | ||
|
|
||
| expect { future.or_timeout(5) }.to raise_error(Restate::TimeoutError) | ||
| expect(ctx).not_to have_received(:cancel_invocation) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| RSpec.describe Restate::TimeoutError do | ||
| it 'inherits from TerminalError so user rescue blocks catch it uniformly' do | ||
| expect(described_class.ancestors).to include(Restate::TerminalError) | ||
| end | ||
|
|
||
| it 'defaults to HTTP status 408 (Request Timeout) matching the TS SDK' do | ||
| expect(described_class.new.status_code).to eq(408) | ||
| end | ||
|
|
||
| it 'has a meaningful default message' do | ||
| expect(described_class.new.message).to eq('Timeout occurred') | ||
| end | ||
| end |
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.
Uh oh!
There was an error while loading. Please reload this page.