Skip to content

Commit

Permalink
Fix TestProvisioningFactoryReset when running with libfaketime
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 authored and Daniel Adam committed Sep 12, 2024
1 parent 5369297 commit bd44498
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 22 deletions.
7 changes: 4 additions & 3 deletions cloud2cloud-connector/service/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/hub/v2/cloud2cloud-connector/store"
pbIS "github.com/plgd-dev/hub/v2/identity-store/pb"
"github.com/plgd-dev/hub/v2/internal/math"
"github.com/plgd-dev/hub/v2/pkg/log"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
pkgHttpUri "github.com/plgd-dev/hub/v2/pkg/net/http/uri"
Expand Down Expand Up @@ -119,7 +120,7 @@ func publishDeviceResources(ctx context.Context, raClient raService.ResourceAggr
link.Href = href
err := publishResource(ctx, raClient, link, &commands.CommandMetadata{
ConnectionId: linkedAccount.ID,
Sequence: uint64(time.Now().UnixNano()),
Sequence: math.CastTo[uint64](time.Now().UnixNano()),
})
if err != nil {
errors = multierror.Append(errors, fmt.Errorf("cannot publish resource %+v: %w", link, err))
Expand Down Expand Up @@ -234,7 +235,7 @@ func (p *pullDevicesHandler) pullDevices(ctx context.Context, linkedAccount stor
},
CommandMetadata: &commands.CommandMetadata{
ConnectionId: linkedAccount.ID,
Sequence: uint64(time.Now().UnixNano()),
Sequence: math.CastTo[uint64](time.Now().UnixNano()),
},
})
if err != nil {
Expand Down Expand Up @@ -303,7 +304,7 @@ func (p *pullDevicesHandler) notifyResourceChanged(ctx context.Context, linkedAc
ResourceId: commands.NewResourceID(deviceID, pkgHttpUri.CanonicalHref(link.Href)),
CommandMetadata: &commands.CommandMetadata{
ConnectionId: linkedAccount.ID,
Sequence: uint64(time.Now().UnixNano()),
Sequence: math.CastTo[uint64](time.Now().UnixNano()),
},
Content: &commands.Content{
Data: body,
Expand Down
3 changes: 2 additions & 1 deletion cloud2cloud-connector/service/retrieveResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/plgd-dev/hub/v2/cloud2cloud-connector/events"
"github.com/plgd-dev/hub/v2/cloud2cloud-connector/store"
"github.com/plgd-dev/hub/v2/internal/math"
"github.com/plgd-dev/hub/v2/pkg/log"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
Expand Down Expand Up @@ -66,7 +67,7 @@ func retrieveResource(ctx context.Context, traceProvider trace.TracerProvider, r
CorrelationId: e.GetAuditContext().GetCorrelationId(),
CommandMetadata: &commands.CommandMetadata{
ConnectionId: linkedAccount.ID,
Sequence: uint64(time.Now().UnixNano()),
Sequence: math.CastTo[uint64](time.Now().UnixNano()),
},
Content: &commands.Content{
Data: content,
Expand Down
3 changes: 2 additions & 1 deletion cloud2cloud-connector/service/updateResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/plgd-dev/hub/v2/cloud2cloud-connector/events"
"github.com/plgd-dev/hub/v2/cloud2cloud-connector/store"
"github.com/plgd-dev/hub/v2/internal/math"
"github.com/plgd-dev/hub/v2/pkg/log"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
pkgHttpUri "github.com/plgd-dev/hub/v2/pkg/net/http/uri"
Expand Down Expand Up @@ -88,7 +89,7 @@ func updateResource(ctx context.Context, tracerProvider trace.TracerProvider, ra
CorrelationId: e.GetAuditContext().GetCorrelationId(),
CommandMetadata: &commands.CommandMetadata{
ConnectionId: linkedAccount.ID,
Sequence: uint64(time.Now().UnixNano()),
Sequence: math.CastTo[uint64](time.Now().UnixNano()),
},
Content: &commands.Content{
Data: content,
Expand Down
3 changes: 2 additions & 1 deletion cloud2cloud-gateway/store/mongodb/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"

"github.com/plgd-dev/hub/v2/internal/math"
pkgMongo "github.com/plgd-dev/hub/v2/pkg/mongodb"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -51,5 +52,5 @@ func incrementSubscriptionSequenceNumber(ctx context.Context, col *mongo.Collect
return 0, fmt.Errorf("cannot increment sequence number for %v: '%v' not found", subscriptionID, sequenceNumberKey)
}

return uint64(value.(int64)) - 1, nil
return math.CastTo[uint64](value.(int64)) - 1, nil
}
3 changes: 2 additions & 1 deletion device-provisioning-service/service/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ func TestProvisioningFactoryReset(t *testing.T) {

deviceID, _ = test.OnboardDpsSim(ctx, t, c, deviceID, dpcCfg.APIs.COAP.Addr, test.TestDevsimResources)

devClient, err := sdk.NewClient(sdk.WithID(events.OwnerToUUID(test.DPSOwner)))
// extended validity is needed when running tests with libfaketime
devClient, err := sdk.NewClient(sdk.WithID(events.OwnerToUUID(test.DPSOwner)), sdk.WithValidity("2000-01-01T12:00:00Z", "876000h"))
require.NoError(t, err)
defer func() {
_ = devClient.Close(ctx)
Expand Down
3 changes: 2 additions & 1 deletion resource-aggregate/cqrs/aggregate/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"github.com/plgd-dev/hub/v2/internal/math"
"github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventstore"
)

Expand Down Expand Up @@ -201,7 +202,7 @@ func (a *Aggregate) HandleCommandWithAggregateModelWrapper(ctx context.Context,
if len(newEvents) == 0 {
return nil, false, nil
}
snapshot, ok := amodel.TakeSnapshot(newVersion + uint64(len(newEvents)-1))
snapshot, ok := amodel.TakeSnapshot(newVersion + math.CastTo[uint64](len(newEvents)-1))
if !ok {
return nil, false, errors.New("cannot take snapshot")
}
Expand Down
3 changes: 2 additions & 1 deletion resource-aggregate/cqrs/eventstore/cqldb/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gocql/gocql"
"github.com/google/uuid"
"github.com/hashicorp/go-multierror"
"github.com/plgd-dev/hub/v2/internal/math"
"github.com/plgd-dev/hub/v2/pkg/cqldb"
pkgTime "github.com/plgd-dev/hub/v2/pkg/time"
"github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventstore"
Expand Down Expand Up @@ -83,7 +84,7 @@ func (i *iterator) Next(ctx context.Context) (eventstore.EventUnmarshaler, bool)
return nil, false
}
return eventstore.NewLoadedEvent(
uint64(version),
math.CastTo[uint64](version),
eventType,
aggregateID.String(),
groupID.String(),
Expand Down
17 changes: 9 additions & 8 deletions resource-aggregate/cqrs/eventstore/mongodb/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/google/uuid"
"github.com/hashicorp/go-multierror"
"github.com/plgd-dev/hub/v2/internal/math"
pkgTime "github.com/plgd-dev/hub/v2/pkg/time"
"github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventstore"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (i *iterator) parseDocument() bool {
return true
}

func (i *iterator) getEvent() (bson.M, int64, error) {
func (i *iterator) getEvent() (bson.M, uint64, error) {
ev, ok := i.events[i.idx].(bson.M)
if !ok {
return nil, 0, fmt.Errorf("invalid data, event %v is not a BSON document", i.idx)
Expand All @@ -79,12 +80,12 @@ func (i *iterator) getEvent() (bson.M, int64, error) {
if !ok {
return nil, 0, fmt.Errorf("invalid data, '%v' of event %v is not an int64", versionKey, i.idx)
}
return ev, version, nil
return ev, math.CastTo[uint64](version), nil
}

func (i *iterator) nextEvent(ctx context.Context) (bson.M, int64) {
func (i *iterator) nextEvent(ctx context.Context) (bson.M, uint64) {
var ev bson.M
var version int64
var version uint64
var ok bool
for {
if i.idx >= len(i.events) {
Expand Down Expand Up @@ -144,7 +145,7 @@ func (i *iterator) Next(ctx context.Context) (eventstore.EventUnmarshaler, bool)
}
i.idx++
return eventstore.NewLoadedEvent(
uint64(version),
version,
eventType,
i.aggregateID,
i.groupID,
Expand Down Expand Up @@ -185,16 +186,16 @@ func (r *queryResolver) set(query eventstore.VersionQuery) error {
return nil
}

func (r *queryResolver) check(aggregateID string, version int64) bool {
func (r *queryResolver) check(aggregateID string, version uint64) bool {
v, ok := r.aggregateVersions[aggregateID]
if !ok {
return false
}
switch r.op {
case signOperator_lt:
return uint64(version) < v
return version < v
case signOperator_gte:
return uint64(version) >= v
return version >= v
}
return false
}
Expand Down
3 changes: 2 additions & 1 deletion resource-aggregate/cqrs/eventstore/mongodb/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"github.com/plgd-dev/hub/v2/internal/math"
"github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventstore/maintenance"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -86,7 +87,7 @@ func (i *dbAggregateVersionIterator) Next(ctx context.Context, task *maintenance
task.GroupID = dbRecord[groupIDKey].(string)
task.AggregateID = dbRecord[aggregateIDKey].(string)
version := dbRecord[versionKey].(int64)
task.Version = uint64(version)
task.Version = math.CastTo[uint64](version)
return true
}

Expand Down
6 changes: 3 additions & 3 deletions resource-aggregate/cqrs/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/golang/snappy"
"github.com/google/uuid"
isEvents "github.com/plgd-dev/hub/v2/identity-store/events"
"github.com/plgd-dev/hub/v2/internal/math"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
)

Expand Down Expand Up @@ -79,9 +80,8 @@ func GetSubjectHrefID(href string) string {
}

func TimeNowMs() uint64 {
now := time.Now()
unix := now.UnixNano()
return uint64(unix / int64(time.Millisecond))
unix := time.Now().UnixNano()
return math.CastTo[uint64](unix / int64(time.Millisecond))
}

type ProtobufMarshaler interface {
Expand Down
2 changes: 1 addition & 1 deletion tools/mongodb/standby-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ func getValue(v primitive.M, keys ...string) (interface{}, bool) {
if !ok {
return nil, false
}
return getValue(sub, keys[1:]...) //nolint:gosec
return getValue(sub, keys[1:]...)
}

func (app *App) getStatus(ctx context.Context, client *mongo.Client) (primitive.M, error) {
Expand Down

0 comments on commit bd44498

Please sign in to comment.