|
| 1 | +package aggregate_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | + "github.com/kelseyhightower/envconfig" |
| 11 | + "github.com/plgd-dev/cloud/pkg/net/grpc" |
| 12 | + "github.com/plgd-dev/cloud/resource-aggregate/cqrs/aggregate" |
| 13 | + "github.com/plgd-dev/cloud/resource-aggregate/cqrs/events" |
| 14 | + "github.com/plgd-dev/cloud/resource-aggregate/cqrs/eventstore" |
| 15 | + "github.com/plgd-dev/cloud/resource-aggregate/cqrs/eventstore/mongodb" |
| 16 | + "github.com/plgd-dev/cloud/resource-aggregate/cqrs/utils" |
| 17 | + "github.com/plgd-dev/cloud/resource-aggregate/pb" |
| 18 | + "github.com/plgd-dev/kit/security/certManager" |
| 19 | + "github.com/stretchr/testify/assert" |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | + "go.uber.org/atomic" |
| 22 | +) |
| 23 | + |
| 24 | +func testNewEventstore(t *testing.T) *mongodb.EventStore { |
| 25 | + var config certManager.Config |
| 26 | + err := envconfig.Process("DIAL", &config) |
| 27 | + assert.NoError(t, err) |
| 28 | + |
| 29 | + dialCertManager, err := certManager.NewCertManager(config) |
| 30 | + require.NoError(t, err) |
| 31 | + |
| 32 | + tlsConfig := dialCertManager.GetClientTLSConfig() |
| 33 | + |
| 34 | + store, err := mongodb.NewEventStore( |
| 35 | + mongodb.Config{ |
| 36 | + URI: "mongodb://localhost:27017", |
| 37 | + }, |
| 38 | + func(f func()) error { go f(); return nil }, |
| 39 | + mongodb.WithTLS(tlsConfig), |
| 40 | + ) |
| 41 | + require.NoError(t, err) |
| 42 | + require.NotNil(t, store) |
| 43 | + |
| 44 | + return store |
| 45 | +} |
| 46 | + |
| 47 | +func cleanUpToSnapshot(ctx context.Context, t *testing.T, store *mongodb.EventStore, evs []eventstore.Event) { |
| 48 | + for _, event := range evs { |
| 49 | + if ru, ok := event.(*events.ResourceStateSnapshotTaken); ok { |
| 50 | + if err := store.RemoveUpToVersion(ctx, []eventstore.VersionQuery{{GroupID: ru.GroupId(), AggregateID: ru.AggregateId(), Version: ru.Version()}}); err != nil { |
| 51 | + require.NoError(t, err) |
| 52 | + } |
| 53 | + fmt.Printf("snapshot at version %v\n", event.Version()) |
| 54 | + break |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func Test_parallelRequest(t *testing.T) { |
| 60 | + store := testNewEventstore(t) |
| 61 | + ctx := context.Background() |
| 62 | + ctx = grpc.CtxWithIncomingUserID(ctx, "test") |
| 63 | + defer store.Close(ctx) |
| 64 | + defer func() { |
| 65 | + err := store.Clear(ctx) |
| 66 | + require.NoError(t, err) |
| 67 | + }() |
| 68 | + |
| 69 | + deviceID := "7397398d-3ae8-4d9a-62d6-511f7b736a60" |
| 70 | + href := "/test/resource/1" |
| 71 | + commandPub := pb.PublishResourceRequest{ |
| 72 | + ResourceId: &pb.ResourceId{ |
| 73 | + DeviceId: deviceID, |
| 74 | + Href: href, |
| 75 | + }, |
| 76 | + Resource: &pb.Resource{ |
| 77 | + Id: utils.MakeResourceId(deviceID, href), |
| 78 | + DeviceId: deviceID, |
| 79 | + }, |
| 80 | + CommandMetadata: &pb.CommandMetadata{}, |
| 81 | + AuthorizationContext: &pb.AuthorizationContext{}, |
| 82 | + } |
| 83 | + |
| 84 | + newAggragate := func(deviceID, href string) *aggregate.Aggregate { |
| 85 | + a, err := aggregate.NewAggregate(deviceID, utils.MakeResourceId(deviceID, href), aggregate.NewDefaultRetryFunc(32), 16, store, func(context.Context) (aggregate.AggregateModel, error) { |
| 86 | + ev := events.NewResourceStateSnapshotTaken() |
| 87 | + ev.Id = utils.MakeResourceId(deviceID, href) |
| 88 | + return ev, nil |
| 89 | + }, nil) |
| 90 | + require.NoError(t, err) |
| 91 | + return a |
| 92 | + } |
| 93 | + |
| 94 | + concurrencyExcepTestA := newAggragate(commandPub.GetResourceId().GetDeviceId(), commandPub.GetResourceId().GetHref()) |
| 95 | + _, err := concurrencyExcepTestA.HandleCommand(ctx, &commandPub) |
| 96 | + require.NoError(t, err) |
| 97 | + |
| 98 | + numParallel := 3 |
| 99 | + var wg sync.WaitGroup |
| 100 | + var anyError atomic.Bool |
| 101 | + for i := 0; i < numParallel; i++ { |
| 102 | + wg.Add(1) |
| 103 | + go func() { |
| 104 | + defer wg.Done() |
| 105 | + for j := 0; j < 100000; j++ { |
| 106 | + if anyError.Load() { |
| 107 | + return |
| 108 | + } |
| 109 | + commandContentChanged := pb.NotifyResourceChangedRequest{ |
| 110 | + ResourceId: &pb.ResourceId{ |
| 111 | + DeviceId: deviceID, |
| 112 | + Href: href, |
| 113 | + }, |
| 114 | + Content: &pb.Content{ |
| 115 | + Data: []byte("hello world"), |
| 116 | + ContentType: "text", |
| 117 | + }, |
| 118 | + CommandMetadata: &pb.CommandMetadata{ |
| 119 | + ConnectionId: uuid.New().String(), |
| 120 | + }, |
| 121 | + Status: pb.Status_OK, |
| 122 | + AuthorizationContext: &pb.AuthorizationContext{}, |
| 123 | + } |
| 124 | + aggr := newAggragate(commandPub.GetResourceId().GetDeviceId(), commandPub.GetResourceId().GetHref()) |
| 125 | + events, err := aggr.HandleCommand(ctx, &commandContentChanged) |
| 126 | + if err != nil { |
| 127 | + anyError.Store(true) |
| 128 | + require.NoError(t, err) |
| 129 | + return |
| 130 | + } |
| 131 | + cleanUpToSnapshot(ctx, t, store, events) |
| 132 | + } |
| 133 | + }() |
| 134 | + } |
| 135 | + wg.Wait() |
| 136 | +} |
0 commit comments