Skip to content

Commit f7bc347

Browse files
committed
add tests
1 parent daeac58 commit f7bc347

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

components/usage/cmd/run.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/gitpod-io/gitpod/common-go/log"
1010
"github.com/gitpod-io/gitpod/usage/pkg/controller"
1111
"github.com/spf13/cobra"
12+
"time"
1213
)
1314

1415
func init() {
@@ -27,12 +28,15 @@ func run() *cobra.Command {
2728
Run: func(cmd *cobra.Command, args []string) {
2829
log.Init(ServiceName, Version, true, verbose)
2930

30-
ctrl, err := controller.New("@every 1m", controller.ReconcilerFunc(controller.HelloWorldReconciler))
31+
ctrl, err := controller.New(1*time.Minute, controller.ReconcilerFunc(controller.HelloWorldReconciler))
3132
if err != nil {
3233
log.WithError(err).Fatal("Failed to initialize usage controller.")
3334
}
3435

35-
ctrl.Start()
36+
err = ctrl.Start()
37+
if err != nil {
38+
log.WithError(err).Fatal("Failed to start usage controller.")
39+
}
3640
defer ctrl.Stop()
3741

3842
srv, err := baseserver.New("usage")

components/usage/pkg/controller/controller.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,27 @@ import (
1212
"time"
1313
)
1414

15-
func New(schedule string, reconciler Reconciler) (*Controller, error) {
16-
sched, err := cron.Parse(schedule)
17-
if err != nil {
18-
return nil, fmt.Errorf("failed to parse cron schedule: %w", err)
19-
}
20-
15+
func New(schedule time.Duration, reconciler Reconciler) (*Controller, error) {
2116
return &Controller{
22-
schedule: sched,
17+
schedule: schedule,
2318
reconciler: reconciler,
2419
scheduler: cron.NewWithLocation(time.UTC),
2520
}, nil
2621
}
2722

2823
type Controller struct {
29-
schedule cron.Schedule
24+
schedule time.Duration
3025
reconciler Reconciler
3126

3227
scheduler *cron.Cron
3328

3429
runningJobs sync.WaitGroup
3530
}
3631

37-
func (c *Controller) Start() {
32+
func (c *Controller) Start() error {
3833
log.Info("Starting usage controller.")
3934

40-
c.scheduler.Schedule(c.schedule, cron.FuncJob(func() {
35+
err := c.scheduler.AddFunc(fmt.Sprintf("@every %s", c.schedule.String()), cron.FuncJob(func() {
4136
log.Info("Starting usage reconciliation.")
4237

4338
c.runningJobs.Add(1)
@@ -50,8 +45,13 @@ func (c *Controller) Start() {
5045
log.Info("Completed usage reconciliation run without errors.")
5146
}
5247
}))
48+
if err != nil {
49+
return fmt.Errorf("failed to add function to scheduler: %w", err)
50+
}
5351

5452
c.scheduler.Start()
53+
54+
return nil
5555
}
5656

5757
// Stop terminates the Controller and awaits for all running jobs to complete.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package controller
6+
7+
import (
8+
"github.com/stretchr/testify/require"
9+
"testing"
10+
"time"
11+
)
12+
13+
func TestController(t *testing.T) {
14+
schedule := time.Second
15+
triggered := false
16+
17+
ctrl, err := New(schedule, ReconcilerFunc(func() error {
18+
triggered = true
19+
return nil
20+
}))
21+
require.NoError(t, err)
22+
23+
require.NoError(t, ctrl.Start())
24+
time.Sleep(schedule + 20*time.Millisecond)
25+
require.True(t, triggered, "must trigger reconciler function")
26+
ctrl.Stop()
27+
}
28+
29+
func TestController_GracefullyHandlesPanic(t *testing.T) {
30+
ctrl, err := New(20*time.Millisecond, ReconcilerFunc(func() error {
31+
panic("pls help")
32+
}))
33+
require.NoError(t, err)
34+
35+
require.NoError(t, ctrl.Start())
36+
ctrl.Stop()
37+
}

0 commit comments

Comments
 (0)