-
Notifications
You must be signed in to change notification settings - Fork 2
Async and error handling features #24
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
jwoertink
merged 3 commits into
luckyframework:main
from
rmarronnier:claude-suggestions
Jun 23, 2025
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,4 +11,4 @@ license: MIT | |
| development_dependencies: | ||
| ameba: | ||
| github: crystal-ameba/ameba | ||
| version: ~> 1.0.0 | ||
| version: ~> 1.6.0 | ||
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,110 @@ | ||
| require "./spec_helper" | ||
|
|
||
| class Pulsar::AsyncTestEvent < Pulsar::Event | ||
| end | ||
|
|
||
| class Pulsar::AsyncTestTimedEvent < Pulsar::TimedEvent | ||
| end | ||
|
|
||
| describe "Pulsar async subscribers" do | ||
| after_each do | ||
| Pulsar::AsyncTestEvent.clear_subscribers | ||
| Pulsar::AsyncTestTimedEvent.clear_subscribers | ||
| end | ||
|
|
||
| describe "Event.subscribe_async" do | ||
| it "runs subscribers asynchronously" do | ||
| channel = Channel(Int32).new | ||
|
|
||
| Pulsar::AsyncTestEvent.subscribe_async do |_event| | ||
| sleep 10.milliseconds | ||
| channel.send(1) | ||
| end | ||
|
|
||
| Pulsar::AsyncTestEvent.subscribe_async do |_event| | ||
| sleep 10.milliseconds | ||
| channel.send(2) | ||
| end | ||
|
|
||
| start_time = Time.monotonic | ||
| Pulsar::AsyncTestEvent.publish | ||
| publish_time = Time.monotonic - start_time | ||
|
|
||
| # Publish should return immediately without waiting | ||
| publish_time.should be < 0.005.seconds | ||
|
|
||
| # Both async subscribers should complete | ||
| results = [channel.receive, channel.receive].sort | ||
| results.should eq([1, 2]) | ||
| end | ||
|
|
||
| it "doesn't block synchronous subscribers" do | ||
| sync_called = false | ||
| async_channel = Channel(Nil).new | ||
|
|
||
| Pulsar::AsyncTestEvent.subscribe do |_event| | ||
| sync_called = true | ||
| end | ||
|
|
||
| Pulsar::AsyncTestEvent.subscribe_async do |_event| | ||
| sleep 10.milliseconds | ||
| async_channel.send(nil) | ||
| end | ||
|
|
||
| Pulsar::AsyncTestEvent.publish | ||
|
|
||
| # Sync subscriber should have been called immediately | ||
| sync_called.should be_true | ||
|
|
||
| # Async subscriber should complete later | ||
| async_channel.receive | ||
| end | ||
| end | ||
|
|
||
| describe "TimedEvent.subscribe_async" do | ||
| it "runs timed subscribers asynchronously" do | ||
| channel = Channel(Float64).new | ||
|
|
||
| Pulsar::AsyncTestTimedEvent.subscribe_async do |_event, duration| | ||
| sleep 10.milliseconds | ||
| channel.send(duration.total_milliseconds) | ||
| end | ||
|
|
||
| start_time = Time.monotonic | ||
| Pulsar::AsyncTestTimedEvent.publish do | ||
| sleep 2.milliseconds | ||
| end | ||
| publish_time = Time.monotonic - start_time | ||
|
|
||
| # Should only wait for the block inside publish, not the async subscriber | ||
| publish_time.should be < 0.01.seconds | ||
|
|
||
| # Async subscriber should receive the duration | ||
| duration_ms = channel.receive | ||
| duration_ms.should be > 2.0 | ||
| end | ||
| end | ||
|
|
||
| describe "error handling with async subscribers" do | ||
| it "handles errors in async subscribers based on strategy" do | ||
| Pulsar::ErrorHandler.strategy = Pulsar::ErrorHandler::Strategy::Custom | ||
|
|
||
| errors_channel = Channel(String).new | ||
|
|
||
| Pulsar::ErrorHandler.custom_handler = ->(exception : Exception, event : Pulsar::BaseEvent) { | ||
| errors_channel.send("#{event.name}: #{exception.message}") | ||
| nil | ||
| } | ||
|
|
||
| Pulsar::AsyncTestEvent.subscribe_async do |_event| | ||
| raise "Async error" | ||
| end | ||
|
|
||
| Pulsar::AsyncTestEvent.publish | ||
|
|
||
| # Error should be handled in the spawned fiber | ||
| error_msg = errors_channel.receive | ||
| error_msg.should eq("Pulsar::AsyncTestEvent: Async error") | ||
| end | ||
| end | ||
| end |
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 @@ | ||
| require "./spec_helper" | ||
|
|
||
| class Pulsar::ErrorTestEvent < Pulsar::Event | ||
| end | ||
|
|
||
| class Pulsar::ErrorTestTimedEvent < Pulsar::TimedEvent | ||
| end | ||
|
|
||
| describe "Pulsar error handling" do | ||
| after_each do | ||
| Pulsar::ErrorHandler.strategy = Pulsar::ErrorHandler::Strategy::Log | ||
| Pulsar::ErrorTestEvent.clear_subscribers | ||
| Pulsar::ErrorTestTimedEvent.clear_subscribers | ||
| end | ||
|
|
||
| describe "with ignore strategy" do | ||
| it "ignores errors and continues to next subscriber" do | ||
| Pulsar::ErrorHandler.strategy = Pulsar::ErrorHandler::Strategy::Ignore | ||
|
|
||
| calls = [] of Int32 | ||
|
|
||
| Pulsar::ErrorTestEvent.subscribe { calls << 1 } | ||
| Pulsar::ErrorTestEvent.subscribe { raise "Test error" } | ||
| Pulsar::ErrorTestEvent.subscribe { calls << 3 } | ||
|
|
||
| Pulsar::ErrorTestEvent.publish | ||
|
|
||
| calls.should eq([1, 3]) | ||
| end | ||
| end | ||
|
|
||
| describe "with raise strategy" do | ||
| it "raises the error and stops processing" do | ||
| Pulsar::ErrorHandler.strategy = Pulsar::ErrorHandler::Strategy::Raise | ||
|
|
||
| calls = [] of Int32 | ||
|
|
||
| Pulsar::ErrorTestEvent.subscribe { calls << 1 } | ||
| Pulsar::ErrorTestEvent.subscribe { raise "Test error" } | ||
| Pulsar::ErrorTestEvent.subscribe { calls << 3 } | ||
|
|
||
| expect_raises(Exception, "Test error") do | ||
| Pulsar::ErrorTestEvent.publish | ||
| end | ||
|
|
||
| calls.should eq([1]) | ||
| end | ||
| end | ||
|
|
||
| describe "with custom strategy" do | ||
| it "calls the custom handler" do | ||
| Pulsar::ErrorHandler.strategy = Pulsar::ErrorHandler::Strategy::Custom | ||
|
|
||
| handled_errors = [] of String | ||
|
|
||
| Pulsar::ErrorHandler.custom_handler = ->(exception : Exception, event : Pulsar::BaseEvent) { | ||
| handled_errors << "#{event.name}: #{exception.message}" | ||
| nil | ||
| } | ||
|
|
||
| Pulsar::ErrorTestEvent.subscribe { raise "Custom error" } | ||
| Pulsar::ErrorTestEvent.publish | ||
|
|
||
| handled_errors.should eq(["Pulsar::ErrorTestEvent: Custom error"]) | ||
| end | ||
| end | ||
|
|
||
| describe "with timed events" do | ||
| it "handles errors in timed event subscribers" do | ||
| Pulsar::ErrorHandler.strategy = Pulsar::ErrorHandler::Strategy::Ignore | ||
|
|
||
| calls = [] of Int32 | ||
|
|
||
| Pulsar::ErrorTestTimedEvent.subscribe { |_, _| calls << 1 } | ||
| Pulsar::ErrorTestTimedEvent.subscribe { |_, _| raise "Timed error" } | ||
| Pulsar::ErrorTestTimedEvent.subscribe { |_, _| calls << 3 } | ||
|
|
||
| result = Pulsar::ErrorTestTimedEvent.publish { :success } | ||
|
|
||
| calls.should eq([1, 3]) | ||
| result.should eq(:success) | ||
| end | ||
| end | ||
| end |
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| require "./pulsar/*" | ||
| require "log" | ||
|
|
||
| module Pulsar | ||
| VERSION = "0.2.3" | ||
|
|
||
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,32 @@ | ||
| module Pulsar | ||
| # Configuration for handling errors in event subscribers | ||
| class ErrorHandler | ||
| enum Strategy | ||
| # Ignore the error and continue to next subscriber | ||
| Ignore | ||
| # Log the error and continue to next subscriber (default) | ||
| Log | ||
| # Stop processing and raise the error | ||
| Raise | ||
| # Call a custom handler | ||
| Custom | ||
| end | ||
|
|
||
| class_property strategy : Strategy = Strategy::Log | ||
| class_property custom_handler : (Exception, Pulsar::BaseEvent) -> Nil = ->(exception : Exception, event : Pulsar::BaseEvent) { } | ||
|
|
||
| # Handle an error from a subscriber | ||
| def self.handle(exception : Exception, event : Pulsar::BaseEvent) | ||
| case strategy | ||
| when .ignore? | ||
| # Do nothing | ||
| when .log? | ||
| Log.error(exception: exception) { "Error in subscriber for #{event.name}" } | ||
| when .raise? | ||
| raise exception | ||
| when .custom? | ||
| custom_handler.call(exception, event) | ||
| end | ||
| end | ||
| end | ||
| end |
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
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.
oops! 😂