Permalink
Browse files

tested and refactored Ruby version for TelemetryDiagnosticControls

  • Loading branch information...
1 parent d8f94c0 commit d95b67013258fb1b545548557bdcde3c4cc89833 Sam Serpoosh committed Sep 9, 2012
@@ -1 +1,2 @@
*.swp
+*.swo
@@ -2,31 +2,40 @@
class TelemetryDiagnosticControls
DIAGNOSTICCHANNELCONNECTIONSTRING = "*111#"
+ NUMBEROFRETRIES = 3
attr_accessor :diagnostic_info
- def initialize
- @telemetry_client = TelemetryClient.new
+ def initialize(telemetry_client=TelemetryClient.new)
+ @telemetry_client = telemetry_client
@diagnostic_info = ""
end
def check_transmission
- @diagnostic_info = ""
@telemetry_client.disconnect
+ try_to_connect
+
+ raise UnableToConnectException if(@telemetry_client.
+ online? == false)
+
+ @telemetry_client.send(TelemetryClient::DIAGNOSTICMESSAGE)
+ @diagnostic_info = @telemetry_client.receive
+ end
- retry_left = 3
- while (@telemetry_client.online_status == false && retry_left > 0)
+ private
+ def try_to_connect
+ retry_left = NUMBEROFRETRIES
+ while still_try?(retry_left)
@telemetry_client.connect(DIAGNOSTICCHANNELCONNECTIONSTRING)
retry_left -= 1
end
+ end
- if(@telemetry_client.online_status == false)
- raise Exception, "Unable to connect."
- end
-
- @telemetry_client.send(TelemetryClient::DIAGNOSTICMESSAGE)
- @diagnostic_info = @telemetry_client.receive
+ def still_try?(retry_left)
+ @telemetry_client.online? == false &&
+ retry_left > 0
end
end
-TelemetryDiagnosticControls.new.check_transmission
+class UnableToConnectException < Exception
+end
@@ -0,0 +1,35 @@
+require_relative "../../code/telemetry_system/telemetry_diagnostic_controls"
+
+describe TelemetryDiagnosticControls do
+ it "raises unable to connect when can't connect to client" do
+ telemetry_client = stub(:online? => false).as_null_object
+ controls = TelemetryDiagnosticControls.new(telemetry_client)
+ lambda do
+ controls.check_transmission
+ end.should raise_error(UnableToConnectException)
+ end
+
+ it "tries to connect few times and when it can't raises exceptin" do
+ telemetry_client = stub(:online? => false).as_null_object
+ telemetry_client.should_receive(:connect).exactly(
+ TelemetryDiagnosticControls::NUMBEROFRETRIES).times
+
+ controls = TelemetryDiagnosticControls.new(telemetry_client)
+ lambda do
+ controls.check_transmission
+ end.should raise_error(UnableToConnectException)
+ end
+
+ it "connects and send and receive message with client" do
+ receive_message = "this is from client"
+ telemetry_client = stub(:online? => true).as_null_object
+ telemetry_client.should_receive(:send).with(
+ TelemetryClient::DIAGNOSTICMESSAGE)
+ telemetry_client.should_receive(:receive).and_return(
+ receive_message)
+
+ controls = TelemetryDiagnosticControls.new(telemetry_client)
+ controls.check_transmission
+ controls.diagnostic_info.should == receive_message
+ end
+end
@@ -48,8 +48,8 @@ def receive
else
#simulate a received message
message = ""
- messageLength = 50 + rand(110)
- i = messageLength
+ message_length = 50 + rand(110)
+ i = message_length
while i >= 0
message += (40 + rand(126)).chr
i -= 1
@@ -28,5 +28,3 @@ def check_transmission
@diagnostic_info = @telemetry_client.receive
end
end
-
-TelemetryDiagnosticControls.new.check_transmission
@@ -0,0 +1,35 @@
+require_relative "../../code/telemetry_system/telemetry_diagnostic_controls"
+
+describe TelemetryDiagnosticControls do
+ it "raises unable to connect when can't connect to client" do
+ telemetry_client = stub(:online? => false).as_null_object
+ controls = TelemetryDiagnosticControls.new(telemetry_client)
+ lambda do
+ controls.check_transmission
+ end.should raise_error(UnableToConnectException)
+ end
+
+ it "tries to connect few times and when it can't raises exceptin" do
+ telemetry_client = stub(:online? => false).as_null_object
+ telemetry_client.should_receive(:connect).exactly(
+ TelemetryDiagnosticControls::NUMBEROFRETRIES).times
+
+ controls = TelemetryDiagnosticControls.new(telemetry_client)
+ lambda do
+ controls.check_transmission
+ end.should raise_error(UnableToConnectException)
+ end
+
+ it "connects and send and receive message with client" do
+ receive_message = "this is from client"
+ telemetry_client = stub(:online? => true).as_null_object
+ telemetry_client.should_receive(:send).with(
+ TelemetryClient::DIAGNOSTICMESSAGE)
+ telemetry_client.should_receive(:receive).and_return(
+ receive_message)
+
+ controls = TelemetryDiagnosticControls.new(telemetry_client)
+ controls.check_transmission
+ controls.diagnostic_info.should == receive_message
+ end
+end

0 comments on commit d95b670

Please sign in to comment.