Skip to content

Commit

Permalink
Add unit test for ScheduledExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
rarruda committed Jan 17, 2019
1 parent a7b4fb8 commit 05e9fcd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -168,6 +168,16 @@ if UNLEASH.is_enabled? "AwesomeFeature", @unleash_context, true
end
```

#### Client methods

Method Name | Description | Return Type |
---------|-------------|-------------|
`is_enabled?` | Check if feature toggle is to be enabled or not. | Boolean |
`enabled?` | Alias to the `is_enabled?` method. But more ruby idiomatic. | Boolean |
`shutdown` | Save metrics to disk, flush metrics to server, and then kill ToggleFetcher and MetricsReporter threads. A safe shutdown. Not really useful in long running applications, like web applications. | nil |
`shutdown!` | Kill ToggleFetcher and MetricsReporter threads immediately. | nil |


## Local test client

```
Expand Down
6 changes: 5 additions & 1 deletion lib/unleash/scheduled_executor.rb
Expand Up @@ -38,8 +38,12 @@ def run(&blk)
end
end

def running?
self.thread.is_a?(Thread) && self.thread.alive?
end

def exit
if self.thread.is_a?(Thread) && self.thread.alive?
if self.running?
Unleash.logger.warn "thread #{name} will exit!"
self.thread.exit
end
Expand Down
7 changes: 4 additions & 3 deletions lib/unleash/toggle_fetcher.rb
Expand Up @@ -35,7 +35,7 @@ def toggles
end

# rename to refresh_from_server! ??
# TODO: should simplify by moving uri / http initialization to elsewhere
# TODO: should simplify by moving uri / http initialization elsewhere
def fetch
Unleash.logger.debug "fetch()"
Unleash.logger.debug "ETag: #{self.etag}" unless self.etag.nil?
Expand Down Expand Up @@ -83,19 +83,20 @@ def save!
begin
backup_file = Unleash.configuration.backup_file
backup_file_tmp = "#{backup_file}.tmp"
file = File.open(backup_file_tmp, "w")

self.toggle_lock.synchronize do
file = File.open(backup_file_tmp, "w")
file.write(self.toggle_cache.to_json)
File.rename(backup_file_tmp, backup_file)
end

File.rename(backup_file_tmp, backup_file)
rescue Exception => e
# This is not really the end of the world. Swallowing the exception.
Unleash.logger.error "Unable to save backup file. Exception thrown #{e.class}:'#{e}'"
Unleash.logger.error "stacktrace: #{e.backtrace}"
ensure
file.close unless file.nil?
self.toggle_lock.unlock if self.toggle_lock.locked?
end
end

Expand Down
18 changes: 18 additions & 0 deletions spec/unleash/scheduled_executor_spec.rb
@@ -0,0 +1,18 @@
require 'spec_helper'

RSpec.describe Unleash::ScheduledExecutor do
# context 'parameters correctly assigned in initialization'
it "can start and exit a thread" do
scheduled_executor = Unleash::ScheduledExecutor.new('TesterLoop', 0.1)
scheduled_executor.run do
loop do
sleep 0.1
end
end

expect(scheduled_executor.running?).to be true
scheduled_executor.exit
sleep 0.1
expect(scheduled_executor.running?).to be false
end
end

0 comments on commit 05e9fcd

Please sign in to comment.