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