-
Notifications
You must be signed in to change notification settings - Fork 169
improve informer test #594
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
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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ def initialize(client, resource_name, reconcile_timeout: 15 * 60, logger: nil) | |
| @logger = logger | ||
| @cache = nil | ||
| @started = nil | ||
| @stopped = false | ||
| @watching = [] | ||
| end | ||
|
|
||
|
|
@@ -21,22 +22,34 @@ def watch(&block) | |
|
|
||
| # not implicit so users know they have to `stop` | ||
| def start_worker | ||
| @stopped = false | ||
| @worker = Thread.new do | ||
| loop do | ||
| fill_cache | ||
| watch_to_update_cache | ||
| rescue StandardError => e | ||
| # need to keep retrying since we work in the background | ||
| @logger&.error("ignoring error during background work #{e}") | ||
| ensure | ||
| sleep(1) # do not overwhelm the api-server if we are somehow broken | ||
| begin | ||
| fill_cache | ||
| watch_to_update_cache | ||
| rescue StandardError => e | ||
| # need to keep retrying since we work in the background | ||
| @logger&.error("ignoring error during background work #{e}") | ||
| ensure | ||
| sleep(1) # do not overwhelm the api-server if we are somehow broken | ||
| end | ||
| break if @stopped | ||
| end | ||
| end | ||
| sleep(0.01) until @cache | ||
| sleep(0.01) until @cache || @stopped | ||
| end | ||
|
|
||
| def stop_worker | ||
| @worker&.kill # TODO: be nicer ? | ||
| @stopped = true | ||
| [@waiter, @worker].compact.each do |thread| | ||
| begin | ||
| thread.run # cancel sleep so either the loop sleep or the timeout sleep are interrupted | ||
| rescue ThreadError | ||
| # thread was already dead | ||
| end | ||
| thread.join | ||
|
Collaborator
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. Similar question about
Member
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.
You can use
Contributor
Author
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. before we killed them, but that made the tests brittle, so I like the joining since that makes anything that goes wrong more obvious (and re-raises exceptions too)
Contributor
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. From my testing In that case |
||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
@@ -69,7 +82,7 @@ def watch_to_update_cache | |
| stop_reason = 'disconnect' | ||
|
|
||
| # stop watcher without using timeout | ||
| Thread.new do | ||
| @waiter = Thread.new do | ||
| sleep(@reconcile_timeout) | ||
| stop_reason = 'reconcile' | ||
| @watcher.finish | ||
|
|
@@ -88,6 +101,14 @@ def watch_to_update_cache | |
| @watching.each { |q| q << notice } | ||
| end | ||
| @logger&.info("watch restarted: #{stop_reason}") | ||
|
|
||
| # wake the waiter unless it's dead so it does not hang around | ||
| begin | ||
| Thread.pass # make sure we get into the sleep state of the waiter | ||
| @waiter.run | ||
| rescue ThreadError # rubocop:disable Lint/SuppressedException | ||
| end | ||
| @waiter.join | ||
|
Collaborator
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. Oh I think I see — previously, leftover waiter thread(s) could execute But if we somehow (e.g. 'ERROR') come here early during
Contributor
Author
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 would have killed a random new waiter before (not really in tests since they all build their own informer) added a |
||
| end | ||
| end | ||
| end | ||
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.
why not change the
looptountil @stoppedthen?