Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
58 changes: 29 additions & 29 deletions cmd/locket/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ var _ = Describe("Locket", func() {

Context("when a lock is acquired", func() {
JustBeforeEach(func() {
requestedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.LOCK}
requestedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK}
_, err := locketClient.Lock(context.Background(), &models.LockRequest{
Resource: requestedResource,
TtlInSeconds: 10,
Expand Down Expand Up @@ -218,7 +218,7 @@ var _ = Describe("Locket", func() {
wg.Add(10)
for i := 0; i < 10; i++ {
key := fmt.Sprintf("test%d", i)
requestedResource := &models.Resource{Key: key, Value: "test-data", Owner: key, TypeCode: models.LOCK}
requestedResource := &models.Resource{Key: key, Value: "test-data", Owner: key, TypeCode: models.TypeCode_LOCK}
go func() {
defer GinkgoRecover()
defer wg.Done()
Expand Down Expand Up @@ -332,7 +332,7 @@ var _ = Describe("Locket", func() {
JustBeforeEach(func() {
_, err := sqlRunner.DB().Exec("DROP TABLE locks")
Expect(err).NotTo(HaveOccurred())
requestedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.LOCK}
requestedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK}
_, err = locketClient.Lock(context.Background(), &models.LockRequest{
Resource: requestedResource,
TtlInSeconds: 10,
Expand All @@ -347,8 +347,8 @@ var _ = Describe("Locket", func() {
})

It("locks the key with the corresponding value", func() {
requestedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.LOCK}
expectedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", Type: "lock", TypeCode: models.LOCK}
requestedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK}
expectedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", Type: "lock", TypeCode: models.TypeCode_LOCK}
_, err := locketClient.Lock(context.Background(), &models.LockRequest{
Resource: requestedResource,
TtlInSeconds: 10,
Expand All @@ -359,7 +359,7 @@ var _ = Describe("Locket", func() {
Expect(err).NotTo(HaveOccurred())
Expect(resp.Resource).To(BeEquivalentTo(expectedResource))

requestedResource = &models.Resource{Key: "test", Value: "test-data", Owner: "nima", TypeCode: models.LOCK}
requestedResource = &models.Resource{Key: "test", Value: "test-data", Owner: "nima", TypeCode: models.TypeCode_LOCK}
_, err = locketClient.Lock(context.Background(), &models.LockRequest{
Resource: requestedResource,
TtlInSeconds: 10,
Expand All @@ -368,7 +368,7 @@ var _ = Describe("Locket", func() {
})

It("logs the uuid of the request", func() {
requestedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.LOCK}
requestedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK}
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("uuid", "some-uuid"))
_, err := locketClient.Lock(ctx, &models.LockRequest{
Resource: requestedResource,
Expand All @@ -380,7 +380,7 @@ var _ = Describe("Locket", func() {
})

It("expires after a ttl", func() {
requestedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.LOCK}
requestedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK}
_, err := locketClient.Lock(context.Background(), &models.LockRequest{
Resource: requestedResource,
TtlInSeconds: 6,
Expand All @@ -395,7 +395,7 @@ var _ = Describe("Locket", func() {

Context("when the lock server disappears unexpectedly", func() {
It("still disappears after ~ the ttl", func() {
requestedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.LOCK}
requestedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK}
_, err := locketClient.Lock(context.Background(), &models.LockRequest{
Resource: requestedResource,
TtlInSeconds: 3,
Expand All @@ -421,16 +421,16 @@ var _ = Describe("Locket", func() {

Context("when the lock does not exist", func() {
It("does not throw an error releasing the lock", func() {
requestedResource = &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.LOCK}
requestedResource = &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK}
_, err := locketClient.Release(context.Background(), &models.ReleaseRequest{Resource: requestedResource})
Expect(err).NotTo(HaveOccurred())
})
})

Context("when the lock exists", func() {
JustBeforeEach(func() {
requestedResource = &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.LOCK}
expectedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", Type: "lock", TypeCode: models.LOCK}
requestedResource = &models.Resource{Key: "test", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK}
expectedResource := &models.Resource{Key: "test", Value: "test-data", Owner: "jim", Type: "lock", TypeCode: models.TypeCode_LOCK}
_, err := locketClient.Lock(context.Background(), &models.LockRequest{Resource: requestedResource, TtlInSeconds: 10})
Expect(err).NotTo(HaveOccurred())

Expand All @@ -449,7 +449,7 @@ var _ = Describe("Locket", func() {

Context("when another process is the lock owner", func() {
It("throws an error", func() {
requestedResource = &models.Resource{Key: "test", Value: "test-data", Owner: "nima", TypeCode: models.LOCK}
requestedResource = &models.Resource{Key: "test", Value: "test-data", Owner: "nima", TypeCode: models.TypeCode_LOCK}
_, err := locketClient.Release(context.Background(), &models.ReleaseRequest{Resource: requestedResource})
Expect(err).To(HaveOccurred())
})
Expand Down Expand Up @@ -492,42 +492,42 @@ var _ = Describe("Locket", func() {
var expectedResource1, expectedResource2, expectedResource3, expectedResource4 *models.Resource

BeforeEach(func() {
resource1 = &models.Resource{Key: "test-lock1", Value: "test-data", Owner: "jim", TypeCode: models.LOCK}
resource2 = &models.Resource{Key: "test-lock2", Value: "test-data", Owner: "jim", TypeCode: models.LOCK}
resource3 = &models.Resource{Key: "test-presence1", Value: "test-data", Owner: "jim", TypeCode: models.PRESENCE}
resource4 = &models.Resource{Key: "test-presence2", Value: "test-data", Owner: "jim", TypeCode: models.PRESENCE}

expectedResource1 = &models.Resource{Key: "test-lock1", Value: "test-data", Owner: "jim", TypeCode: models.LOCK, Type: models.LockType}
expectedResource2 = &models.Resource{Key: "test-lock2", Value: "test-data", Owner: "jim", TypeCode: models.LOCK, Type: models.LockType}
expectedResource3 = &models.Resource{Key: "test-presence1", Value: "test-data", Owner: "jim", TypeCode: models.PRESENCE, Type: models.PresenceType}
expectedResource4 = &models.Resource{Key: "test-presence2", Value: "test-data", Owner: "jim", TypeCode: models.PRESENCE, Type: models.PresenceType}
resource1 = &models.Resource{Key: "test-lock1", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK}
resource2 = &models.Resource{Key: "test-lock2", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK}
resource3 = &models.Resource{Key: "test-presence1", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_PRESENCE}
resource4 = &models.Resource{Key: "test-presence2", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_PRESENCE}

expectedResource1 = &models.Resource{Key: "test-lock1", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK, Type: models.LockType}
expectedResource2 = &models.Resource{Key: "test-lock2", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK, Type: models.LockType}
expectedResource3 = &models.Resource{Key: "test-presence1", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_PRESENCE, Type: models.PresenceType}
expectedResource4 = &models.Resource{Key: "test-presence2", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_PRESENCE, Type: models.PresenceType}
})

It("fetches all the locks corresponding to type code", func() {
response, err := locketClient.FetchAll(context.Background(), &models.FetchAllRequest{TypeCode: models.LOCK})
response, err := locketClient.FetchAll(context.Background(), &models.FetchAllRequest{TypeCode: models.TypeCode_LOCK})
Expect(err).NotTo(HaveOccurred())
Expect(response.Resources).To(ConsistOf(expectedResource1, expectedResource2))
})

It("fetches all the presences corresponding to type", func() {
response, err := locketClient.FetchAll(context.Background(), &models.FetchAllRequest{TypeCode: models.PRESENCE})
response, err := locketClient.FetchAll(context.Background(), &models.FetchAllRequest{TypeCode: models.TypeCode_PRESENCE})
Expect(err).NotTo(HaveOccurred())
Expect(response.Resources).To(ConsistOf(expectedResource3, expectedResource4))
})
})

Context("if the table disappears", func() {
BeforeEach(func() {
resource1 = &models.Resource{Key: "test-lock1", Value: "test-data", Owner: "jim", TypeCode: models.LOCK}
resource2 = &models.Resource{Key: "test-lock2", Value: "test-data", Owner: "jim", TypeCode: models.LOCK}
resource3 = &models.Resource{Key: "test-presence1", Value: "test-data", Owner: "jim", TypeCode: models.PRESENCE}
resource4 = &models.Resource{Key: "test-presence2", Value: "test-data", Owner: "jim", TypeCode: models.PRESENCE}
resource1 = &models.Resource{Key: "test-lock1", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK}
resource2 = &models.Resource{Key: "test-lock2", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_LOCK}
resource3 = &models.Resource{Key: "test-presence1", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_PRESENCE}
resource4 = &models.Resource{Key: "test-presence2", Value: "test-data", Owner: "jim", TypeCode: models.TypeCode_PRESENCE}
})

JustBeforeEach(func() {
_, err := sqlRunner.DB().Exec("DROP TABLE locks")
Expect(err).NotTo(HaveOccurred())
_, err = locketClient.FetchAll(context.Background(), &models.FetchAllRequest{TypeCode: models.LOCK})
_, err = locketClient.FetchAll(context.Background(), &models.FetchAllRequest{TypeCode: models.TypeCode_LOCK})
Expect(err).To(HaveOccurred())
})

Expand Down
2 changes: 1 addition & 1 deletion db/dbfakes/fake_lock_db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions db/lock_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ func (db *SQLDB) Lock(ctx context.Context, logger lager.Logger, resource *models
if newLock {
_, err = db.helper.Insert(ctx, logger, tx, "locks",
helpers.SQLAttributes{
"path": lock.Key,
"owner": lock.Owner,
"value": lock.Value,
"path": lock.Key,
"owner": lock.Owner,
"value": lock.Value,
//lint:ignore SA1019 - backwards compatibility
"type": lock.Type,
"modified_index": lock.ModifiedIndex,
"modified_id": lock.ModifiedId,
Expand All @@ -70,8 +71,9 @@ func (db *SQLDB) Lock(ctx context.Context, logger lager.Logger, resource *models
} else {
_, err = db.helper.Update(ctx, logger, tx, "locks",
helpers.SQLAttributes{
"owner": lock.Owner,
"value": lock.Value,
"owner": lock.Owner,
"value": lock.Value,
//lint:ignore SA1019 - backwards compatibility
"type": lock.Type,
"modified_index": lock.ModifiedIndex,
"modified_id": lock.ModifiedId,
Expand Down
30 changes: 19 additions & 11 deletions db/lock_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func validateLockInDB(rawDB *sql.DB, res *models.Resource, expectedIndex, expect
if res.Value != value {
errMsg += fmt.Sprintf("mismatch value (%s, %s),", res.Value, value)
}
//lint:ignore SA1019 - testing deprecated functionality
if res.Type != lockType {
//lint:ignore SA1019 - testing deprecated functionality
errMsg += fmt.Sprintf("mismatch value (%s, %s),", res.Type, lockType)
}
if expectedIndex != index {
Expand Down Expand Up @@ -83,11 +85,12 @@ var _ = Describe("Lock", func() {
Type: "lock",
}
expectedResource = &models.Resource{
Key: resource.Key,
Owner: resource.Owner,
Value: resource.Value,
Key: resource.Key,
Owner: resource.Owner,
Value: resource.Value,
//lint:ignore SA1019 - testing deprecated functionality
Type: resource.Type,
TypeCode: models.LOCK,
TypeCode: models.TypeCode_LOCK,
}

fakeGUIDProvider.NextGUIDReturns("new-guid", nil)
Expand All @@ -102,7 +105,7 @@ var _ = Describe("Lock", func() {
Key: "quack",
Owner: "iamthelizardking",
Value: "i can do anything",
TypeCode: models.LOCK,
TypeCode: models.TypeCode_LOCK,
}
lock, err := sqlDB.Lock(ctx, logger, typeCodeResource, 10)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -290,6 +293,7 @@ var _ = Describe("Lock", func() {
`INSERT INTO locks (path, owner, value, type, modified_index, modified_id, ttl) VALUES (?, ?, ?, ?, ?, ?, ?);`,
dbFlavor,
)
//lint:ignore SA1019 - testing deprecated functionality
result, err := rawDB.Exec(query, lock.Key, lock.Owner, lock.Value, lock.Type, 434, "modified-id", 5)
Expect(err).NotTo(HaveOccurred())
Expect(result.RowsAffected()).To(BeEquivalentTo(1))
Expand Down Expand Up @@ -329,7 +333,7 @@ var _ = Describe("Lock", func() {
Type: "lock",
}
expectedLock = lock
expectedLock.TypeCode = models.LOCK
expectedLock.TypeCode = models.TypeCode_LOCK
})

It("returns the lock from the database", func() {
Expand All @@ -353,7 +357,7 @@ var _ = Describe("Lock", func() {
Type: "presence",
}
expectedLock = lock
expectedLock.TypeCode = models.PRESENCE
expectedLock.TypeCode = models.TypeCode_PRESENCE
})

It("returns the lock from the database", func() {
Expand All @@ -374,9 +378,10 @@ var _ = Describe("Lock", func() {
Key: "test",
Owner: "jim",
Value: "locks stuff for days",
TypeCode: models.LOCK,
TypeCode: models.TypeCode_LOCK,
}
expectedLock = lock
//lint:ignore SA1019 - testing deprecated functionality
expectedLock.Type = models.LockType
})

Expand All @@ -398,9 +403,10 @@ var _ = Describe("Lock", func() {
Key: "test",
Owner: "jim",
Value: "locks stuff for days",
TypeCode: models.PRESENCE,
TypeCode: models.TypeCode_PRESENCE,
}
expectedLock = lock
//lint:ignore SA1019 - testing deprecated functionality
expectedLock.Type = models.PresenceType
})

Expand Down Expand Up @@ -499,7 +505,7 @@ var _ = Describe("Lock", func() {
Owner: "finn",
Value: "thehuman",
Type: "presence",
TypeCode: models.PRESENCE,
TypeCode: models.TypeCode_PRESENCE,
},
ModifiedIndex: 10,
ModifiedId: "hello",
Expand Down Expand Up @@ -554,6 +560,7 @@ var _ = Describe("Lock", func() {
dbFlavor,
)

//lint:ignore SA1019 - testing deprecated functionality
result, err := rawDB.Exec(query, resource.Key, resource.Owner, resource.Value, currentIndex, currentTTL, modifiedId, resource.Type)
Expect(err).NotTo(HaveOccurred())
Expect(result.RowsAffected()).To(BeEquivalentTo(1))
Expand Down Expand Up @@ -643,7 +650,8 @@ var _ = Describe("Lock", func() {
Key: resource.Key,
Owner: "danny",
Value: resource.Value,
Type: resource.Type,
//lint:ignore SA1019 - testing deprecated functionality
Type: resource.Type,
}

released, err := sqlDB.FetchAndRelease(ctx, logger, oldLock)
Expand Down
4 changes: 2 additions & 2 deletions expiration/burglar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _ = Describe("Burglar", func() {
Owner: "town",
Value: "won't you take me to",
Type: "lock",
TypeCode: models.LOCK,
TypeCode: models.TypeCode_LOCK,
},
TtlInSeconds: 25,
ModifiedIndex: 3587584357348,
Expand All @@ -58,7 +58,7 @@ var _ = Describe("Burglar", func() {
Owner: "bar",
Value: "chocolate chip",
Type: "presence",
TypeCode: models.PRESENCE,
TypeCode: models.TypeCode_PRESENCE,
},
TtlInSeconds: 437,
ModifiedIndex: 2346,
Expand Down
2 changes: 1 addition & 1 deletion expiration/expirationfakes/fake_lock_pick.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions expiration/lock_pick.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (l lockPick) ExpirationCounts() (uint32, uint32) {
}

func (l lockPick) RegisterTTL(logger lager.Logger, lock *db.Lock) {
//lint:ignore SA1019 - backwards compatibility
logger = logger.Session("register-ttl", lager.Data{"key": lock.Key, "modified-index": lock.ModifiedIndex, "type": lock.Type})
logger.Debug("starting")
logger.Debug("completed")
Expand Down Expand Up @@ -107,6 +108,7 @@ func (l lockPick) checkExpiration(logger lager.Logger, lock *db.Lock, closeChan
if expired {
logger.Info("lock-expired")
counter := l.locksExpiredCount
//lint:ignore SA1019 - backwards compatibility
if lock.Type == models.PresenceType {
counter = l.presencesExpiredCount
}
Expand Down
4 changes: 3 additions & 1 deletion grpcserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ var _ = Describe("GRPCServer", func() {
})
})

type testHandler struct{}
type testHandler struct {
models.UnimplementedLocketServer
}

func (h *testHandler) Lock(ctx context.Context, req *models.LockRequest) (*models.LockResponse, error) {
return &models.LockResponse{}, nil
Expand Down
2 changes: 1 addition & 1 deletion handlers/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var _ = Describe("LocketHandler", func() {
Key: "test",
Value: "test-value",
Owner: "myself",
TypeCode: models.LOCK,
TypeCode: models.TypeCode_LOCK,
}

locketHandler = handlers.NewLocketHandler(
Expand Down
3 changes: 2 additions & 1 deletion handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type locketHandler struct {
exitCh chan<- struct{}
lockPick expiration.LockPick
metrics metrics_helpers.RequestMetrics
models.UnimplementedLocketServer
}

func NewLocketHandler(logger lager.Logger, db db.LockDB, lockPick expiration.LockPick, requestMetrics metrics_helpers.RequestMetrics, exitCh chan<- struct{}) *locketHandler {
Expand Down Expand Up @@ -259,7 +260,7 @@ func validate(req interface{}) error {
return models.ErrInvalidType
}

if reqTypeCode == models.UNKNOWN {
if reqTypeCode == models.TypeCode_UNKNOWN {
return models.ErrInvalidType
}

Expand Down
Loading