Multiple clients are attempting to push the same new revision concurrently; first writer should be successful, subsequent writers should fail on CAS, and then identify that revision already exists on retry.
(t *testing.T)
| 3378 | // Multiple clients are attempting to push the same new revision concurrently; first writer should be successful, |
| 3379 | // subsequent writers should fail on CAS, and then identify that revision already exists on retry. |
| 3380 | func TestConcurrentPushSameNewRevision(t *testing.T) { |
| 3381 | base.SetUpTestLogging(t, base.LevelDebug, base.KeyCRUD) |
| 3382 | var db *Database |
| 3383 | var enableCallback bool |
| 3384 | var revId string |
| 3385 | var ctx context.Context |
| 3386 | |
| 3387 | writeUpdateCallback := func(key string) { |
| 3388 | if enableCallback { |
| 3389 | enableCallback = false |
| 3390 | body := Body{"name": "Bob", "age": 52} |
| 3391 | collection, ctx := GetSingleDatabaseCollectionWithUser(ctx, t, db) |
| 3392 | revId, _, err := collection.Put(ctx, "doc1", body) |
| 3393 | assert.NoError(t, err, "Couldn't create document") |
| 3394 | assert.NotEmpty(t, revId) |
| 3395 | } |
| 3396 | } |
| 3397 | |
| 3398 | // Use leaky bucket to inject callback in query invocation |
| 3399 | queryCallbackConfig := base.LeakyBucketConfig{ |
| 3400 | UpdateCallback: writeUpdateCallback, |
| 3401 | } |
| 3402 | |
| 3403 | db, ctx = setupTestLeakyDBWithCacheOptions(t, DefaultCacheOptions(), queryCallbackConfig) |
| 3404 | defer db.Close(ctx) |
| 3405 | collection, ctx := GetSingleDatabaseCollectionWithUser(ctx, t, db) |
| 3406 | |
| 3407 | enableCallback = true |
| 3408 | |
| 3409 | body := Body{"name": "Bob", "age": 52} |
| 3410 | _, _, err := collection.Put(ctx, "doc1", body) |
| 3411 | require.Error(t, err) |
| 3412 | assert.Equal(t, "409 Document exists", err.Error()) |
| 3413 | |
| 3414 | doc, err := collection.GetDocument(ctx, "doc1", DocUnmarshalAll) |
| 3415 | assert.Equal(t, revId, doc.RevID) |
| 3416 | assert.NoError(t, err, "Couldn't retrieve document") |
| 3417 | assert.Equal(t, "Bob", doc.Body(ctx)["name"]) |
| 3418 | assert.Equal(t, json.Number("52"), doc.Body(ctx)["age"]) |
| 3419 | } |
| 3420 | |
| 3421 | // Multiple clients are attempting to push the same new, non-winning revision concurrently; non-winning is an |
| 3422 | // update to a non-winning branch that leaves the branch still non-winning (i.e. shorter) than the active branch |
nothing calls this directly
no test coverage detected