@@ -38,6 +38,7 @@ import (
38
38
"github.com/coder/coder/v2/tailnet"
39
39
"github.com/coder/coder/v2/tailnet/tailnettest"
40
40
"github.com/coder/coder/v2/testutil"
41
+ "github.com/coder/quartz"
41
42
)
42
43
43
44
func TestActiveUsers (t * testing.T ) {
@@ -118,21 +119,21 @@ func TestUsers(t *testing.T) {
118
119
for _ , tc := range []struct {
119
120
Name string
120
121
Database func (t * testing.T ) database.Store
121
- Count map [string ]int
122
+ Count map [database. UserStatus ]int
122
123
}{{
123
124
Name : "None" ,
124
125
Database : func (t * testing.T ) database.Store {
125
126
return dbmem .New ()
126
127
},
127
- Count : map [string ]int {},
128
+ Count : map [database. UserStatus ]int {},
128
129
}, {
129
130
Name : "One" ,
130
131
Database : func (t * testing.T ) database.Store {
131
132
db := dbmem .New ()
132
133
dbgen .User (t , db , database.User {Status : database .UserStatusActive })
133
134
return db
134
135
},
135
- Count : map [string ]int {"active" : 1 },
136
+ Count : map [database. UserStatus ]int {database . UserStatusActive : 1 },
136
137
}, {
137
138
Name : "MultipleStatuses" ,
138
139
Database : func (t * testing.T ) database.Store {
@@ -143,7 +144,7 @@ func TestUsers(t *testing.T) {
143
144
144
145
return db
145
146
},
146
- Count : map [string ]int {"active" : 1 , "dormant" : 1 },
147
+ Count : map [database. UserStatus ]int {database . UserStatusActive : 1 , database . UserStatusDormant : 1 },
147
148
}, {
148
149
Name : "MultipleActive" ,
149
150
Database : func (t * testing.T ) database.Store {
@@ -153,17 +154,25 @@ func TestUsers(t *testing.T) {
153
154
dbgen .User (t , db , database.User {Status : database .UserStatusActive })
154
155
return db
155
156
},
156
- Count : map [string ]int {"active" : 3 },
157
+ Count : map [database. UserStatus ]int {database . UserStatusActive : 3 },
157
158
}} {
158
159
tc := tc
159
160
t .Run (tc .Name , func (t * testing.T ) {
160
161
t .Parallel ()
162
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
163
+ defer cancel ()
164
+
161
165
registry := prometheus .NewRegistry ()
162
- closeFunc , err := prometheusmetrics .Users (context .Background (), slogtest .Make (t , nil ), registry , tc .Database (t ), time .Millisecond )
166
+ mClock := quartz .NewMock (t )
167
+ db := tc .Database (t )
168
+ closeFunc , err := prometheusmetrics .Users (context .Background (), slogtest .Make (t , nil ), mClock , registry , db , time .Millisecond )
163
169
require .NoError (t , err )
164
170
t .Cleanup (closeFunc )
165
171
166
- require .Eventually (t , func () bool {
172
+ _ , w := mClock .AdvanceNext ()
173
+ w .MustWait (ctx )
174
+
175
+ checkFn := func () bool {
167
176
metrics , err := registry .Gather ()
168
177
if err != nil {
169
178
return false
@@ -176,13 +185,24 @@ func TestUsers(t *testing.T) {
176
185
}
177
186
178
187
for _ , metric := range metrics [0 ].Metric {
179
- if tc .Count [* metric .Label [0 ].Value ] != int (metric .Gauge .GetValue ()) {
188
+ if tc .Count [database . UserStatus ( * metric .Label [0 ].Value ) ] != int (metric .Gauge .GetValue ()) {
180
189
return false
181
190
}
182
191
}
183
192
184
193
return true
185
- }, testutil .WaitShort , testutil .IntervalSlow )
194
+ }
195
+
196
+ require .Eventually (t , checkFn , testutil .WaitShort , testutil .IntervalFast )
197
+
198
+ // Add another dormant user and ensure it updates
199
+ dbgen .User (t , db , database.User {Status : database .UserStatusDormant })
200
+ tc .Count [database .UserStatusDormant ]++
201
+
202
+ _ , w = mClock .AdvanceNext ()
203
+ w .MustWait (ctx )
204
+
205
+ require .Eventually (t , checkFn , testutil .WaitShort , testutil .IntervalFast )
186
206
})
187
207
}
188
208
}
0 commit comments