@@ -2362,7 +2362,7 @@ func TestUpsertConnectionLog(t *testing.T) {
2362
2362
require .Equal (t , log , rows [0 ].ConnectionLog )
2363
2363
})
2364
2364
2365
- t .Run ("NoConnect " , func (t * testing.T ) {
2365
+ t .Run ("DisconnectThenConnect " , func (t * testing.T ) {
2366
2366
t .Parallel ()
2367
2367
2368
2368
db , _ := dbtestutil .NewDB (t )
@@ -2375,7 +2375,7 @@ func TestUpsertConnectionLog(t *testing.T) {
2375
2375
2376
2376
// Insert just a 'disconect' event
2377
2377
disconnectTime := dbtime .Now ()
2378
- connectParams := database.UpsertConnectionLogParams {
2378
+ disconnectParams := database.UpsertConnectionLogParams {
2379
2379
ID : uuid .New (),
2380
2380
Time : disconnectTime ,
2381
2381
OrganizationID : ws .OrganizationID ,
@@ -2386,20 +2386,60 @@ func TestUpsertConnectionLog(t *testing.T) {
2386
2386
Type : database .ConnectionTypeSsh ,
2387
2387
ConnectionID : uuid.NullUUID {UUID : connectionID , Valid : true },
2388
2388
ConnectionStatus : database .ConnectionStatusDisconnected ,
2389
+ CloseReason : sql.NullString {String : "server shutting down" , Valid : true },
2389
2390
}
2390
2391
2391
- _ , err := db .UpsertConnectionLog (ctx , connectParams )
2392
+ _ , err := db .UpsertConnectionLog (ctx , disconnectParams )
2392
2393
require .NoError (t , err )
2393
2394
2394
- rows , err := db .GetConnectionLogsOffset (ctx , database.GetConnectionLogsOffsetParams {})
2395
+ firstRows , err := db .GetConnectionLogsOffset (ctx , database.GetConnectionLogsOffsetParams {})
2395
2396
require .NoError (t , err )
2396
- require .Len (t , rows , 1 )
2397
+ require .Len (t , firstRows , 1 )
2397
2398
2398
2399
// We expect the connection event to be marked as closed with the start
2399
2400
// and close time being the same.
2400
- require .True (t , rows [0 ].ConnectionLog .CloseTime .Valid )
2401
- require .Equal (t , disconnectTime , rows [0 ].ConnectionLog .CloseTime .Time .UTC ())
2402
- require .Equal (t , rows [0 ].ConnectionLog .Time .UTC (), rows [0 ].ConnectionLog .CloseTime .Time .UTC ())
2401
+ require .True (t , firstRows [0 ].ConnectionLog .CloseTime .Valid )
2402
+ require .Equal (t , disconnectTime , firstRows [0 ].ConnectionLog .CloseTime .Time .UTC ())
2403
+ require .Equal (t , firstRows [0 ].ConnectionLog .Time .UTC (), firstRows [0 ].ConnectionLog .CloseTime .Time .UTC ())
2404
+
2405
+ // Now insert a 'connect' event for the same connection.
2406
+ // This should be a no op
2407
+ connectTime := disconnectTime .Add (time .Second )
2408
+ connectParams := database.UpsertConnectionLogParams {
2409
+ ID : uuid .New (),
2410
+ Time : connectTime ,
2411
+ OrganizationID : ws .OrganizationID ,
2412
+ WorkspaceOwnerID : ws .OwnerID ,
2413
+ WorkspaceID : ws .ID ,
2414
+ WorkspaceName : ws .Name ,
2415
+ AgentName : agentName ,
2416
+ Type : database .ConnectionTypeSsh ,
2417
+ ConnectionID : uuid.NullUUID {UUID : connectionID , Valid : true },
2418
+ ConnectionStatus : database .ConnectionStatusConnected ,
2419
+ CloseReason : sql.NullString {String : "reconnected" , Valid : true },
2420
+ Code : sql.NullInt32 {Int32 : 0 , Valid : false },
2421
+ }
2422
+
2423
+ _ , err = db .UpsertConnectionLog (ctx , connectParams )
2424
+ require .NoError (t , err )
2425
+
2426
+ secondRows , err := db .GetConnectionLogsOffset (ctx , database.GetConnectionLogsOffsetParams {})
2427
+ require .NoError (t , err )
2428
+ require .Len (t , secondRows , 1 )
2429
+ require .Equal (t , firstRows , secondRows )
2430
+
2431
+ // Upsert a disconnection, which should also be a no op
2432
+ disconnectParams .CloseReason = sql.NullString {
2433
+ String : "updated close reason" ,
2434
+ Valid : true ,
2435
+ }
2436
+ _ , err = db .UpsertConnectionLog (ctx , disconnectParams )
2437
+ require .NoError (t , err )
2438
+ thirdRows , err := db .GetConnectionLogsOffset (ctx , database.GetConnectionLogsOffsetParams {})
2439
+ require .NoError (t , err )
2440
+ require .Len (t , secondRows , 1 )
2441
+ // The close reason shouldn't be updated
2442
+ require .Equal (t , secondRows , thirdRows )
2403
2443
})
2404
2444
}
2405
2445
0 commit comments