@@ -67,5 +67,71 @@ module LaunchDarkly
6767 expect ( listener . statuses [ 1 ] . last_error . kind ) . to eq ( Interfaces ::DataSource ::ErrorInfo ::INVALID_DATA )
6868 end
6969 end
70+
71+ describe '#log_connection_result' do
72+ it "logs successful connection when diagnostic_accumulator is provided" do
73+ diagnostic_accumulator = double ( "DiagnosticAccumulator" )
74+ expect ( diagnostic_accumulator ) . to receive ( :record_stream_init ) . with (
75+ kind_of ( Integer ) ,
76+ false ,
77+ kind_of ( Integer )
78+ )
79+
80+ processor = subject . new ( "sdk_key" , config , diagnostic_accumulator )
81+ processor . send ( :log_connection_started )
82+ processor . send ( :log_connection_result , true )
83+ end
84+
85+ it "logs failed connection when diagnostic_accumulator is provided" do
86+ diagnostic_accumulator = double ( "DiagnosticAccumulator" )
87+ expect ( diagnostic_accumulator ) . to receive ( :record_stream_init ) . with (
88+ kind_of ( Integer ) ,
89+ true ,
90+ kind_of ( Integer )
91+ )
92+
93+ processor = subject . new ( "sdk_key" , config , diagnostic_accumulator )
94+ processor . send ( :log_connection_started )
95+ processor . send ( :log_connection_result , false )
96+ end
97+
98+ it "logs connection metrics with correct timestamp and duration" do
99+ diagnostic_accumulator = double ( "DiagnosticAccumulator" )
100+
101+ processor = subject . new ( "sdk_key" , config , diagnostic_accumulator )
102+
103+ expect ( diagnostic_accumulator ) . to receive ( :record_stream_init ) do |timestamp , failed , duration |
104+ expect ( timestamp ) . to be_a ( Integer )
105+ expect ( timestamp ) . to be > 0
106+ expect ( failed ) . to eq ( false )
107+ expect ( duration ) . to be_a ( Integer )
108+ expect ( duration ) . to be >= 0
109+ end
110+
111+ processor . send ( :log_connection_started )
112+ sleep ( 0.01 ) # Small delay to ensure measurable duration
113+ processor . send ( :log_connection_result , true )
114+ end
115+
116+ it "only logs once per connection attempt" do
117+ diagnostic_accumulator = double ( "DiagnosticAccumulator" )
118+ expect ( diagnostic_accumulator ) . to receive ( :record_stream_init ) . once
119+
120+ processor = subject . new ( "sdk_key" , config , diagnostic_accumulator )
121+ processor . send ( :log_connection_started )
122+ processor . send ( :log_connection_result , true )
123+ # Second call should not trigger another log
124+ processor . send ( :log_connection_result , true )
125+ end
126+
127+ it "works gracefully when no diagnostic_accumulator is provided" do
128+ processor = subject . new ( "sdk_key" , config , nil )
129+
130+ expect {
131+ processor . send ( :log_connection_started )
132+ processor . send ( :log_connection_result , true )
133+ } . not_to raise_error
134+ end
135+ end
70136 end
71137end
0 commit comments