Skip to content
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

Only return the cached value if it exists. #247

Merged
merged 1 commit into from
Jul 21, 2017

Conversation

jrafanie
Copy link
Member

If the cache was cleared and an exception was raised when trying to set
the new cache[:value], we'd end up setting the cache[:timeout] but
not the new value. Any subsequent call would return a nil from
cache[:value] because cache[:timeout] exists, making cache non-empty.

The new code only returns from the cache if the :value key exists in the
cache.

Additionally, for consistency, we only set the cache[:timeout]
and cache[:value] after we've calculated these values, therefore the
cache doesn't get partially set. This is not required but makes me
feel better.

https://bugzilla.redhat.com/show_bug.cgi?id=1469307
https://bugzilla.redhat.com/show_bug.cgi?id=1468898

This replaces #244

@@ -41,6 +41,30 @@
expect(test_module.default).to eq(value)
end

it 'clears the cache even if the block raises an exception' do
$call_count = 0
Copy link
Member

@Fryguy Fryguy Jul 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can use an instance variable in the test class?

def test_class.call_count  # or use an instance_variable_set or whatever
  @call_count ||= 0
end

test_class.cache_with_timeout...

@Fryguy
Copy link
Member

Fryguy commented Jul 19, 2017

In the other PR you mentioned a test for 5, raise, raise, etc. Do you think you still need that?

@Fryguy
Copy link
Member

Fryguy commented Jul 19, 2017

👍 on the code...just need to clean up the specs.

end
end

test_class.flapping_method(true) # initializes cache to 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an expectation here as well that it's 0

expect(test_class.flapping_method).to eq(0) # returns cached 0

test_class.flapping_method(true) rescue nil # blows up, clears cache for next request
expect(test_class.flapping_method).to eq(2) # sets new value since cache is cleared
Copy link
Member

@Fryguy Fryguy Jul 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to throw another call after this one to show that it's cached since technically this line is a fresh invocation.

expect(test_class.flapping_method).to eq(2) # sets new value since cache is cleared

test_class.flapping_method(true) rescue nil # blows up, clears cache for next request
expect(test_class.flapping_method).to eq(4) # sets new value since cache is cleared
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here...you might want a second call to verify the caching.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha, I contemplated these too...

If the cache was cleared and an exception was raised when trying to set
the new `cache[:value]`, we'd end up setting the `cache[:timeout]` but
not the new value.  Any subsequent call would return a nil from
`cache[:value]` because `cache[:timeout]` exists, making `cache` non-empty.

The new code only returns from the cache if the :value key exists in the
`cache`.

Additionally, for consistency, we only set the `cache[:timeout]`
and `cache[:value]` after we've calculated these values, therefore the
`cache` doesn't get partially set.  This is not required but makes me
feel better.

https://bugzilla.redhat.com/show_bug.cgi?id=1469307
https://bugzilla.redhat.com/show_bug.cgi?id=1468898

This replaces ManageIQ#244
@jrafanie jrafanie force-pushed the cache_invalidation_is_harder branch from f8bb265 to 59aab09 Compare July 19, 2017 17:47
@miq-bot
Copy link
Member

miq-bot commented Jul 19, 2017

Checked commit jrafanie@59aab09 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0
2 files checked, 3 offenses detected

lib/gems/pending/util/extensions/miq-module.rb

spec/util/extensions/miq-module_spec.rb

expect(test_class.flapping_method).to eq(0) # returns from cache

test_class.flapping_method(true) rescue nil # blows up, clears cache for next request
expect(test_class.flapping_method).to eq(2) # sets new value since cache is cleared
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the other PR you mentioned a test for 5, raise, raise, etc. Do you think you still need that?

@Fryguy No. This expectation shows that we are actually setting a new value and returning it, NOT the previously cached value, 0. So, I think this test encompasses the desired test cases.

@Fryguy Fryguy merged commit 6bdd644 into ManageIQ:master Jul 21, 2017
@Fryguy Fryguy added this to the Sprint 65 Ending Jul 24, 2017 milestone Jul 21, 2017
simaishi pushed a commit that referenced this pull request Jul 21, 2017
@simaishi
Copy link
Contributor

Fine backport details:

$ git log -1
commit 8f7d63f441d54ff41247c7605e2bf5986133f9f1
Author: Jason Frey <[email protected]>
Date:   Fri Jul 21 12:06:41 2017 -0400

    Merge pull request #247 from jrafanie/cache_invalidation_is_harder
    
    Only return the cached value if it exists.
    (cherry picked from commit 6bdd64449485857526c968a119d55a7edba6e015)
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1473787

@simaishi
Copy link
Contributor

@jrafanie The BZs referenced here is for Embedded Ansible. Since that's not available in Euwe branch, I assume this bug will show up in some different way... Can you create a BZ that describes the problem in Euwe?

@jrafanie
Copy link
Member Author

jrafanie commented Jul 26, 2017

@simaishi Thanks! I'll remove the euwe label. While it's possible for this bug on euwe, it's very unlikely without the embedded ansible thread worker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants