Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions go/test/endtoend/topotest/zk2/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package zk2
import (
"context"
"flag"
"fmt"
"os"
"testing"
"time"
Expand Down Expand Up @@ -197,6 +198,53 @@ func TestKeyspaceLocking(t *testing.T) {
topoutils.WaitForBoolValue(t, &secondThreadLockAcquired, true)
}

// TestNamedLocking tests that named locking works as intended.
func TestNamedLocking(t *testing.T) {
// Create topo server connection.
ts, err := topo.OpenServer(*clusterInstance.TopoFlavorString(), clusterInstance.VtctldClientProcess.TopoGlobalAddress, clusterInstance.VtctldClientProcess.TopoGlobalRoot)
require.NoError(t, err)

ctx := context.Background()
lockName := "TestNamedLocking"
action := "Testing"

// Acquire a named lock.
ctx, unlock, err := ts.LockName(ctx, lockName, action)
require.NoError(t, err)

// Check that we can't reacquire it from the same context.
_, _, err = ts.LockName(ctx, lockName, action)
require.ErrorContains(t, err, fmt.Sprintf("lock for named %s is already held", lockName))

// Check that CheckNameLocked doesn't return an error as we should still be
// holding the lock.
err = topo.CheckNameLocked(ctx, lockName)
require.NoError(t, err)

// We'll now try to acquire the lock from a different goroutine.
secondCallerAcquired := false
go func() {
_, unlock, err := ts.LockName(context.Background(), lockName, action)
defer unlock(&err)
require.NoError(t, err)
secondCallerAcquired = true
}()

// Wait for some time and ensure that the second attempt at acquiring the lock
// is blocked.
time.Sleep(100 * time.Millisecond)
require.False(t, secondCallerAcquired)

// Unlock the name.
unlock(&err)
// Check that we no longer have the named lock.
err = topo.CheckNameLocked(ctx, lockName)
require.ErrorContains(t, err, fmt.Sprintf("named %s is not locked (no lockInfo in map)", lockName))

// Wait to see that the second goroutine WAS now able to acquire the named lock.
topoutils.WaitForBoolValue(t, &secondCallerAcquired, true)
}

func execMulti(t *testing.T, conn *mysql.Conn, query string) []*sqltypes.Result {
t.Helper()
var res []*sqltypes.Result
Expand Down
Loading
Loading