@@ -387,6 +387,88 @@ func TestConnectionInactiveTimeout(t *testing.T) {
387387 closeSocket (closeWS )
388388}
389389
390+ // TestTACSDurationMetrics tests that the TACSSessionCallDurationName and TACSDisconnectedDuration and metrics are
391+ // emitted when they should be.
392+ func TestTACSDurationMetrics (t * testing.T ) {
393+ ctrl := gomock .NewController (t )
394+ defer ctrl .Finish ()
395+
396+ numTimesConnectToTACS := 2
397+ mockMetricsFactory := mock_metrics .NewMockEntryFactory (ctrl )
398+
399+ // TACSSessionCallDuration is emitted on all connections to TACS.
400+ mockDurationEntry := mock_metrics .NewMockEntry (ctrl )
401+ mockDurationEntry .EXPECT ().WithGauge (gomock .Any ()).Return (mockDurationEntry ).Times (numTimesConnectToTACS )
402+ mockDurationEntry .EXPECT ().Done (gomock .Any ()).Times (numTimesConnectToTACS )
403+ mockMetricsFactory .EXPECT ().New (metrics .TACSSessionCallDurationName ).Return (mockDurationEntry ).
404+ Times (numTimesConnectToTACS )
405+
406+ // TACSDisconnectedDuration should be emitted upon reconnection to TACS only (i.e., all connections to TACS except
407+ // the very first one).
408+ mockDisconnectedEntry := mock_metrics .NewMockEntry (ctrl )
409+ mockDisconnectedEntry .EXPECT ().WithGauge (gomock .Any ()).Return (mockDisconnectedEntry ).Times (numTimesConnectToTACS - 1 )
410+ mockDisconnectedEntry .EXPECT ().Done (gomock .Any ()).Times (numTimesConnectToTACS - 1 )
411+ mockMetricsFactory .EXPECT ().New (metrics .TACSDisconnectedDurationName ).Return (mockDisconnectedEntry ).
412+ Times (numTimesConnectToTACS - 1 )
413+
414+ // Start test server.
415+ closeWS := make (chan []byte )
416+ server , _ , requestChan , _ , err := wsmock .GetMockServer (closeWS )
417+ if err != nil {
418+ t .Fatal (err )
419+ }
420+ server .StartTLS ()
421+ defer server .Close ()
422+
423+ go func () {
424+ for {
425+ select {
426+ case <- requestChan :
427+ }
428+ }
429+ }()
430+
431+ testecsclient := & wsmock.TestECSClient {
432+ TCSurl : server .URL ,
433+ }
434+
435+ ctx , cancel := context .WithCancel (context .Background ())
436+ defer cancel ()
437+
438+ deregisterInstanceEventStream := eventstream .NewEventStream ("Deregister_Instance" , ctx )
439+ deregisterInstanceEventStream .StartListening ()
440+
441+ telemetryMessages := make (chan ecstcs.TelemetryMessage , testTelemetryChannelDefaultBufferSize )
442+ healthMessages := make (chan ecstcs.HealthMessage , testTelemetryChannelDefaultBufferSize )
443+ instanceStatusMessages := make (chan ecstcs.InstanceStatusMessage , testTelemetryChannelDefaultBufferSize )
444+
445+ session := NewTelemetrySession (
446+ testInstanceArn ,
447+ testClusterArn ,
448+ testAgentVersion ,
449+ testAgentHash ,
450+ testContainerRuntimeVersion ,
451+ false ,
452+ aws .NewCredentialsCache (testCreds ),
453+ testCfg ,
454+ deregisterInstanceEventStream ,
455+ testHeartbeatTimeout ,
456+ testHeartbeatJitter ,
457+ testDisconnectionTimeout ,
458+ testDisconnectionJitter ,
459+ mockMetricsFactory , // use the mock metrics factory
460+ telemetryMessages ,
461+ healthMessages ,
462+ instanceStatusMessages ,
463+ emptyDoctor ,
464+ testecsclient ,
465+ )
466+
467+ for i := 0 ; i < numTimesConnectToTACS ; i ++ {
468+ session .StartTelemetrySession (ctx )
469+ }
470+ }
471+
390472// TestTACSConnectionFailureMetric tests that the TACSConnectionFailure metric is recorded when there's a connection error
391473func TestTACSConnectionFailureMetric (t * testing.T ) {
392474 ctrl := gomock .NewController (t )
0 commit comments