Skip to content

Commit

Permalink
Move Clock into ports
Browse files Browse the repository at this point in the history
  • Loading branch information
yitsushi committed Oct 8, 2021
1 parent 08e73e6 commit 0c68217
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
9 changes: 0 additions & 9 deletions core/application/app.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package application

import (
"time"

"github.com/weaveworks/reignite/core/ports"
)

Expand All @@ -12,25 +10,18 @@ type App interface {
ports.MicroVMCommandUseCases
ports.MicroVMQueryUseCases
ports.ReconcileMicroVMsUseCase
ports.HasTime
}

func New(cfg *Config, ports *ports.Collection) App {
return &app{
cfg: cfg,
ports: ports,
clock: time.Now,
}
}

type app struct {
cfg *Config
ports *ports.Collection
clock func() time.Time
}

func (a app) SetClock(clock func() time.Time) {
a.clock = clock
}

type Config struct {
Expand Down
6 changes: 3 additions & 3 deletions core/application/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ func TestApp_CreateMicroVM(t *testing.T) {
NetworkService: ns,
ImageService: is,
FileSystem: fs,
Clock: frozenTime,
}

tc.expect(rm.EXPECT(), em.EXPECT(), im.EXPECT(), pm.EXPECT())

ctx := context.Background()
app := application.New(&application.Config{}, ports)
app.SetClock(frozenTime)
_, err := app.CreateMicroVM(ctx, tc.specToCreate)

if tc.expectError {
Expand Down Expand Up @@ -242,13 +242,13 @@ func TestApp_UpdateMicroVM(t *testing.T) {
NetworkService: ns,
ImageService: is,
FileSystem: fs,
Clock: frozenTime,
}

tc.expect(rm.EXPECT(), em.EXPECT(), im.EXPECT(), pm.EXPECT())

ctx := context.Background()
app := application.New(&application.Config{}, ports)
app.SetClock(frozenTime)
_, err := app.UpdateMicroVM(ctx, tc.specToUpdate)

if tc.expectError {
Expand Down Expand Up @@ -354,13 +354,13 @@ func TestApp_DeleteMicroVM(t *testing.T) {
NetworkService: ns,
ImageService: is,
FileSystem: fs,
Clock: frozenTime,
}

tc.expect(rm.EXPECT(), em.EXPECT(), im.EXPECT(), pm.EXPECT())

ctx := context.Background()
app := application.New(&application.Config{}, ports)
app.SetClock(frozenTime)
err := app.DeleteMicroVM(ctx, tc.toDeleteID, tc.toDeleteNS)

if tc.expectError {
Expand Down
7 changes: 4 additions & 3 deletions core/application/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (a *app) CreateMicroVM(ctx context.Context, mvm *models.MicroVM) (*models.M
// TODO: validate the spec

// Set the timestamp when the VMspec was created.
mvm.Spec.CreatedAt = a.clock().UnixMilli()
mvm.Spec.CreatedAt = a.ports.Clock().UnixMilli()

createdMVM, err := a.ports.Repo.Save(ctx, mvm)
if err != nil {
Expand Down Expand Up @@ -90,7 +90,7 @@ func (a *app) UpdateMicroVM(ctx context.Context, mvm *models.MicroVM) (*models.M
// TODO: check if update is valid (i.e. compare existing to requested update)

// Set the timestamp when the VMspec was updated.
mvm.Spec.UpdatedAt = a.clock().UnixMilli()
mvm.Spec.UpdatedAt = a.ports.Clock().UnixMilli()

updatedMVM, err := a.ports.Repo.Save(ctx, mvm)
if err != nil {
Expand Down Expand Up @@ -123,7 +123,8 @@ func (a *app) DeleteMicroVM(ctx context.Context, id, namespace string) error {
return fmt.Errorf("microvm %s/%s doesn't exist, skipping delete", id, namespace)
}

foundMvm.Spec.DeletedAt = a.clock().UnixMilli()
// Set the timestamp when the VMspec was deleted.
foundMvm.Spec.DeletedAt = a.ports.Clock().UnixMilli()

_, err = a.ports.Repo.Save(ctx, foundMvm)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion core/ports/collection.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package ports

import "github.com/spf13/afero"
import (
"time"

"github.com/spf13/afero"
)

type Collection struct {
Repo MicroVMRepository
Expand All @@ -10,4 +14,5 @@ type Collection struct {
NetworkService NetworkService
ImageService ImageService
FileSystem afero.Fs
Clock func() time.Time
}
2 changes: 2 additions & 0 deletions internal/inject/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package inject

import (
"fmt"
"time"

"github.com/google/wire"
"github.com/spf13/afero"
Expand Down Expand Up @@ -93,6 +94,7 @@ func appPorts(repo ports.MicroVMRepository, prov ports.MicroVMService, es ports.
NetworkService: ns,
ImageService: ims,
FileSystem: fs,
Clock: time.Now,
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/inject/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0c68217

Please sign in to comment.