6
6
"context"
7
7
"io"
8
8
"strings"
9
+ "sync/atomic"
9
10
"testing"
10
11
"time"
11
12
@@ -16,17 +17,14 @@ import (
16
17
"github.com/coder/coder/cli/clibase"
17
18
"github.com/coder/coder/cli/clitest"
18
19
"github.com/coder/coder/cli/cliui"
20
+ "github.com/coder/coder/coderd/util/ptr"
19
21
"github.com/coder/coder/codersdk"
20
22
"github.com/coder/coder/testutil"
21
23
)
22
24
23
25
func TestAgent (t * testing.T ) {
24
26
t .Parallel ()
25
27
26
- ptrTime := func (t time.Time ) * time.Time {
27
- return & t
28
- }
29
-
30
28
for _ , tc := range []struct {
31
29
name string
32
30
iter []func (context.Context , * codersdk.WorkspaceAgent , chan []codersdk.WorkspaceAgentStartupLog ) error
@@ -47,7 +45,7 @@ func TestAgent(t *testing.T) {
47
45
},
48
46
func (_ context.Context , agent * codersdk.WorkspaceAgent , logs chan []codersdk.WorkspaceAgentStartupLog ) error {
49
47
agent .Status = codersdk .WorkspaceAgentConnected
50
- agent .FirstConnectedAt = ptrTime (time .Now ())
48
+ agent .FirstConnectedAt = ptr . Ref (time .Now ())
51
49
close (logs )
52
50
return nil
53
51
},
@@ -69,7 +67,7 @@ func TestAgent(t *testing.T) {
69
67
func (_ context.Context , agent * codersdk.WorkspaceAgent , _ chan []codersdk.WorkspaceAgentStartupLog ) error {
70
68
agent .Status = codersdk .WorkspaceAgentConnecting
71
69
agent .LifecycleState = codersdk .WorkspaceAgentLifecycleStarting
72
- agent .StartedAt = ptrTime (time .Now ())
70
+ agent .StartedAt = ptr . Ref (time .Now ())
73
71
return nil
74
72
},
75
73
func (_ context.Context , agent * codersdk.WorkspaceAgent , _ chan []codersdk.WorkspaceAgentStartupLog ) error {
@@ -78,9 +76,9 @@ func TestAgent(t *testing.T) {
78
76
},
79
77
func (_ context.Context , agent * codersdk.WorkspaceAgent , logs chan []codersdk.WorkspaceAgentStartupLog ) error {
80
78
agent .Status = codersdk .WorkspaceAgentConnected
81
- agent .FirstConnectedAt = ptrTime (time .Now ())
79
+ agent .FirstConnectedAt = ptr . Ref (time .Now ())
82
80
agent .LifecycleState = codersdk .WorkspaceAgentLifecycleReady
83
- agent .ReadyAt = ptrTime (time .Now ())
81
+ agent .ReadyAt = ptr . Ref (time .Now ())
84
82
close (logs )
85
83
return nil
86
84
},
@@ -102,17 +100,17 @@ func TestAgent(t *testing.T) {
102
100
iter : []func (context.Context , * codersdk.WorkspaceAgent , chan []codersdk.WorkspaceAgentStartupLog ) error {
103
101
func (_ context.Context , agent * codersdk.WorkspaceAgent , _ chan []codersdk.WorkspaceAgentStartupLog ) error {
104
102
agent .Status = codersdk .WorkspaceAgentDisconnected
105
- agent .FirstConnectedAt = ptrTime (time .Now ().Add (- 1 * time .Minute ))
106
- agent .LastConnectedAt = ptrTime (time .Now ().Add (- 1 * time .Minute ))
107
- agent .DisconnectedAt = ptrTime (time .Now ())
103
+ agent .FirstConnectedAt = ptr . Ref (time .Now ().Add (- 1 * time .Minute ))
104
+ agent .LastConnectedAt = ptr . Ref (time .Now ().Add (- 1 * time .Minute ))
105
+ agent .DisconnectedAt = ptr . Ref (time .Now ())
108
106
agent .LifecycleState = codersdk .WorkspaceAgentLifecycleReady
109
- agent .StartedAt = ptrTime (time .Now ().Add (- 1 * time .Minute ))
110
- agent .ReadyAt = ptrTime (time .Now ())
107
+ agent .StartedAt = ptr . Ref (time .Now ().Add (- 1 * time .Minute ))
108
+ agent .ReadyAt = ptr . Ref (time .Now ())
111
109
return nil
112
110
},
113
111
func (_ context.Context , agent * codersdk.WorkspaceAgent , _ chan []codersdk.WorkspaceAgentStartupLog ) error {
114
112
agent .Status = codersdk .WorkspaceAgentConnected
115
- agent .LastConnectedAt = ptrTime (time .Now ())
113
+ agent .LastConnectedAt = ptr . Ref (time .Now ())
116
114
return nil
117
115
},
118
116
func (_ context.Context , _ * codersdk.WorkspaceAgent , logs chan []codersdk.WorkspaceAgentStartupLog ) error {
@@ -136,9 +134,9 @@ func TestAgent(t *testing.T) {
136
134
iter : []func (context.Context , * codersdk.WorkspaceAgent , chan []codersdk.WorkspaceAgentStartupLog ) error {
137
135
func (_ context.Context , agent * codersdk.WorkspaceAgent , logs chan []codersdk.WorkspaceAgentStartupLog ) error {
138
136
agent .Status = codersdk .WorkspaceAgentConnected
139
- agent .FirstConnectedAt = ptrTime (time .Now ())
137
+ agent .FirstConnectedAt = ptr . Ref (time .Now ())
140
138
agent .LifecycleState = codersdk .WorkspaceAgentLifecycleStarting
141
- agent .StartedAt = ptrTime (time .Now ())
139
+ agent .StartedAt = ptr . Ref (time .Now ())
142
140
logs <- []codersdk.WorkspaceAgentStartupLog {
143
141
{
144
142
CreatedAt : time .Now (),
@@ -149,7 +147,7 @@ func TestAgent(t *testing.T) {
149
147
},
150
148
func (_ context.Context , agent * codersdk.WorkspaceAgent , logs chan []codersdk.WorkspaceAgentStartupLog ) error {
151
149
agent .LifecycleState = codersdk .WorkspaceAgentLifecycleReady
152
- agent .ReadyAt = ptrTime (time .Now ())
150
+ agent .ReadyAt = ptr . Ref (time .Now ())
153
151
logs <- []codersdk.WorkspaceAgentStartupLog {
154
152
{
155
153
CreatedAt : time .Now (),
@@ -176,10 +174,10 @@ func TestAgent(t *testing.T) {
176
174
iter : []func (context.Context , * codersdk.WorkspaceAgent , chan []codersdk.WorkspaceAgentStartupLog ) error {
177
175
func (_ context.Context , agent * codersdk.WorkspaceAgent , logs chan []codersdk.WorkspaceAgentStartupLog ) error {
178
176
agent .Status = codersdk .WorkspaceAgentConnected
179
- agent .FirstConnectedAt = ptrTime (time .Now ())
180
- agent .StartedAt = ptrTime (time .Now ())
177
+ agent .FirstConnectedAt = ptr . Ref (time .Now ())
178
+ agent .StartedAt = ptr . Ref (time .Now ())
181
179
agent .LifecycleState = codersdk .WorkspaceAgentLifecycleStartError
182
- agent .ReadyAt = ptrTime (time .Now ())
180
+ agent .ReadyAt = ptr . Ref (time .Now ())
183
181
logs <- []codersdk.WorkspaceAgentStartupLog {
184
182
{
185
183
CreatedAt : time .Now (),
@@ -222,9 +220,9 @@ func TestAgent(t *testing.T) {
222
220
iter : []func (context.Context , * codersdk.WorkspaceAgent , chan []codersdk.WorkspaceAgentStartupLog ) error {
223
221
func (_ context.Context , agent * codersdk.WorkspaceAgent , logs chan []codersdk.WorkspaceAgentStartupLog ) error {
224
222
agent .Status = codersdk .WorkspaceAgentConnected
225
- agent .FirstConnectedAt = ptrTime (time .Now ())
223
+ agent .FirstConnectedAt = ptr . Ref (time .Now ())
226
224
agent .LifecycleState = codersdk .WorkspaceAgentLifecycleStarting
227
- agent .StartedAt = ptrTime (time .Now ())
225
+ agent .StartedAt = ptr . Ref (time .Now ())
228
226
logs <- []codersdk.WorkspaceAgentStartupLog {
229
227
{
230
228
CreatedAt : time .Now (),
@@ -234,7 +232,7 @@ func TestAgent(t *testing.T) {
234
232
return nil
235
233
},
236
234
func (_ context.Context , agent * codersdk.WorkspaceAgent , logs chan []codersdk.WorkspaceAgentStartupLog ) error {
237
- agent .ReadyAt = ptrTime (time .Now ())
235
+ agent .ReadyAt = ptr . Ref (time .Now ())
238
236
agent .LifecycleState = codersdk .WorkspaceAgentLifecycleShuttingDown
239
237
close (logs )
240
238
return nil
@@ -310,7 +308,7 @@ func TestAgent(t *testing.T) {
310
308
311
309
cmd := & clibase.Cmd {
312
310
Handler : func (inv * clibase.Invocation ) error {
313
- tc .opts .Fetch = func (_ context.Context ) (codersdk.WorkspaceAgent , error ) {
311
+ tc .opts .Fetch = func (_ context.Context , _ uuid. UUID ) (codersdk.WorkspaceAgent , error ) {
314
312
var err error
315
313
if len (tc .iter ) > 0 {
316
314
err = tc .iter [0 ](ctx , & agent , logs )
@@ -321,7 +319,7 @@ func TestAgent(t *testing.T) {
321
319
tc .opts .FetchLogs = func (_ context.Context , _ uuid.UUID , _ int64 , _ bool ) (<- chan []codersdk.WorkspaceAgentStartupLog , io.Closer , error ) {
322
320
return logs , closeFunc (func () error { return nil }), nil
323
321
}
324
- err := cliui .Agent (inv .Context (), & buf , tc .opts )
322
+ err := cliui .Agent (inv .Context (), & buf , uuid . Nil , tc .opts )
325
323
return err
326
324
},
327
325
}
@@ -350,4 +348,37 @@ func TestAgent(t *testing.T) {
350
348
}
351
349
})
352
350
}
351
+
352
+ t .Run ("NotInfinite" , func (t * testing.T ) {
353
+ t .Parallel ()
354
+ var fetchCalled uint64
355
+
356
+ cmd := & clibase.Cmd {
357
+ Handler : func (inv * clibase.Invocation ) error {
358
+ buf := bytes.Buffer {}
359
+ err := cliui .Agent (inv .Context (), & buf , uuid .Nil , cliui.AgentOptions {
360
+ FetchInterval : 10 * time .Millisecond ,
361
+ Fetch : func (ctx context.Context , agentID uuid.UUID ) (codersdk.WorkspaceAgent , error ) {
362
+ atomic .AddUint64 (& fetchCalled , 1 )
363
+
364
+ return codersdk.WorkspaceAgent {
365
+ Status : codersdk .WorkspaceAgentConnected ,
366
+ LifecycleState : codersdk .WorkspaceAgentLifecycleReady ,
367
+ }, nil
368
+ },
369
+ })
370
+ if err != nil {
371
+ return err
372
+ }
373
+
374
+ require .Never (t , func () bool {
375
+ called := atomic .LoadUint64 (& fetchCalled )
376
+ return called > 5 || called == 0
377
+ }, time .Second , 100 * time .Millisecond )
378
+
379
+ return nil
380
+ },
381
+ }
382
+ require .NoError (t , cmd .Invoke ().Run ())
383
+ })
353
384
}
0 commit comments