-
-
Notifications
You must be signed in to change notification settings - Fork 187
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
Log reconnects #244
Merged
Merged
Log reconnects #244
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
1346ef1
Initialize Hello with a `reconnected` variable
wasabigeek 58edabe
test: Add spec for logging the connection
wasabigeek 9b8e0cd
test: Add context with no client
wasabigeek 0f8b688
test: Add spec for client w/o team
wasabigeek 663417c
feat: Log a reconnection if hello hook is received again
wasabigeek 19976e5
refactor: Remove unnecessary spec
wasabigeek caa77e8
refactor: Remove subject
wasabigeek 6fa3ddd
doc: Add changelog for #244
wasabigeek 0d9a2dd
refactor: Record the connected_at time instead of boolean
wasabigeek f841611
refactor: record first connection time only
wasabigeek 13cb8f5
feat: Log time since last connection naively
wasabigeek 89b5ca3
feat: Use monotonic clock
wasabigeek 573e5fe
fix: failing specs
wasabigeek 01962da
test: Attempt to fix mocks
wasabigeek 85d3db6
test: Another attempt to fix mocks
wasabigeek 5db8e70
Reduce number of logs created
wasabigeek 3b18d4c
Shorten the log message
wasabigeek 7b556e2
test: Let spec handle very small floats
wasabigeek 38cfca8
feat: Shorten and round seconds in log
wasabigeek cdde173
refactor: Use compact
wasabigeek a92bacf
style: Fix rubocop warnings
wasabigeek 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 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,15 +1,22 @@ | ||
module SlackRubyBot | ||
module Hooks | ||
class Hello | ||
attr_accessor :logger | ||
attr_accessor :logger, :connected_at | ||
|
||
def initialize(logger) | ||
self.logger = logger | ||
end | ||
|
||
def call(client, _data) | ||
return unless client && client.team | ||
logger.info "Successfully connected team #{client.team.name} (#{client.team.id}) to https://#{client.team.domain}.slack.com." | ||
log = ["Successfully #{@connected_at ? 'reconnected' : 'connected'} team #{client.team.name} (#{client.team.id}) to https://#{client.team.domain}.slack.com"] | ||
|
||
connected_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
log << "after #{connected_at - self.connected_at} seconds" if self.connected_at | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would round it up to 1 or 2 decimals and just stick an |
||
|
||
logger.info "#{log.join(' ')}." | ||
|
||
self.connected_at = connected_at | ||
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,49 @@ | ||
describe SlackRubyBot::Hooks::Hello do | ||
let(:logger) { double(:logger, info: nil) } | ||
let(:hello_hook) { described_class.new(logger) } | ||
|
||
describe '#call' do | ||
let(:team_name) { 'Example Team' } | ||
let(:team_id) { SecureRandom.uuid } | ||
let(:team_domain) { 'example' } | ||
let(:team) { double(:team, name: team_name, id: team_id, domain: team_domain) } | ||
let(:client) { instance_double(SlackRubyBot::Client, team: team) } | ||
|
||
def receive_hello | ||
hello_hook.call(client, double) | ||
end | ||
|
||
it 'logs the connection' do | ||
expect(logger).to receive(:info).with("Successfully connected team #{team_name} (#{team_id}) to https://#{team_domain}.slack.com.") | ||
receive_hello | ||
end | ||
|
||
context 'with no client' do | ||
let(:client) { nil } | ||
it 'does nothing' do | ||
expect(logger).not_to receive(:info) | ||
receive_hello | ||
end | ||
end | ||
|
||
context 'when client has no team' do | ||
let(:team) { nil } | ||
it 'does nothing' do | ||
expect(logger).not_to receive(:info) | ||
receive_hello | ||
end | ||
end | ||
|
||
context 'when hook is received multiple times' do | ||
before do | ||
receive_hello | ||
end | ||
|
||
it 'logs the reconnections' do | ||
expect(logger).to receive(:info).with(/^Successfully reconnected .+ after \S+ seconds\.$/).twice | ||
receive_hello | ||
receive_hello | ||
end | ||
end | ||
end | ||
end |
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.
Here's a neat trick I use for this:
Maybe nicer?