|
| 1 | +package verifier |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/rand" |
| 6 | + "sync/atomic" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/10gen/migration-verifier/internal/partitions" |
| 10 | + "github.com/10gen/migration-verifier/mslices" |
| 11 | + "github.com/pkg/errors" |
| 12 | + "github.com/samber/lo" |
| 13 | + "go.mongodb.org/mongo-driver/bson" |
| 14 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 15 | +) |
| 16 | + |
| 17 | +// TestFetchAndCompareDocuments_ContextCancellation ensures that nothing hangs |
| 18 | +// when a context is canceled during FetchAndCompareDocuments(). |
| 19 | +func (s *IntegrationTestSuite) TestFetchAndCompareDocuments_Context() { |
| 20 | + ctx := s.Context() |
| 21 | + |
| 22 | + for _, client := range mslices.Of(s.srcMongoClient, s.dstMongoClient) { |
| 23 | + docs := lo.RepeatBy( |
| 24 | + 10_000, |
| 25 | + func(i int) bson.D { |
| 26 | + return bson.D{} |
| 27 | + }, |
| 28 | + ) |
| 29 | + |
| 30 | + _, err := client.Database(s.DBNameForTest()).Collection("stuff"). |
| 31 | + InsertMany(ctx, lo.ToAnySlice(docs)) |
| 32 | + |
| 33 | + s.Require().NoError(err) |
| 34 | + } |
| 35 | + |
| 36 | + task := VerificationTask{ |
| 37 | + PrimaryKey: primitive.NewObjectID(), |
| 38 | + Type: verificationTaskVerifyDocuments, |
| 39 | + Status: verificationTaskProcessing, |
| 40 | + QueryFilter: QueryFilter{ |
| 41 | + Namespace: s.DBNameForTest() + ".stuff", |
| 42 | + Partition: &partitions.Partition{ |
| 43 | + Key: partitions.PartitionKey{ |
| 44 | + Lower: primitive.MinKey{}, |
| 45 | + }, |
| 46 | + Upper: primitive.MaxKey{}, |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + verifier := s.BuildVerifier() |
| 52 | + |
| 53 | + for range 100 { |
| 54 | + cancelableCtx, cancel := context.WithCancelCause(ctx) |
| 55 | + |
| 56 | + var done atomic.Bool |
| 57 | + go func() { |
| 58 | + verifier.FetchAndCompareDocuments( |
| 59 | + cancelableCtx, |
| 60 | + &task, |
| 61 | + ) |
| 62 | + done.Store(true) |
| 63 | + }() |
| 64 | + |
| 65 | + delay := time.Duration(100 * float64(time.Millisecond) * rand.Float64()) |
| 66 | + time.Sleep(delay) |
| 67 | + cancel(errors.Errorf("canceled after %s", delay)) |
| 68 | + |
| 69 | + s.Assert().Eventually( |
| 70 | + func() bool { |
| 71 | + return done.Load() |
| 72 | + }, |
| 73 | + time.Minute, |
| 74 | + 10*time.Millisecond, |
| 75 | + "cancellation after %s should not cause hang", |
| 76 | + ) |
| 77 | + } |
| 78 | +} |
0 commit comments