2019-02-28 20:54:57 +00:00
|
|
|
// Copyright 2019 clair authors
|
2016-01-19 20:16:45 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-01-08 19:42:07 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-28 20:54:57 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-01-08 19:42:07 +00:00
|
|
|
)
|
|
|
|
|
2019-02-28 20:54:57 +00:00
|
|
|
func TestAcquireLockReturnsExistingLockDuration(t *testing.T) {
|
2019-03-06 18:44:23 +00:00
|
|
|
tx, cleanup := createTestPgSessionWithFixtures(t, "Lock")
|
|
|
|
defer cleanup()
|
2019-02-28 20:54:57 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-01-08 19:42:07 +00:00
|
|
|
func TestLock(t *testing.T) {
|
2017-07-26 23:23:54 +00:00
|
|
|
datastore, tx := openSessionForTest(t, "Lock", true)
|
2016-01-08 19:42:07 +00:00
|
|
|
defer datastore.Close()
|
|
|
|
|
|
|
|
var l bool
|
|
|
|
|
|
|
|
// Create a first lock.
|
2019-02-28 19:17:09 +00:00
|
|
|
l, _, err := tx.AcquireLock("test1", "owner1", time.Minute)
|
2017-07-26 23:23:54 +00:00
|
|
|
assert.Nil(t, err)
|
2016-01-08 19:42:07 +00:00
|
|
|
assert.True(t, l)
|
2017-07-26 23:23:54 +00:00
|
|
|
tx = restartSession(t, datastore, tx, true)
|
2016-01-08 19:42:07 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
// lock again by itself, the previous lock is not expired yet.
|
2019-02-28 19:17:09 +00:00
|
|
|
l, _, err = tx.AcquireLock("test1", "owner1", time.Minute)
|
2017-07-26 23:23:54 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-06 18:44:23 +00:00
|
|
|
assert.True(t, l) // acquire lock no-op when owner already has the lock.
|
2017-07-26 23:23:54 +00:00
|
|
|
tx = restartSession(t, datastore, tx, false)
|
2016-01-08 19:42:07 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
// Try to renew the same lock with another owner.
|
2019-02-28 19:17:09 +00:00
|
|
|
l, _, err = tx.ExtendLock("test1", "owner2", time.Minute)
|
2017-07-26 23:23:54 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.False(t, l)
|
|
|
|
tx = restartSession(t, datastore, tx, false)
|
|
|
|
|
2019-02-28 19:17:09 +00:00
|
|
|
l, _, err = tx.AcquireLock("test1", "owner2", time.Minute)
|
2017-07-26 23:23:54 +00:00
|
|
|
assert.Nil(t, err)
|
2016-01-08 19:42:07 +00:00
|
|
|
assert.False(t, l)
|
2017-07-26 23:23:54 +00:00
|
|
|
tx = restartSession(t, datastore, tx, false)
|
2016-01-08 19:42:07 +00:00
|
|
|
|
|
|
|
// Renew the lock.
|
2019-02-28 19:17:09 +00:00
|
|
|
l, _, err = tx.ExtendLock("test1", "owner1", 2*time.Minute)
|
2017-07-26 23:23:54 +00:00
|
|
|
assert.Nil(t, err)
|
2016-01-08 19:42:07 +00:00
|
|
|
assert.True(t, l)
|
2017-07-26 23:23:54 +00:00
|
|
|
tx = restartSession(t, datastore, tx, true)
|
2016-01-08 19:42:07 +00:00
|
|
|
|
|
|
|
// Unlock and then relock by someone else.
|
2019-02-28 19:17:09 +00:00
|
|
|
err = tx.ReleaseLock("test1", "owner1")
|
2017-07-26 23:23:54 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
tx = restartSession(t, datastore, tx, true)
|
2016-01-08 19:42:07 +00:00
|
|
|
|
2019-02-28 19:17:09 +00:00
|
|
|
l, _, err = tx.AcquireLock("test1", "owner2", time.Minute)
|
2017-07-26 23:23:54 +00:00
|
|
|
assert.Nil(t, err)
|
2016-01-08 19:42:07 +00:00
|
|
|
assert.True(t, l)
|
2017-07-26 23:23:54 +00:00
|
|
|
tx = restartSession(t, datastore, tx, true)
|
2016-01-08 19:42:07 +00:00
|
|
|
|
|
|
|
// Create a second lock which is actually already expired ...
|
2019-02-28 19:17:09 +00:00
|
|
|
l, _, err = tx.AcquireLock("test2", "owner1", -time.Minute)
|
2017-07-26 23:23:54 +00:00
|
|
|
assert.Nil(t, err)
|
2016-01-08 19:42:07 +00:00
|
|
|
assert.True(t, l)
|
2017-07-26 23:23:54 +00:00
|
|
|
tx = restartSession(t, datastore, tx, true)
|
2016-01-08 19:42:07 +00:00
|
|
|
|
|
|
|
// Take over the lock
|
2019-02-28 19:17:09 +00:00
|
|
|
l, _, err = tx.AcquireLock("test2", "owner2", time.Minute)
|
2017-07-26 23:23:54 +00:00
|
|
|
assert.Nil(t, err)
|
2016-01-08 19:42:07 +00:00
|
|
|
assert.True(t, l)
|
2017-07-26 23:23:54 +00:00
|
|
|
tx = restartSession(t, datastore, tx, true)
|
|
|
|
|
|
|
|
if !assert.Nil(t, tx.Rollback()) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|