@@ -387,6 +387,90 @@ 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+ tacsSession := telemetrySession {
446+ containerInstanceArn : testInstanceArn ,
447+ cluster : testClusterArn ,
448+ agentVersion : testAgentVersion ,
449+ agentHash : testAgentHash ,
450+ containerRuntimeVersion : testContainerRuntimeVersion ,
451+ disableMetrics : false ,
452+ credentialsCache : aws .NewCredentialsCache (testCreds ),
453+ cfg : testCfg ,
454+ deregisterInstanceEventStream : deregisterInstanceEventStream ,
455+ heartbeatTimeout : 50 * time .Millisecond , // use smaller value than testHeartbeatTimeout
456+ heartbeatJitterMax : 10 * time .Millisecond , // use smaller value than testHeartbeatJitter
457+ disconnectTimeout : testDisconnectionTimeout ,
458+ disconnectJitterMax : testDisconnectionJitter ,
459+ metricsFactory : mockMetricsFactory , // use the mock metrics factory
460+ metricsChannel : telemetryMessages ,
461+ healthChannel : healthMessages ,
462+ instanceStatusChannel : instanceStatusMessages ,
463+ doctor : emptyDoctor ,
464+ ecsClient : testecsclient ,
465+ lastDisconnectedTime : time.Time {},
466+ }
467+
468+ for i := 0 ; i < numTimesConnectToTACS ; i ++ {
469+ tacsSession .StartTelemetrySession (ctx )
470+ tacsSession .lastDisconnectedTime = time .Now ()
471+ }
472+ }
473+
390474// TestTACSConnectionFailureMetric tests that the TACSConnectionFailure metric is recorded when there's a connection error
391475func TestTACSConnectionFailureMetric (t * testing.T ) {
392476 ctrl := gomock .NewController (t )
0 commit comments