-
Notifications
You must be signed in to change notification settings - Fork 90
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
Adds cached_client method in BaseConnection #371
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,54 @@ | |
.method(:logger).call.must_equal(l) | ||
end | ||
|
||
describe 'cached_client helper' do | ||
class DemoConnection < Train::Plugins::Transport::BaseConnection | ||
def initialize(options = {}) | ||
super(options) | ||
@cache_enabled[:api_call] = true | ||
@cache[:api_call] = {} | ||
end | ||
|
||
def demo_client | ||
cached_client(:api_call, :demo_client) do | ||
DemoClient.new | ||
end | ||
end | ||
|
||
class DemoClient | ||
end | ||
end | ||
|
||
it 'returns a new connection when cached disabled' do | ||
conn = DemoConnection.new | ||
conn.disable_cache(:api_call) | ||
|
||
client1 = conn.demo_client | ||
client2 = conn.demo_client | ||
|
||
client1.wont_be_same_as client2 | ||
end | ||
|
||
it 'returns a new connection when cache enabled and not hydrated' do | ||
conn = DemoConnection.new | ||
conn.enable_cache(:api_call) | ||
|
||
client1 = conn.demo_client | ||
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. It wouldn't be useful in the other tests, because they don't look anything like real world code, but this one made me wonder if we could just return def enable_cache(type)
fail Train::UnknownCacheType, "#{type} is not a valid cache type" unless @cache_enabled.keys.include?(type.to_sym)
@cache_enabled[type.to_sym] = true
self
end it 'returns a new connection when cache enabled and not hydrated' do
conn = DemoConnection.new.enable_cache(:api_call)

client1 = conn.demo_client
client1.must_be_instance_of DemoConnection::DemoClient
end
# or even...
it 'returns a new connection when cache enabled and not hydrated' do
client1 = DemoConnection.new.enable_cache(:api_call).demo_client

client1.must_be_instance_of DemoConnection::DemoClient
end |
||
|
||
client1.must_be_instance_of DemoConnection::DemoClient | ||
end | ||
|
||
it 'returns a cached connection when cache enabled and hydrated' do | ||
conn = DemoConnection.new | ||
conn.enable_cache(:api_call) | ||
|
||
client1 = conn.demo_client | ||
client2 = conn.demo_client | ||
|
||
client1.must_be_same_as client2 | ||
end | ||
end | ||
|
||
describe 'create cache connection' do | ||
it 'default connection cache settings' do | ||
connection.cache_enabled?(:file).must_equal true | ||
|
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.
@cache.dig(type, key) ||= yield
?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.
I wasn't sure if you could do assignment when using dig.
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.
That doesn't work. You aren't allowed to do assignment from the return of
dig
.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.
Yeah, you're right. I wasn't thinking about the assignment. That's a shame--it should work that way!