MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestConcurrentUserWrites

Function TestConcurrentUserWrites

auth/auth_test.go:701–794  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

699}
700
701func TestConcurrentUserWrites(t *testing.T) {
702 ctx := base.TestCtx(t)
703 bucket := base.GetTestBucket(t)
704 defer bucket.Close(ctx)
705
706 dataStore := bucket.GetSingleDataStore()
707
708 username := "foo"
709 password := "password"
710 email := "foo@bar.org"
711
712 // Create user
713 auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(base.TestCtx(t)))
714
715 // Modify the bcrypt cost to test rehashPassword properly below
716 require.Error(t, auth.SetBcryptCost(5))
717
718 user, _ := auth.NewUser(username, password, ch.BaseSetOf(t, "123", "456"))
719 user.SetExplicitRoles(ch.TimedSet{"role1": ch.NewVbSimpleSequence(1), "role2": ch.NewVbSimpleSequence(1)}, 1)
720 createErr := auth.Save(user)
721 if createErr != nil {
722 t.Errorf("Error creating user: %v", createErr)
723 }
724
725 // Retrieve user to trigger initial calculation of roles, channels
726 _, getErr := auth.GetUser(username)
727 require.NoError(t, getErr, "Error retrieving user")
728
729 require.NoError(t, auth.SetBcryptCost(DefaultBcryptCost))
730 // Reset bcryptCostChanged state after test runs
731 defer func() {
732 auth.bcryptCostChanged = false
733 }()
734
735 var wg sync.WaitGroup
736 wg.Add(4)
737 // Update user email, password hash, and invalidate user channels, roles concurrently
738 go func() {
739 user, getErr := auth.GetUser(username)
740 require.NoError(t, getErr, "Error retrieving user prior to invalidate channels")
741 require.NotNil(t, user, "User is nil prior to invalidate channels")
742
743 invalidateErr := auth.InvalidateDefaultChannels(username, true, 1)
744 assert.NoError(t, invalidateErr, "Error invalidating user's channels")
745 wg.Done()
746 }()
747
748 go func() {
749 user, getErr := auth.GetUser(username)
750 require.NoError(t, getErr, "Error retrieving user")
751 require.NotNil(t, user, "User is nil prior to email update")
752
753 updateErr := auth.UpdateUserEmail(user, email)
754 assert.NoError(t, updateErr, "Error updating user email")
755 wg.Done()
756 }()
757
758 go func() {

Callers

nothing calls this directly

Calls 15

SetExplicitRolesMethod · 0.95
EmailMethod · 0.95
RoleNamesMethod · 0.95
TestCtxFunction · 0.92
GetTestBucketFunction · 0.92
NewTestAuthenticatorFunction · 0.85
SetBcryptCostMethod · 0.80
NewUserMethod · 0.80
SaveMethod · 0.80
ErrorfMethod · 0.80
GetUserMethod · 0.80

Tested by

no test coverage detected