This test performs a more 'integration-style' test with multiple instances.
(t *testing.T)
| 109 | |
| 110 | // This test performs a more 'integration-style' test with multiple instances. |
| 111 | func TestTracker_MultipleInstances(t *testing.T) { |
| 112 | t.Parallel() |
| 113 | |
| 114 | // Given we have two coderd instances connected to the same database |
| 115 | var ( |
| 116 | ctx = testutil.Context(t, testutil.WaitLong) |
| 117 | db, _ = dbtestutil.NewDB(t) |
| 118 | // real pubsub is not safe for concurrent use, and this test currently |
| 119 | // does not depend on pubsub |
| 120 | ps = pubsub.NewInMemory() |
| 121 | wuTickA = make(chan time.Time) |
| 122 | wuFlushA = make(chan int, 1) |
| 123 | wuTickB = make(chan time.Time) |
| 124 | wuFlushB = make(chan int, 1) |
| 125 | clientA = coderdtest.New(t, &coderdtest.Options{ |
| 126 | WorkspaceUsageTrackerTick: wuTickA, |
| 127 | WorkspaceUsageTrackerFlush: wuFlushA, |
| 128 | Database: db, |
| 129 | Pubsub: ps, |
| 130 | }) |
| 131 | clientB = coderdtest.New(t, &coderdtest.Options{ |
| 132 | WorkspaceUsageTrackerTick: wuTickB, |
| 133 | WorkspaceUsageTrackerFlush: wuFlushB, |
| 134 | Database: db, |
| 135 | Pubsub: ps, |
| 136 | }) |
| 137 | owner = coderdtest.CreateFirstUser(t, clientA) |
| 138 | now = dbtime.Now() |
| 139 | ) |
| 140 | |
| 141 | clientB.SetSessionToken(clientA.SessionToken()) |
| 142 | |
| 143 | // Create a number of workspaces |
| 144 | numWorkspaces := 10 |
| 145 | w := make([]dbfake.WorkspaceResponse, numWorkspaces) |
| 146 | for i := 0; i < numWorkspaces; i++ { |
| 147 | wr := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ |
| 148 | OwnerID: owner.UserID, |
| 149 | OrganizationID: owner.OrganizationID, |
| 150 | LastUsedAt: now, |
| 151 | }).WithAgent().Do() |
| 152 | w[i] = wr |
| 153 | } |
| 154 | |
| 155 | // Use client A to update LastUsedAt of the first three |
| 156 | require.NoError(t, clientA.PostWorkspaceUsage(ctx, w[0].Workspace.ID)) |
| 157 | require.NoError(t, clientA.PostWorkspaceUsage(ctx, w[1].Workspace.ID)) |
| 158 | require.NoError(t, clientA.PostWorkspaceUsage(ctx, w[2].Workspace.ID)) |
| 159 | // Use client B to update LastUsedAt of the next three |
| 160 | require.NoError(t, clientB.PostWorkspaceUsage(ctx, w[3].Workspace.ID)) |
| 161 | require.NoError(t, clientB.PostWorkspaceUsage(ctx, w[4].Workspace.ID)) |
| 162 | require.NoError(t, clientB.PostWorkspaceUsage(ctx, w[5].Workspace.ID)) |
| 163 | // The next two will have updated from both instances |
| 164 | require.NoError(t, clientA.PostWorkspaceUsage(ctx, w[6].Workspace.ID)) |
| 165 | require.NoError(t, clientB.PostWorkspaceUsage(ctx, w[6].Workspace.ID)) |
| 166 | require.NoError(t, clientA.PostWorkspaceUsage(ctx, w[7].Workspace.ID)) |
| 167 | require.NoError(t, clientB.PostWorkspaceUsage(ctx, w[7].Workspace.ID)) |
| 168 | // The last two will not report any usage. |
nothing calls this directly
no test coverage detected