LIFX / lifx-gem

A Ruby gem that gives you the ability to easily interact with your LIFX lights.
http://lifx.co/
MIT License
179 stars 28 forks source link

GatewayConnection testing is unstable #43

Closed sinorga closed 4 years ago

sinorga commented 10 years ago

Hi, The rspec log is below.

LIFX::GatewayConnection
  write queue resiliency
    does not send if there is no available connection
    pushes message back into queue if unable to write (FAILED - 1)

Failures:

  1) LIFX::GatewayConnection write queue resiliency pushes message back into queue if unable to write
     Failure/Error: expect(gateway).to receive(:actually_write).and_return(false, true)
       (#<LIFX::GatewayConnection tcp= tcp_attempts=0 udp=#<LIFX::Transport::UDP 127.0.0.1:35003>>).actually_write(any args)
           expected: 2 times with any arguments
           received: 0 times with any arguments
     # ./spec/gateway_connection_spec.rb:22:in `block (3 levels) in <module:LIFX>'

Finished in 0.72379 seconds (files took 0.54662 seconds to load)

I have traced code of gateway_connection.rb. I think it is caused by thread of message queue pop message out before call actually_write(message) method. At this moment, the queue is empty and flush method check the queue size at the same time. it found queue is empty then return. Thus, actually_write(message) method won't be called. You can easy reproduce this issue by adding a long sleep right after message = @queue.pop in initialize_write_queue method.

I am new in Ruby world and interested in IoT, so I would like to solve this issue by myself for practicing more. A simple solution is only pop message out after actually_write(message) succeed. First of all, I need to write a test which can exactly reproduce this issue. But I think that run a large number of loops for trigger the issue is not a good way. Does anyone have suggestions? :D