-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't start event source until Rails is ready. (#117)
* Don't start event source until rails is ready. * Fix up when HTTP subscriber bindings are resolved. * Properly name parameter. * Fix http subscriber routing. * Add more documentation.
- Loading branch information
Showing
29 changed files
with
331 additions
and
97 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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--format documentation | ||
--color | ||
--require spec_helper | ||
--exclude-pattern "spec/rails_app/**/*" |
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,3 @@ | ||
--format documentation | ||
--color | ||
--exclude-pattern "**/*" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'set' | ||
require 'monitor' | ||
|
||
module EventSource | ||
# This class manages correct/loading of subscribers and publishers | ||
# based on the current stage of the EventSource lifecycle. | ||
# | ||
# Depending on both the time the initialization of EventSource is invoked | ||
# and when subscriber/publisher code is loaded, this can become complicated. | ||
# This is largely caused by two confounding factors: | ||
# 1. We want to delay initialization of EventSource until Rails is fully | ||
# 'ready' | ||
# 2. Based on the Rails environment, such as production, development, or | ||
# test (primarily how those different environments treat lazy vs. eager | ||
# loading of classes in a Rails application), subscriber and publisher | ||
# code can be loaded before, after, or sometimes even DURING the | ||
# EventSource boot process - we need to support all models | ||
class BootRegistry | ||
def initialize | ||
@unbooted_publishers = Set.new | ||
@unbooted_subscribers = Set.new | ||
@booted_publishers = Set.new | ||
@booted_subscribers = Set.new | ||
# This is our re-entrant mutex. We're going to use it to make sure that | ||
# registration and boot methods aren't allowed to simultaneously alter | ||
# our state. You'll notice most methods on this class are wrapped in | ||
# synchronize calls against this. | ||
@bootex = Monitor.new | ||
@booted = false | ||
end | ||
|
||
def boot!(force = false) | ||
@bootex.synchronize do | ||
return if @booted && !force | ||
yield | ||
boot_publishers! | ||
boot_subscribers! | ||
@booted = true | ||
end | ||
end | ||
|
||
# Register a publisher for EventSource. | ||
# | ||
# If the EventSource hasn't been booted, save publisher for later. | ||
# Otherwise, boot it now. | ||
def register_publisher(publisher_klass) | ||
@bootex.synchronize do | ||
if @booted | ||
publisher_klass.validate | ||
@booted_publishers << publisher_klass | ||
else | ||
@unbooted_publishers << publisher_klass | ||
end | ||
end | ||
end | ||
|
||
# Register a subscriber for EventSource. | ||
# | ||
# If the EventSource hasn't been booted, save the subscriber for later. | ||
# Otherwise, boot it now. | ||
def register_subscriber(subscriber_klass) | ||
@bootex.synchronize do | ||
if @booted | ||
subscriber_klass.create_subscription | ||
@booted_subscribers << subscriber_klass | ||
else | ||
@unbooted_subscribers << subscriber_klass | ||
end | ||
end | ||
end | ||
|
||
# Boot the publishers. | ||
def boot_publishers! | ||
@bootex.synchronize do | ||
@unbooted_publishers.each do |pk| | ||
pk.validate | ||
@booted_publishers << pk | ||
end | ||
@unbooted_publishers = Set.new | ||
end | ||
end | ||
|
||
# Boot the subscribers. | ||
def boot_subscribers! | ||
@bootex.synchronize do | ||
@unbooted_subscribers.each do |sk| | ||
sk.create_subscription | ||
@booted_subscribers << sk | ||
end | ||
@unbooted_subscribers = Set.new | ||
end | ||
end | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require "spec_helper" | ||
require "parallel_tests" | ||
require "parallel_tests/rspec/runner" | ||
|
||
RSpec.describe EventSource, "rails specs" do | ||
it "runs the rails tests in the rails application context" do | ||
ParallelTests.with_pid_file do | ||
specs_run_result = ParallelTests::RSpec::Runner.run_tests( | ||
[ | ||
"spec/rails_app/spec/railtie_spec.rb", | ||
"spec/rails_app/spec/http_service_integration_spec.rb" | ||
], | ||
1, | ||
1, | ||
{ | ||
serialize_stdout: true, | ||
test_options: ["-O", ".rspec_rails_specs", "--format", "documentation"] | ||
} | ||
) | ||
if specs_run_result[:exit_status] != 0 | ||
fail(specs_run_result[:stdout] + "\n\n") | ||
end | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
spec/rails_app/app/event_source/events/determinations/eval.rb
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module Events | ||
module Determinations | ||
# Eval will register event publisher for MiTC | ||
class Eval < EventSource::Event | ||
publisher_path 'publishers.mitc_publisher' | ||
|
||
end | ||
end | ||
end |
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Publishers | ||
class MitcPublisher | ||
# Publisher will send request payload to MiTC for determinations | ||
include ::EventSource::Publisher[http: '/determinations/eval'] | ||
register_event '/determinations/eval' | ||
end | ||
end |
12 changes: 0 additions & 12 deletions
12
spec/rails_app/app/event_source/publishers/parties/mitc_publisher.rb
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
spec/rails_app/app/event_source/subscribers/mitc_response_subscriber.rb
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,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module Subscribers | ||
class MitcResponseSubscriber | ||
include ::EventSource::Subscriber[http: '/determinations/eval'] | ||
extend EventSource::Logging | ||
|
||
subscribe(:on_determinations_eval) do |body, status, headers| | ||
$GLOBAL_TEST_FLAG = true | ||
end | ||
end | ||
end |
14 changes: 0 additions & 14 deletions
14
spec/rails_app/app/event_source/subscribers/parties/mitc_subscriber.rb
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.