From 74aa4747b7dae56d5cad52d41b61cd18c3b0ae23 Mon Sep 17 00:00:00 2001 From: Daniel Adam Date: Wed, 11 Sep 2024 18:27:45 +0200 Subject: [PATCH] Fix TestProvisioningFactoryReset when running with libfaketime --- cloud2cloud-connector/service/pull.go | 7 ++++--- .../service/retrieveResource.go | 3 ++- cloud2cloud-connector/service/updateResource.go | 3 ++- cloud2cloud-gateway/store/mongodb/store.go | 3 ++- .../service/provision_test.go | 3 ++- resource-aggregate/cqrs/aggregate/aggregate.go | 3 ++- .../cqrs/eventstore/cqldb/load.go | 3 ++- .../cqrs/eventstore/mongodb/load.go | 17 +++++++++-------- .../cqrs/eventstore/mongodb/maintenance.go | 3 ++- resource-aggregate/cqrs/utils/utils.go | 6 +++--- tools/mongodb/standby-tool/main.go | 2 +- 11 files changed, 31 insertions(+), 22 deletions(-) diff --git a/cloud2cloud-connector/service/pull.go b/cloud2cloud-connector/service/pull.go index 336de558d..64cf2e0e7 100644 --- a/cloud2cloud-connector/service/pull.go +++ b/cloud2cloud-connector/service/pull.go @@ -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" @@ -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)) @@ -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 { @@ -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, diff --git a/cloud2cloud-connector/service/retrieveResource.go b/cloud2cloud-connector/service/retrieveResource.go index a0c0592f8..076ca5a72 100644 --- a/cloud2cloud-connector/service/retrieveResource.go +++ b/cloud2cloud-connector/service/retrieveResource.go @@ -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" @@ -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, diff --git a/cloud2cloud-connector/service/updateResource.go b/cloud2cloud-connector/service/updateResource.go index 4f6aa010f..92144c357 100644 --- a/cloud2cloud-connector/service/updateResource.go +++ b/cloud2cloud-connector/service/updateResource.go @@ -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" @@ -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, diff --git a/cloud2cloud-gateway/store/mongodb/store.go b/cloud2cloud-gateway/store/mongodb/store.go index f7b80bf01..428c14265 100644 --- a/cloud2cloud-gateway/store/mongodb/store.go +++ b/cloud2cloud-gateway/store/mongodb/store.go @@ -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" @@ -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 } diff --git a/device-provisioning-service/service/provision_test.go b/device-provisioning-service/service/provision_test.go index 90e9d5aef..0db930164 100644 --- a/device-provisioning-service/service/provision_test.go +++ b/device-provisioning-service/service/provision_test.go @@ -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) diff --git a/resource-aggregate/cqrs/aggregate/aggregate.go b/resource-aggregate/cqrs/aggregate/aggregate.go index bec81ca0f..b63e37375 100644 --- a/resource-aggregate/cqrs/aggregate/aggregate.go +++ b/resource-aggregate/cqrs/aggregate/aggregate.go @@ -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" ) @@ -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") } diff --git a/resource-aggregate/cqrs/eventstore/cqldb/load.go b/resource-aggregate/cqrs/eventstore/cqldb/load.go index e9e2bb33d..fde225e8a 100644 --- a/resource-aggregate/cqrs/eventstore/cqldb/load.go +++ b/resource-aggregate/cqrs/eventstore/cqldb/load.go @@ -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" @@ -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(), diff --git a/resource-aggregate/cqrs/eventstore/mongodb/load.go b/resource-aggregate/cqrs/eventstore/mongodb/load.go index b449ad334..0455489c0 100644 --- a/resource-aggregate/cqrs/eventstore/mongodb/load.go +++ b/resource-aggregate/cqrs/eventstore/mongodb/load.go @@ -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" @@ -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) @@ -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) { @@ -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, @@ -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 } diff --git a/resource-aggregate/cqrs/eventstore/mongodb/maintenance.go b/resource-aggregate/cqrs/eventstore/mongodb/maintenance.go index a8db631fe..c31e18331 100644 --- a/resource-aggregate/cqrs/eventstore/mongodb/maintenance.go +++ b/resource-aggregate/cqrs/eventstore/mongodb/maintenance.go @@ -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" @@ -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 } diff --git a/resource-aggregate/cqrs/utils/utils.go b/resource-aggregate/cqrs/utils/utils.go index dcbf49301..24440b85b 100644 --- a/resource-aggregate/cqrs/utils/utils.go +++ b/resource-aggregate/cqrs/utils/utils.go @@ -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" ) @@ -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 { diff --git a/tools/mongodb/standby-tool/main.go b/tools/mongodb/standby-tool/main.go index 56d52776b..c7062086f 100644 --- a/tools/mongodb/standby-tool/main.go +++ b/tools/mongodb/standby-tool/main.go @@ -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) {