(t *testing.T)
| 4775 | } |
| 4776 | |
| 4777 | func TestGetWorkspaceConn_StatusCheck(t *testing.T) { |
| 4778 | // The cache-hit status check re-fetches the agent row for a fresh |
| 4779 | // heartbeat timestamp. Healthy, timed-out, and DB-error paths return |
| 4780 | // the cached connection. Disconnected agents are covered separately |
| 4781 | // because they now trigger a fresh dial before recovery. |
| 4782 | t.Parallel() |
| 4783 | |
| 4784 | type testCase struct { |
| 4785 | name string |
| 4786 | buildAgent func(now time.Time) database.WorkspaceAgent |
| 4787 | dbError bool |
| 4788 | } |
| 4789 | |
| 4790 | tests := []testCase{ |
| 4791 | { |
| 4792 | // Agent never connected and the connection timeout |
| 4793 | // has elapsed. This should not trigger lifecycle |
| 4794 | // recovery because the agent did not connect and |
| 4795 | // then disconnect. |
| 4796 | name: "TimedOutAgentCacheHit", |
| 4797 | buildAgent: func(now time.Time) database.WorkspaceAgent { |
| 4798 | return database.WorkspaceAgent{ |
| 4799 | CreatedAt: now.Add(-10 * time.Minute), |
| 4800 | ConnectionTimeoutSeconds: 60, |
| 4801 | } |
| 4802 | }, |
| 4803 | }, |
| 4804 | { |
| 4805 | name: "CacheHitHealthyAgent", |
| 4806 | buildAgent: func(now time.Time) database.WorkspaceAgent { |
| 4807 | return database.WorkspaceAgent{ |
| 4808 | FirstConnectedAt: sql.NullTime{ |
| 4809 | Time: now.Add(-5 * time.Minute), |
| 4810 | Valid: true, |
| 4811 | }, |
| 4812 | LastConnectedAt: sql.NullTime{ |
| 4813 | Time: now, |
| 4814 | Valid: true, |
| 4815 | }, |
| 4816 | } |
| 4817 | }, |
| 4818 | }, |
| 4819 | { |
| 4820 | // When GetWorkspaceAgentByID returns an error on |
| 4821 | // cache hit, the cached connection should be returned. |
| 4822 | name: "CacheHitDBError", |
| 4823 | buildAgent: func(now time.Time) database.WorkspaceAgent { |
| 4824 | return database.WorkspaceAgent{ |
| 4825 | FirstConnectedAt: sql.NullTime{ |
| 4826 | Time: now.Add(-5 * time.Minute), |
| 4827 | Valid: true, |
| 4828 | }, |
| 4829 | LastConnectedAt: sql.NullTime{ |
| 4830 | Time: now, |
| 4831 | Valid: true, |
| 4832 | }, |
| 4833 | } |
| 4834 | }, |
nothing calls this directly
no test coverage detected