@@ -310,6 +310,81 @@ def initialize(type, data = nil)
310310 expect ( update . change_set . changes [ 0 ] . action ) . to eq ( LaunchDarkly ::Interfaces ::DataSystem ::ChangeType ::DELETE )
311311 end
312312 end
313+
314+ describe 'diagnostic event recording' do
315+ let ( :synchronizer ) { StreamingDataSource . new ( sdk_key , config ) }
316+
317+ it "logs successful connection when diagnostic_accumulator is provided" do
318+ diagnostic_accumulator = double ( "DiagnosticAccumulator" )
319+ expect ( diagnostic_accumulator ) . to receive ( :record_stream_init ) . with (
320+ kind_of ( Integer ) ,
321+ false ,
322+ kind_of ( Integer )
323+ )
324+
325+ synchronizer . set_diagnostic_accumulator ( diagnostic_accumulator )
326+ synchronizer . send ( :log_connection_started )
327+ synchronizer . send ( :log_connection_result , true )
328+ end
329+
330+ it "logs failed connection when diagnostic_accumulator is provided" do
331+ diagnostic_accumulator = double ( "DiagnosticAccumulator" )
332+ expect ( diagnostic_accumulator ) . to receive ( :record_stream_init ) . with (
333+ kind_of ( Integer ) ,
334+ true ,
335+ kind_of ( Integer )
336+ )
337+
338+ synchronizer . set_diagnostic_accumulator ( diagnostic_accumulator )
339+ synchronizer . send ( :log_connection_started )
340+ synchronizer . send ( :log_connection_result , false )
341+ end
342+
343+ it "logs connection metrics with correct timestamp and duration" do
344+ diagnostic_accumulator = double ( "DiagnosticAccumulator" )
345+
346+ synchronizer . set_diagnostic_accumulator ( diagnostic_accumulator )
347+
348+ expect ( diagnostic_accumulator ) . to receive ( :record_stream_init ) do |timestamp , failed , duration |
349+ expect ( timestamp ) . to be_a ( Integer )
350+ expect ( timestamp ) . to be > 0
351+ expect ( failed ) . to eq ( false )
352+ expect ( duration ) . to be_a ( Integer )
353+ expect ( duration ) . to be >= 0
354+ end
355+
356+ synchronizer . send ( :log_connection_started )
357+ sleep ( 0.01 ) # Small delay to ensure measurable duration
358+ synchronizer . send ( :log_connection_result , true )
359+ end
360+
361+ it "only logs once per connection attempt" do
362+ diagnostic_accumulator = double ( "DiagnosticAccumulator" )
363+ expect ( diagnostic_accumulator ) . to receive ( :record_stream_init ) . once
364+
365+ synchronizer . set_diagnostic_accumulator ( diagnostic_accumulator )
366+ synchronizer . send ( :log_connection_started )
367+ synchronizer . send ( :log_connection_result , true )
368+
369+ # Second call should not record again (no new connection_started)
370+ synchronizer . send ( :log_connection_result , true )
371+ end
372+
373+ it "does not log when diagnostic_accumulator is not set" do
374+ # Should not raise an error
375+ expect { synchronizer . send ( :log_connection_started ) } . not_to raise_error
376+ expect { synchronizer . send ( :log_connection_result , true ) } . not_to raise_error
377+ end
378+
379+ it "does not log when connection was not started" do
380+ diagnostic_accumulator = double ( "DiagnosticAccumulator" )
381+ expect ( diagnostic_accumulator ) . not_to receive ( :record_stream_init )
382+
383+ synchronizer . set_diagnostic_accumulator ( diagnostic_accumulator )
384+ # Call log_connection_result without log_connection_started
385+ synchronizer . send ( :log_connection_result , true )
386+ end
387+ end
313388 end
314389 end
315390 end
0 commit comments