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

21 Nov 2013, 23:00
Generic-user-small

Ariel Kirkwood (5 posts)

I’m running into issues here, too. Here’s Steven Jing’s code sample, formatted:

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 # this was added by Steven
    File.read(next_message_file) 
    FileUtils.rm_rf(next_message_file) 
  end 
end

I put a few extra puts statements into my TransactionQueue functions, too, and found some weirdness going on with the order of reading, writing, and deleting files.

My tests are still flickering, too. I’ve ensured that the code in the book matches the code in my files.

21 Nov 2013, 23:21
Generic-user-small

Ariel Kirkwood (5 posts)

To be 100% clear - my tests pass in 29 of the 30 instances I ran cucumber -f progress. Here’s the output from the time it failed:

Starting transaction_processor in /Users/arielkirkwood/Code/cucumber_atm_async with 'ruby lib/transaction_processor.rb'
transaction processor ready
Server transaction_processor (68242) is up.
----.......F...........

(::) failed steps (::)


expected: 40
     got: 100

(compared using ==)
 (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/account_steps.rb:6:in `block (2 levels) in <top (required)>'
./features/support/async_support.rb:8:in `block in eventually'
./features/support/async_support.rb:6:in `loop'
./features/support/async_support.rb:6:in `eventually'
./features/step_definitions/account_steps.rb:6:in `/^the balance of my account should be (\$(?:\d+))$/'
features/cash_withdrawal.feature:7:in `And the balance of my account should be $<remaining_balance>'

Failing Scenarios:
cucumber features/cash_withdrawal.feature:3 # Scenario: Successful withdrawal of $20 from an account in credit

5 scenarios (1 failed, 4 passed)
19 steps (1 failed, 18 passed)
0m2.187s
Shutting down transaction_processor (68242)
Server transaction_processor (68242) (68242) is shut down
21 Nov 2013, 23:23
Generic-user-small

Ariel Kirkwood (5 posts)

Oops, I should probably add - I changed the cash_withdrawal feature to use a Scenario Outline instead of just a single scenario. Here’s the text:

Feature: Cash Withdrawal

  Scenario Outline: Successful withdrawal of $20 from an account in credit
    Given my account has been credited with $<amount_credited>
    When I withdraw $<amount_withdrawn>
    Then $<amount_dispensed> should be dispensed
      And the balance of my account should be $<remaining_balance>

    Examples:
      | amount_credited | amount_withdrawn | amount_dispensed | remaining_balance |
      | 100 | 20 | 20 | 80 |
      | 100 | 60 | 60 | 40 |
      | 100 | 100 | 100 | 0 |
21 Nov 2013, 23:53
Generic-user-small

Ariel Kirkwood (5 posts)

So, correct me if I’m wrong, but is the end result after implementing eventually that tests will still occasionally flicker? I finished actually reading chapter 9 (doh) and am thinking that because we aren’t listening for events and are only sampling, that there is always a risk of flickering tests.

If this is the case and/or the lesson intended to be taught to the reader, I might suggest making this a bit more upfront; I spent a ton of extra time looking through my code trying to figure out why I couldn’t get 30 iterations of tests to pass with 100% accuracy.

22 Nov 2013, 21:45
Avatar_pragsmall

Matt Wynne (86 posts)

Hi Ariel and Steven,

Sorry to leave you in the lurch on this - neither Aslak or I were getting notified about messages to this forum so we didn’t realise you were stuck. We’ll have a look into your problem now and get back to you as soon as possible.

22 Nov 2013, 22:54
Avatar_pragsmall

Matt Wynne (86 posts)

Please can you download this file and try again?

http://media.pragprog.com/titles/hwcuc/code/message_queues/01/lib/transaction_queue.rb

I think there was a significant problem with Steven’s edits that would have caused it to behave unexpectedly.

23 Nov 2013, 17:16
Generic-user-small

Ariel Kirkwood (5 posts)

Matt - I copied the code to my codebase and noted the difference between that file and my own version of transaction_queue.rb:


diff --git a/lib/transaction_queue.rb b/lib/transaction_queue.rb
index 075eb59..f6d93e3 100644
--- a/lib/transaction_queue.rb
+++ b/lib/transaction_queue.rb
@@ -11,7 +11,7 @@ class TransactionQueue
   end
 
   def write(transaction)
-    File.open("messages/#{@next_id}", "w") { |f| f.puts(transaction) }
+    File.open("messages/#{@next_id}", 'w') { |f| f.puts(transaction) }
     @next_id += 1
   end
 

Still, even with the small change, I had flickering tests. :(

  You must be logged in to comment