|
|
@ -1,4 +1,4 @@
|
|
|
|
// Copyright 2016 clair authors
|
|
|
|
// Copyright 2019 clair authors
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
@ -19,8 +19,23 @@ import (
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestAcquireLockReturnsExistingLockDuration(t *testing.T) {
|
|
|
|
|
|
|
|
datastore, tx := openSessionForTest(t, "Lock", true)
|
|
|
|
|
|
|
|
defer datastore.Close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
acquired, originalExpiration, err := tx.AcquireLock("test1", "owner1", time.Minute)
|
|
|
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
require.True(t, acquired)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
acquired2, expiration, err := tx.AcquireLock("test1", "owner2", time.Hour)
|
|
|
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
require.False(t, acquired2)
|
|
|
|
|
|
|
|
require.Equal(t, expiration, originalExpiration)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestLock(t *testing.T) {
|
|
|
|
func TestLock(t *testing.T) {
|
|
|
|
datastore, tx := openSessionForTest(t, "Lock", true)
|
|
|
|
datastore, tx := openSessionForTest(t, "Lock", true)
|
|
|
|
defer datastore.Close()
|
|
|
|
defer datastore.Close()
|
|
|
@ -28,52 +43,52 @@ func TestLock(t *testing.T) {
|
|
|
|
var l bool
|
|
|
|
var l bool
|
|
|
|
|
|
|
|
|
|
|
|
// Create a first lock.
|
|
|
|
// Create a first lock.
|
|
|
|
l, _, err := tx.Lock("test1", "owner1", time.Minute, false)
|
|
|
|
l, _, err := tx.AcquireLock("test1", "owner1", time.Minute)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, l)
|
|
|
|
assert.True(t, l)
|
|
|
|
tx = restartSession(t, datastore, tx, true)
|
|
|
|
tx = restartSession(t, datastore, tx, true)
|
|
|
|
|
|
|
|
|
|
|
|
// lock again by itself, the previous lock is not expired yet.
|
|
|
|
// lock again by itself, the previous lock is not expired yet.
|
|
|
|
l, _, err = tx.Lock("test1", "owner1", time.Minute, false)
|
|
|
|
l, _, err = tx.AcquireLock("test1", "owner1", time.Minute)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.False(t, l)
|
|
|
|
assert.False(t, l)
|
|
|
|
tx = restartSession(t, datastore, tx, false)
|
|
|
|
tx = restartSession(t, datastore, tx, false)
|
|
|
|
|
|
|
|
|
|
|
|
// Try to renew the same lock with another owner.
|
|
|
|
// Try to renew the same lock with another owner.
|
|
|
|
l, _, err = tx.Lock("test1", "owner2", time.Minute, true)
|
|
|
|
l, _, err = tx.ExtendLock("test1", "owner2", time.Minute)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.False(t, l)
|
|
|
|
assert.False(t, l)
|
|
|
|
tx = restartSession(t, datastore, tx, false)
|
|
|
|
tx = restartSession(t, datastore, tx, false)
|
|
|
|
|
|
|
|
|
|
|
|
l, _, err = tx.Lock("test1", "owner2", time.Minute, false)
|
|
|
|
l, _, err = tx.AcquireLock("test1", "owner2", time.Minute)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.False(t, l)
|
|
|
|
assert.False(t, l)
|
|
|
|
tx = restartSession(t, datastore, tx, false)
|
|
|
|
tx = restartSession(t, datastore, tx, false)
|
|
|
|
|
|
|
|
|
|
|
|
// Renew the lock.
|
|
|
|
// Renew the lock.
|
|
|
|
l, _, err = tx.Lock("test1", "owner1", 2*time.Minute, true)
|
|
|
|
l, _, err = tx.ExtendLock("test1", "owner1", 2*time.Minute)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, l)
|
|
|
|
assert.True(t, l)
|
|
|
|
tx = restartSession(t, datastore, tx, true)
|
|
|
|
tx = restartSession(t, datastore, tx, true)
|
|
|
|
|
|
|
|
|
|
|
|
// Unlock and then relock by someone else.
|
|
|
|
// Unlock and then relock by someone else.
|
|
|
|
err = tx.Unlock("test1", "owner1")
|
|
|
|
err = tx.ReleaseLock("test1", "owner1")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
tx = restartSession(t, datastore, tx, true)
|
|
|
|
tx = restartSession(t, datastore, tx, true)
|
|
|
|
|
|
|
|
|
|
|
|
l, _, err = tx.Lock("test1", "owner2", time.Minute, false)
|
|
|
|
l, _, err = tx.AcquireLock("test1", "owner2", time.Minute)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, l)
|
|
|
|
assert.True(t, l)
|
|
|
|
tx = restartSession(t, datastore, tx, true)
|
|
|
|
tx = restartSession(t, datastore, tx, true)
|
|
|
|
|
|
|
|
|
|
|
|
// Create a second lock which is actually already expired ...
|
|
|
|
// Create a second lock which is actually already expired ...
|
|
|
|
l, _, err = tx.Lock("test2", "owner1", -time.Minute, false)
|
|
|
|
l, _, err = tx.AcquireLock("test2", "owner1", -time.Minute)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, l)
|
|
|
|
assert.True(t, l)
|
|
|
|
tx = restartSession(t, datastore, tx, true)
|
|
|
|
tx = restartSession(t, datastore, tx, true)
|
|
|
|
|
|
|
|
|
|
|
|
// Take over the lock
|
|
|
|
// Take over the lock
|
|
|
|
l, _, err = tx.Lock("test2", "owner2", time.Minute, false)
|
|
|
|
l, _, err = tx.AcquireLock("test2", "owner2", time.Minute)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, l)
|
|
|
|
assert.True(t, l)
|
|
|
|
tx = restartSession(t, datastore, tx, true)
|
|
|
|
tx = restartSession(t, datastore, tx, true)
|
|
|
|