Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Sam-Serpoosh/master
Added the Ruby version of TelemetrySystem both original code and also the tested and refactored version!
- Loading branch information
Showing
8 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1 +1,2 @@ | |||
*.swp | *.swp | ||
*.swo |
85 changes: 85 additions & 0 deletions
85
TDDMicroExercises.YoursSolutions/Sam-Serpoosh/code/telemetry_system/telemetry_client.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,85 @@ | |||
class TelemetryClient | |||
DIAGNOSTICMESSAGE = "AT#UD" | |||
|
|||
attr_reader :diagnostic_message_result | |||
|
|||
def initialize(connection_events_simulator=ConnectionEventsSimulator) | |||
@online_status = false | |||
@diagnostic_message_result = "" | |||
@connection_events_simulator = connection_events_simulator | |||
end | |||
|
|||
def online? | |||
@online_status | |||
end | |||
|
|||
def connect(telemetry_server_connection_string) | |||
raise ArgumentNullException if String.null_or_empty?( | |||
telemetry_server_connection_string) | |||
|
|||
#simulate the operation on a real modem | |||
success = | |||
@connection_events_simulator.connect(1, 10) <= 8 | |||
@online_status = success | |||
end | |||
|
|||
def disconnect | |||
@online_status = false | |||
end | |||
|
|||
def send(message) | |||
raise ArgumentNullException if String.null_or_empty?( | |||
message) | |||
|
|||
if (message == DIAGNOSTICMESSAGE) | |||
#simulate a status report | |||
@diagnostic_message_result = "some failure message" | |||
return | |||
end | |||
#here should go the real Send operation | |||
end | |||
|
|||
def receive | |||
if !String.null_or_empty?(@diagnostic_message_result) | |||
message = @diagnostic_message_result | |||
reset_diagnostic_message | |||
else | |||
#simulate a received message | |||
message = receive_message_in_non_failure_case | |||
end | |||
|
|||
message | |||
end | |||
|
|||
private | |||
def reset_diagnostic_message | |||
@diagnostic_message_result = "" | |||
end | |||
|
|||
def receive_message_in_non_failure_case | |||
message = "" | |||
message_length = | |||
@connection_events_simulator.connect(65, 110) | |||
while message_length >= 0 | |||
message += | |||
@connection_events_simulator.connect(40, 126).chr | |||
message_length -= 1 | |||
end | |||
message | |||
end | |||
end | |||
|
|||
class ArgumentNullException < Exception | |||
end | |||
|
|||
class ConnectionEventsSimulator | |||
def self.connect(base_number, upper_bound) | |||
base_number + rand(upper_bound) | |||
end | |||
end | |||
|
|||
class String | |||
def self.null_or_empty?(str) | |||
str.nil? || str == "" | |||
end | |||
end |
41 changes: 41 additions & 0 deletions
41
...rcises.YoursSolutions/Sam-Serpoosh/code/telemetry_system/telemetry_diagnostic_controls.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,41 @@ | |||
require_relative "./telemetry_client" | |||
|
|||
class TelemetryDiagnosticControls | |||
DIAGNOSTICCHANNELCONNECTIONSTRING = "*111#" | |||
NUMBEROFRETRIES = 3 | |||
|
|||
attr_accessor :diagnostic_info | |||
|
|||
def initialize(telemetry_client=TelemetryClient.new) | |||
@telemetry_client = telemetry_client | |||
@diagnostic_info = "" | |||
end | |||
|
|||
def check_transmission | |||
@telemetry_client.disconnect | |||
try_to_connect | |||
|
|||
raise UnableToConnectException if(@telemetry_client. | |||
online? == false) | |||
|
|||
@telemetry_client.send(TelemetryClient::DIAGNOSTICMESSAGE) | |||
@diagnostic_info = @telemetry_client.receive | |||
end | |||
|
|||
private | |||
def try_to_connect | |||
retry_left = NUMBEROFRETRIES | |||
while still_try?(retry_left) | |||
@telemetry_client.connect(DIAGNOSTICCHANNELCONNECTIONSTRING) | |||
retry_left -= 1 | |||
end | |||
end | |||
|
|||
def still_try?(retry_left) | |||
@telemetry_client.online? == false && | |||
retry_left > 0 | |||
end | |||
end | |||
|
|||
class UnableToConnectException < Exception | |||
end |
60 changes: 60 additions & 0 deletions
60
...Exercises.YoursSolutions/Sam-Serpoosh/spec/telemetry_system_spec/telemetry_client_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,60 @@ | |||
require_relative "../../code/telemetry_system/telemetry_client" | |||
|
|||
describe TelemetryClient do | |||
it "is not online at the beginning" do | |||
subject.should_not be_online | |||
end | |||
|
|||
it "raises exception when connection string is nil or empty" do | |||
lambda do | |||
subject.connect(nil) | |||
end.should raise_error(ArgumentNullException) | |||
end | |||
|
|||
it "becomes online when connect to it" do | |||
good_connection_range = 1 | |||
StubConnectionEventsSimulator.stub(:connect => good_connection_range) | |||
client = TelemetryClient.new(StubConnectionEventsSimulator) | |||
client.connect("test server connection") | |||
client.should be_online | |||
end | |||
|
|||
it "desn't become online when simulator can't connect" do | |||
bad_connection_range = 10 | |||
StubConnectionEventsSimulator.stub(:connect => bad_connection_range) | |||
client = TelemetryClient.new(StubConnectionEventsSimulator) | |||
client.connect("test server connection") | |||
client.should_not be_online | |||
end | |||
|
|||
it "can disconnect" do | |||
subject.disconnect | |||
subject.should_not be_online | |||
end | |||
|
|||
it "raises exception when sending message is nil or empty" do | |||
lambda do | |||
subject.send(nil) | |||
end.should raise_error(ArgumentNullException) | |||
end | |||
|
|||
it "doesn't send message when it's a diagnostic message" do | |||
subject.send(TelemetryClient::DIAGNOSTICMESSAGE) | |||
subject.diagnostic_message_result.should == "some failure message" | |||
end | |||
|
|||
it "receives the diagnostic message if there is any and reset the diagnostic message" do | |||
subject.send(TelemetryClient::DIAGNOSTICMESSAGE) | |||
subject.receive.should == "some failure message" | |||
subject.diagnostic_message_result.should == "" | |||
end | |||
|
|||
it "receives the message when there is no diagnostic message" do | |||
StubConnectionEventsSimulator.stub(:connect => 97) | |||
client = TelemetryClient.new(StubConnectionEventsSimulator) | |||
client.send("test server connection") | |||
client.receive.should == "a" * 98 | |||
end | |||
end | |||
|
|||
class StubConnectionEventsSimulator; end |
35 changes: 35 additions & 0 deletions
35
...rsSolutions/Sam-Serpoosh/spec/telemetry_system_spec/telemetry_diagnostic_controls_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
61 changes: 61 additions & 0 deletions
61
TDDMicroExercises/Ruby/telemetry_system/telemetry_client.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,61 @@ | |||
class TelemetryClient | |||
DIAGNOSTICMESSAGE = "AT#UD" | |||
|
|||
def initialize | |||
@online_status = false | |||
@diagnostic_message_result = "" | |||
end | |||
|
|||
def online_status | |||
@online_status | |||
end | |||
|
|||
def connect(telemetry_server_connection_string) | |||
if (telemetry_server_connection_string.nil? || telemetry_server_connection_string == "") | |||
raise Exception, "Argument Null" | |||
end | |||
|
|||
#simulate the operation on a real modem | |||
success = (1 + rand(10)) <= 8 | |||
|
|||
@online_status = success | |||
end | |||
|
|||
def disconnect | |||
@online_status = false | |||
end | |||
|
|||
def send(message) | |||
if (message.nil? || message == "") | |||
raise Exception, "Argument Null" | |||
end | |||
|
|||
if (message == DIAGNOSTICMESSAGE) | |||
#simulate a status report | |||
@diagnostic_message_result = | |||
"LAST TX rate................ 100 MBPS\nHIGHEST TX rate............. 100 MBPS\nLAST RX rate................ 100 MBPS\nHIGHEST RX rate............. 100 MBPS\nBIT RATE.................... 100000000\nWORD LEN.................... 16\nWORD/FRAME.................. 511\nBITS/FRAME.................. 8192\nMODULATION TYPE............. PCM/FM\nTX Digital Los.............. 0.75\nRX Digital Los.............. 0.10\nBEP Test.................... -5\nLocal Rtrn Count............ 00\nRemote Rtrn Count........... 00" | |||
|
|||
return | |||
end | |||
|
|||
#here should go the real Send operation | |||
end | |||
|
|||
def receive | |||
if (!@diagnostic_message_result.nil? && @diagnostic_message_result != "") | |||
message = @diagnostic_message_result | |||
@diagnostic_message_result = "" | |||
else | |||
#simulate a received message | |||
message = "" | |||
message_length = 50 + rand(110) | |||
i = message_length | |||
while i >= 0 | |||
message += (40 + rand(126)).chr | |||
i -= 1 | |||
end | |||
end | |||
|
|||
return message | |||
end | |||
end |
30 changes: 30 additions & 0 deletions
30
TDDMicroExercises/Ruby/telemetry_system/telemetry_diagnostic_controls.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,30 @@ | |||
require_relative "./telemetry_client" | |||
|
|||
class TelemetryDiagnosticControls | |||
DIAGNOSTICCHANNELCONNECTIONSTRING = "*111#" | |||
|
|||
attr_accessor :diagnostic_info | |||
|
|||
def initialize | |||
@telemetry_client = TelemetryClient.new | |||
@diagnostic_info = "" | |||
end | |||
|
|||
def check_transmission | |||
@diagnostic_info = "" | |||
@telemetry_client.disconnect | |||
|
|||
retry_left = 3 | |||
while (@telemetry_client.online_status == false && retry_left > 0) | |||
@telemetry_client.connect(DIAGNOSTICCHANNELCONNECTIONSTRING) | |||
retry_left -= 1 | |||
end | |||
|
|||
if(@telemetry_client.online_status == false) | |||
raise Exception, "Unable to connect." | |||
end | |||
|
|||
@telemetry_client.send(TelemetryClient::DIAGNOSTICMESSAGE) | |||
@diagnostic_info = @telemetry_client.receive | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |