I had this code passing ALL the specs the other day, so I moved it into another folder (for completed assignments). When I tried to run the specs again on it from there, I couldn't get the file to load despite typing the path exactly (several times) as I saw it. Hmmmm. Is there some special way to run rspecs from a subfolder?? Anyways, So I moved it back to the main directory it was in, and tried from there, and suddenly it would't pass!! I must have accidentally deleted something. But for the life of me after almost two days...I just DON'T SEE IT. I've gone through it a million times and it seems like it should be working, and when I take the methods out of the class they return as expected. I need fresh eyes. Why is this code suddenly not passing?
class :: Timer
attr_accessor :seconds, :padded, :time_string
def initialize(seconds = 0)
#seconds = seconds
end
def padded(num)
num = num.to_s
num.length == 1 ? "0#{num}" : "#{num}"
end
def time_string=(seconds)
#seconds = seconds
mins = 0
hours = 0
if seconds == 0
return "00:00:00"
elsif seconds < 60
return "00:00:#{padded(seconds)}"
end
while seconds > 59
mins += 1
seconds -= 60
if mins > 59
hours += 1
mins -= 60
end
end#while
return "#{padded(hours)}:#{padded(mins)}:#{padded(seconds)}"
end
end#class
Here are the specs:
require_relative '09_timer'
describe "Timer" do
before(:each) do
#timer = Timer.new
end
it "should initialize to 0 seconds" do
#timer.seconds.should == 0
end
describe 'time_string' do
it "should display 0 seconds as 00:00:00" do
#timer.seconds = 0
#timer.time_string.should == "00:00:00"
end
it "should display 12 seconds as 00:00:12" do
#timer.seconds = 12
#timer.time_string.should == "00:00:12"
end
it "should display 66 seconds as 00:01:06" do
#timer.seconds = 66
#timer.time_string.should == "00:01:06"
end
it "should display 4000 seconds as 01:06:40" do
#timer.seconds = 4000
#timer.time_string.should == "01:06:40"
end
end
# One way to implement the Timer is with a helper method.
# Uncomment these specs if you want to test-drive that
# method, then call that method from inside of time_string.
#
describe 'padded' do
it 'pads zero' do
#timer.padded(0).should == '00'
end
it 'pads one' do
#timer.padded(1).should == '01'
end
it "doesn't pad a two-digit number" do
#timer.padded(12).should == '12'
end
end
end
class :: Timer
attr_accessor :seconds
def initialize(seconds = 0)
#seconds = seconds
end
def padded(num)
num = num.to_s
num.length == 1 ? "0#{num}" : "#{num}"
end
def time_string
seconds = #seconds
mins = 0
hours = 0
if seconds == 0
return "00:00:00"
elsif seconds < 60
return "00:00:#{padded(seconds)}"
end
while seconds > 59
mins += 1
seconds -= 60
if mins > 59
hours += 1
mins -= 60
end
end#while
return "#{padded(hours)}:#{padded(mins)}:#{padded(seconds)}"
end
end#class
Specs
require_relative '09_timer'
describe "Timer" do
before(:each) do
#timer = Timer.new
end
it "should initialize to 0 seconds" do
expect(#timer.seconds).to eq 0
end
describe 'time_string' do
it "should display 0 seconds as 00:00:00" do
#timer.seconds = 0
expect(#timer.time_string).to eq "00:00:00"
end
it "should display 12 seconds as 00:00:12" do
#timer.seconds = 12
expect(#timer.time_string).to eq "00:00:12"
end
it "should display 66 seconds as 00:01:06" do
#timer.seconds = 66
expect(#timer.time_string).to eq "00:01:06"
end
it "should display 4000 seconds as 01:06:40" do
#timer.seconds = 4000
expect(#timer.time_string).to eq "01:06:40"
end
end
# One way to implement the Timer is with a helper method.
# Uncomment these specs if you want to test-drive that
# method, then call that method from inside of time_string.
#
describe 'padded' do
it 'pads zero' do
expect(#timer.padded(0)).to eq '00'
end
it 'pads one' do
expect(#timer.padded(1)).to eq '01'
end
it "doesn't pad a two-digit number" do
expect(#timer.padded(12)).to eq '12'
end
end
end
I don't really know how your code got messed up, but the time_string method isn't taking any arguments in your specs, and I don't see why it would just from a logical perspective. Therefore I took out the =(seconds) part from its method signature.
You were also setting the instance variable #seconds to the passed argument, but really we want to use a local seconds variable that starts out as the instance variable, so that just gets flipped around to seconds = #seconds.
Lastly, I changed your specs to use the new expect syntax instead of should which is now deprecated.
Related
I am using the redis-semaphore gem, version 0.3.1.
For some reason, I occasionally can't release a stale Redis lock. From my analysis it seems to happen if my Docker process crashed after the lock was created.
I have described my debugging process below and would like to know if anyone can suggest how to further debug.
Assume that we want to create a redis lock with this name:
name = "test"
We insert this variable in two different terminal windows. In the first, we run:
def lock_for_15_secs(name)
job = Redis::Semaphore.new(name.to_sym, redis: NonBlockingRedis.new(), custom_blpop: true, :stale_client_timeout => 15)
if job.lock(-1) == "0"
puts "Locked and starting"
sleep(15)
puts "Now it's stale, try to release in another process"
sleep(15)
puts "Now trying to unlock"
unlock = job.unlock
puts unlock == false ? "Wuhuu, already unlocked" : "Hm, should have been unlocked by another process, but wasn't"
end
end
lock_for_15_secs(name)
In the second we run:
def release_and_lock(name)
job = Redis::Semaphore.new(name.to_sym, redis: NonBlockingRedis.new(), custom_blpop: true, :stale_client_timeout => 15)
release = job.release_stale_locks!
count = job.available_count
puts "Release reponse is #{release.inspect} and available count is #{count}"
if job.lock(-1) == "0"
puts "Wuhuu, we can lock it"
job.unlock
else
puts "Hmm, we can't lock it"
end
end
release_and_lock(name)
This usually plays out as expected. For 15 seconds, the second terminal can't relase the lock, but when run after 15 seconds, it releases. Below is the output from release_and_lock(name).
Before 15 seconds have passed:
irb(main):1:0> release_and_lock(name)
Release reponse is {"0"=>"1580292557.321834"} and available count is 0
Hmm, we can't lock it
=> nil
After 15 seconds have passed:
irb(main):2:0> release_and_lock(name)
Release reponse is {"0"=>"1580292557.321834"} and available count is 1
Wuhuu, we can lock it
=> 1
irb(main):3:0> release_and_lock(name)
Release reponse is {} and available count is 1
Wuhuu, we can lock it
But whenever I see that a stale lock isn't released, and I run release_and_lock(name) to diagnose, this is returned:
irb(main):4:0> release_and_lock(name)
Release reponse is {} and available count is 0
Hmm, we can't lock it
And at this point my only option is to flush redis:
require 'non_blocking_redis'
non_blocking_redis = NonBlockingRedis.new()
non_blocking_redis.flushall
P.s. My NonBlockingRedis inherits from Redis:
class Redis
class Semaphore
def initialize(name, opts = {})
#custom_opts = opts
#name = name
#resource_count = opts.delete(:resources) || 1
#stale_client_timeout = opts.delete(:stale_client_timeout)
#redis = opts.delete(:redis) || Redis.new(opts)
#use_local_time = opts.delete(:use_local_time)
#custom_blpop = opts.delete(:custom_blpop) # false=queue, true=cancel
#tokens = []
end
def lock(timeout = 0)
exists_or_create!
release_stale_locks! if check_staleness?
token_pair = #redis.blpop(available_key, timeout, #custom_blpop)
return false if token_pair.nil?
current_token = token_pair[1]
#tokens.push(current_token)
#redis.hset(grabbed_key, current_token, current_time.to_f)
if block_given?
begin
yield current_token
ensure
signal(current_token)
end
end
current_token
end
alias_method :wait, :lock
end
end
class NonBlockingRedis < Redis
def initialize(options = {})
if options.empty?
options = {
url: Rails.application.secrets.redis_url,
db: Rails.application.secrets.redis_sidekiq_db,
driver: :hiredis,
network_timeout: 5
}
end
super(options)
end
def blpop(key, timeout, custom_blpop)
if custom_blpop
if timeout == -1
result = lpop(key)
return result if result.nil?
return [key, result]
else
super(key, timeout)
end
else
super
end
end
def lock(timeout = 0)
exists_or_create!
release_stale_locks! if check_staleness?
token_pair = #redis.blpop(available_key, timeout, #custom_blpop)
return false if token_pair.nil?
current_token = token_pair[1]
#tokens.push(current_token)
#redis.hset(grabbed_key, current_token, current_time.to_f)
if block_given?
begin
yield current_token
ensure
signal(current_token)
end
end
current_token
end
alias_method :wait, :lock
end
require 'non_blocking_redis'
😜 An awesome bug 👏
The bug
I think it happens if you kill the process when it does lpop on the SEMAPHORE:test:AVAILABLE
Most probably here https://github.com/dv/redis-semaphore/blob/v0.3.1/lib/redis/semaphore.rb#L67
To replicate it
NonBlockingRedis.new.flushall
release_and_lock('test');
NonBlockingRedis.new.lpop('SEMAPHORE:test:AVAILABLE')
Now initially you have:
SEMAPHORE:test:AVAILABLE 0
SEMAPHORE:test:VERSION 1
SEMAPHORE:test:EXISTS 1
After the above code you get:
SEMAPHORE:test:VERSION 1
SEMAPHORE:test:EXISTS 1
The code checks the SEMAPHORE:test:EXISTS and then expects to have SEMAPHORE:test:AVAILABLE / SEMAPHORE:test:GRABBED
Solution
From my brief check I don't think it is possible to make the gem work without a modification. I tried adding an expiration: but somehow it managed to disable the expiration for SEMAPHORE:test:EXISTS
NonBlockingRedis.new.ttl('SEMAPHORE:test:EXISTS') # => -1 and it should have been e.g. 20 seconds and going down
So.. maybe a fix will be
class Redis
class Semaphore
def exists_or_create!
token = #redis.getset(exists_key, EXISTS_TOKEN)
if token.nil? || all_tokens.empty?
create!
else
# Previous versions of redis-semaphore did not set `version_key`.
# Make sure it's set now, so we can use it in future versions.
if token == API_VERSION && #redis.get(version_key).nil?
#redis.set(version_key, API_VERSION)
end
true
end
end
end
end
the all_tokens is https://github.com/dv/redis-semaphore/blob/v0.3.1/lib/redis/semaphore.rb#L120
I'll open a PR to the gem shortly -> https://github.com/dv/redis-semaphore/pull/66 maybe 🤷♂️
Note 1
Not sure how you use the NonBlockingRedis but it is not in use in Redis::Semaphore. You do lock(-1) which does in the code lpop. Also the code never calls your lock.
Random
Here is a helper to dump the keys
class Test
def self.all
r = NonBlockingRedis.new
puts r.keys('*').map { |k|
[
k,
((r.hgetall(k) rescue r.get(k)) rescue r.lrange(k, 0, -1).join(' | '))
].join("\t\t")
}
end
end
> Test.all
SEMAPHORE:test:AVAILABLE 0
SEMAPHORE:test:VERSION 1
SEMAPHORE:test:EXISTS 1
For completeness here is how it looks when you have grabbed the lock
SEMAPHORE:test:VERSION 1
SEMAPHORE:test:EXISTS 1
SEMAPHORE:test:GRABBED {"0"=>"1583672948.7168388"}
I have the following code to compile jobs from github jobs API. How do I reset a counter back to 0 every time I call on a new city? I've tried putting it in several different places with no luck.
def ft_count_and_percentage
##url += #city
uri = URI(##url)
response = Net::HTTP.get(uri)
result = JSON.parse(response)
result.each do |job|
if job["type"] == "Full Time"
##fulltime_count += 1
end
end
puts "Total number of jobs in #{#city}: #{result.length}"
if ##fulltime_count > 0
puts ("full time percent ") + "#{(##fulltime_count/result.length) * 100}"
else
puts "No FT Positions"
end
end
##fulltime_count is defined outside this method to start at 0. Currently, as expected the counter just keeps adding jobs every time I add a new city.
boston = Job.new("Boston")
boston.ft_count_and_percentage
sf = Job.new("San Francisco")
sf.ft_count_and_percentage
la = Job.new("Los Angeles")
la.ft_count_and_percentage
denver = Job.new("Denver")
denver.ft_count_and_percentage
boulder = Job.new("Boulder")
boulder.ft_count_and_percentage
chicago = Job.new("Chicago")
chicago.ft_count_and_percentage
ny = Job.new("New York City")
ny.ft_count_and_percentage
You may need to reset it inside Job init
class Job
def initialize
##count = 0
end
def ft_count_and_percentage
#the blah you already have
end
end
Hi I'm completely new to Ruby. I'm trying to run Minitests, it use to work fine until I added a constructor to my CountDown class.
Here is the code:
require 'benchmark'
require 'minitest/autorun'
#! /usr/bin/env ruby
#This is our countdown class
# :reek:DuplicateMethodCall
# :reek:TooManyStatements
class CountDown
def initialize(time)
#time = time
end
def count()
wait_time = #time.to_i
print wait_time.to_s + " seconds left\n"
sleep 1
wait_time = wait_time - 1
wait_time.downto(1).each do |time_left|
sleep 1
print time_left.to_s + " seconds left\n" if (time_left % 60) == 0
end
print "\a"
end
end
#This class is responsible for our test cases
class CountDownTest < Minitest::Test
def setup
#count_down = CountDown.new(10)
end
def testing_lowerbound
time = Benchmark.measure{
#count_down.count
}
assert time.real.to_i == 10
end
end
This my my output:
teamcity[enteredTheMatrix timestamp = '2017-09-28T15:10:11.470-0700']
teamcity[testCount count = '0' timestamp = '2017-09-28T15:10:11.471-0700'] Finished in 0.00038s 0 tests, 0
assertions, 0 failures, 0 errors, 0 skips
Process finished with exit code 0
Any idea what's wrong? It looks fine to me.
Tests should start with the prefix test_ not testing_. Using the wrong prefix makes MiniTest assume they're doing something other than running a test, so it ignores them.
I am trying to implement a simple timeout class that handles timeouts of different requests.
Here is the first version:
class MyTimer
def handleTimeout mHash, k
while mHash[k] > 0 do
mHash[k] -=1
sleep 1
puts "#{k} : #{mHash[k]}"
end
end
end
MAX = 3
timeout = Hash.new
timeout[1] = 41
timeout[2] = 5
timeout[3] = 14
t1 = MyTimer.new
t2 = MyTimer.new
t3 = MyTimer.new
first = Thread.new do
t1.handleTimeout(timeout,1)
end
second = Thread.new do
t2.handleTimeout(timeout,2)
end
third = Thread.new do
t3.handleTimeout(timeout,3)
end
first.join
second.join
third.join
This seems to work fine. All the timeouts work independently of each other.
Screenshot attached
The second version of the code however produces different results:
class MyTimer
def handleTimeout mHash, k
while mHash[k] > 0 do
mHash[k] -=1
sleep 1
puts "#{k} : #{mHash[k]}"
end
end
end
MAX = 3
timeout = Hash.new
timers = Array.new(MAX+1)
threads = Array.new(MAX+1)
for i in 0..MAX do
timeout[i] = rand(40)
# To see timeout value
puts "#{i} : #{timeout[i]}"
end
sleep 1
for i in 0..MAX do
timers[i] = MyTimer.new
threads[i] = Thread.new do
timers[i].handleTimeout( timeout, i)
end
end
for i in 0..MAX do
threads[i].join
end
Screenshot attached
Why is this happening?
How can I implement this functionality using arrays?
Is there a better way to implement the same functionality?
In the loop in which you are creating threads by using Thread.new, the variable i is shared between main thread (where threads are getting created) and in the threads created. So, the value of i seen by handleTimeout is not consistent and you get different results.
You can validate this by adding a debug statement in your method:
#...
def handleTimeout mHash, k
puts "Handle timeout called for #{mHash} and #{k}"
#...
end
#...
To fix the issue, you need to use code like below. Here parameters are passed to Thread.new and subsequently accessed using block variables.
for i in 0..MAX do
timers[i] = MyTimer.new
threads[i] = Thread.new(timeout, i) do |a, b|
timers[i].handleTimeout(a, b)
end
end
More on this issue is described in When do you need to pass arguments to Thread.new? and this article.
I'm starting the 06_performance_monitor exercise in TestFirst Ruby and the failure it lists is in the Rspec that was provided in the materials for TestFirst Ruby tutorial (where it says undefined method 'measure'). From what I've been able to find online, it seems this tutorial was written for Rspec 2 and Ruby 1.9. I currently have Rspec 2.99.0, Ruby 1.9.3, and Rake 0.9.22 installed. I guess my question is, what am I doing wrong/do I need different versions? I don't understand why the provided Rspec doesn't seem to work as it should. This happened in one other exercise for this tutorial but it was only a minor error message that didn't interfere with my testing.
caitlyns-mbp:06_performance_monitor caitlynyu$ rake
(in /Users/caitlynyu/Desktop/learn_ruby)
Run options: include {:focus=>true}
All examples were filtered out; ignoring {:focus=>true}
Performance Monitor
runs a block N times (FAILED - 1)
Failures:
1) Performance Monitor runs a block N times
Failure/Error: measure(4) do
NoMethodError:
undefined method `measure' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fcdc11b8578>
# ./06_performance_monitor/performance_monitor_spec.rb:53:in `block (2 levels) in <top (required)>'
Finished in 0.00081 seconds
1 example, 1 failure
Failed examples:
rspec ./06_performance_monitor/performance_monitor_spec.rb:51 # Performance Monitor runs a block N times
Randomized with seed 2933
/Users/caitlynyu/.rvm/rubies/ruby-1.9.3-p547/bin/ruby -S rspec /Users/caitlynyu/Desktop/learn_ruby/06_performance_monitor/performance_monitor_spec.rb -I/Users/caitlynyu/Desktop/learn_ruby/06_performance_monitor -I/Users/caitlynyu/Desktop/learn_ruby/06_performance_monitor/solution -f documentation -r ./rspec_config failed
caitlyns-mbp:06_performance_monitor caitlynyu$
UPDATE
My code below (I know it doesn't pass any of the tests below, I was just playing around with this first):
def performance_monitor
start = Time.now
yield
return "#{Time.now - start}"
end
I've never used a method called 'measure' that shows up in the Rspec which is below:
# # Topics
#
# * stubs
# * blocks
# * yield
#
# # Performance Monitor
#
# This is (a stripped down version of) an actual useful concept: a
# function that runs a block of code and then tells you how long it
# took to run.
require "performance_monitor"
require "time" # loads up the Time.parse method -- do NOT create time.rb!
describe "Performance Monitor" do
before do
#eleven_am = Time.parse("2011-1-2 11:00:00")
end
it "takes about 0 seconds to run an empty block" do
elapsed_time = measure do
end
elapsed_time.should be_within(0.1).of(0)
end
it "takes exactly 0 seconds to run an empty block (with stubs)" do
Time.stub(:now) { #eleven_am }
elapsed_time = measure do
end
elapsed_time.should == 0
end
it "takes about 1 second to run a block that sleeps for 1 second" do
elapsed_time = measure do
sleep 1
end
elapsed_time.should be_within(0.1).of(1)
end
it "takes exactly 1 second to run a block that sleeps for 1 second (with stubs)" do
fake_time = #eleven_am
Time.stub(:now) { fake_time }
elapsed_time = measure do
fake_time += 60 # adds one minute to fake_time
end
elapsed_time.should == 60
end
it "runs a block N times" do
n = 0
measure(4) do
n += 1
end
n.should == 4
end
it "returns the average time, not the total time, when running multiple times" do
run_times = [8,6,5,7]
fake_time = #eleven_am
Time.stub(:now) { fake_time }
average_time = measure(4) do
fake_time += run_times.pop
end
average_time.should == 6.5
end
it "returns the average time when running a random number of times for random lengths of time" do
fake_time = #eleven_am
Time.stub(:now) { fake_time }
number_of_times = rand(10) + 2
average_time = measure(number_of_times) do
delay = rand(10)
fake_time += delay
end
average_time.should == (fake_time - #eleven_am).to_f/number_of_times
end
end