06 Mar 2013, 09:36
Generic-user-small

Steven Jing (4 posts)

when running the atm program even with the “eventually” method is applied, I still got a flicking result when running it repeatedly with command ruby -e “30.times {system ‘cucumber -f progress’}”

And I find sometimes the program will get into below result.

Within a scenario
1. First read from queue gets 0 instead of 100.
2. Second read from queue get -20 as expected.
Then finally the bankstore got -20 instead of expected 80

And this problem doesn’t seem to be the race between processor and cucumber. It’s weird

And this is the only case it will get into, and all the rest runs are passed.

06 Mar 2013, 09:47
Generic-user-small

Steven Jing (4 posts)

below is part of relevant code. I’m wondering if it is because that while it’s creating the 1st transaction file, before 100 writing is completed, the processor come to read the queue and find the file indeed exits, but just read 0, and then delete the file so that we got a “0” & “-20” result

class TransactionQueue def self.clear FileUtils.rm_rf(‘messages’) FileUtils.mkdir_p(‘messages’) end

def initialize
  @next_id = 1
end
def write(transaction)
  File.open("messages/#{@next_id}", 'w') { |f| f.puts(transaction) }
  @next_id += 1
end
def read
  next_message_file = Dir['messages/*'].first
  return unless next_message_file
  puts "Read: #{next_message_file}" 
  yield File.read(next_message_file)
  FileUtils.rm_rf(next_message_file)
end
end
  You must be logged in to comment