Skip to content
Merged
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
71 changes: 71 additions & 0 deletions sync/lock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright © 2025 Prabhjot Singh Sethi, All Rights reserved
// Author: Prabhjot Singh Sethi <prabhjot.sethi@gmail.com>

package sync

import (
"context"
"testing"

"github.com/Prabhjot-Sethi/core/db"
"github.com/Prabhjot-Sethi/core/errors"
)

type lockKey struct {
Scope string
Name string
}

func Test_LockBaseTesting(t *testing.T) {
config := &db.MongoConfig{
Host: "localhost",
Port: "27017",
Username: "root",
Password: "password",
}

client, err := db.NewMongoClient(config)

if err != nil {
t.Errorf("failed to connect to mongo DB Error: %s", err)
return
}

s := client.GetDataStore("test-sync")

err = InitializeLockOwner(context.Background(), s, "test-owner")
if err != nil && !errors.IsAlreadyExists(err) {
t.Errorf("Got error while initializing lock owner %s", err)
}

tbl, err := LocateLockTable(s, "demo-test")

if err != nil {
t.Errorf("failed to locate Lock Table: %s", err)
}

key1 := &lockKey{
Scope: "scope-1",
Name: "test-key",
}

lock, err := tbl.TryAcquire(context.Background(), key1)
if err != nil {
t.Errorf("failed to acquire lock: %s", err)
}
_, err = tbl.TryAcquire(context.Background(), key1)
if err == nil {
t.Errorf("Acquired lock for %s:%s, which should have failed", key1.Scope, key1.Name)
}
key2 := &lockKey{
Scope: "scope-2",
Name: "test-key",
}

lock1, err := tbl.TryAcquire(context.Background(), key2)
if err != nil {
t.Errorf("failed to acquire lock: %s", err)
}
_ = lock1.Close()
_ = lock.Close()
}
5 changes: 3 additions & 2 deletions sync/owner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/Prabhjot-Sethi/core/db"
"github.com/Prabhjot-Sethi/core/errors"
)

func Test_OwnerInit(t *testing.T) {
Expand All @@ -34,9 +35,9 @@ func Test_OwnerInit(t *testing.T) {
t.Errorf("failed to perform Health check with DB Error: %s", err)
}

s := client.GetDataStore("test")
s := client.GetDataStore("test-sync")
err = InitializeLockOwner(ctx, s, "test-owner")
if err != nil {
if err != nil && !errors.IsAlreadyExists(err) {
t.Errorf("Got error while initializing lock owner %s", err)
}
time.Sleep(1 * time.Second)
Expand Down