-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
72 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module NetworkResiliency | ||
class Syncer < Thread | ||
class << self | ||
def start(redis) | ||
@instance&.shutdown | ||
@instance = new(redis) | ||
end | ||
|
||
def stop | ||
@instance&.shutdown | ||
@instance = nil | ||
end | ||
end | ||
|
||
def initialize(redis) | ||
@redis = redis | ||
|
||
super { sync } | ||
end | ||
|
||
def shutdown | ||
@shutdown = true | ||
|
||
# prevent needless delay | ||
raise Interrupt if status == "sleep" | ||
end | ||
|
||
private | ||
|
||
def sync | ||
until @shutdown | ||
StatsEngine.sync(@redis) | ||
|
||
sleep(3) | ||
end | ||
rescue Interrupt | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
describe NetworkResiliency::Syncer do | ||
before do | ||
# mocking not supported in Threads | ||
NetworkResiliency.statsd = nil | ||
|
||
# unstub from spec_helper | ||
allow(NetworkResiliency::Syncer).to receive(:start).and_call_original | ||
end | ||
|
||
let(:redis) { Redis.new } | ||
|
||
def start | ||
described_class.start(redis) | ||
end | ||
|
||
describe ".start" do | ||
it "returns a Thread" do | ||
expect(start).to be_a(Thread) | ||
end | ||
|
||
it "can be called many times without error" do | ||
3.times { start } | ||
end | ||
|
||
it "will stop previous workers so only one is running at a time" do | ||
first_worker = start | ||
second_worker = start | ||
|
||
first_worker.join | ||
|
||
expect(first_worker).not_to be_alive | ||
expect(second_worker).to be_alive | ||
end | ||
end | ||
|
||
describe ".stop" do | ||
it "stops syncing" do | ||
worker = start | ||
expect(worker).to be_alive | ||
|
||
described_class.stop | ||
|
||
worker.join | ||
expect(worker).not_to be_alive | ||
end | ||
end | ||
end |