2018-05-29 18:35:48 +00:00
|
|
|
// Copyright 2016 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package fileutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-02 15:43:54 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-08-14 09:07:02 +00:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2018-05-29 18:35:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLocking(t *testing.T) {
|
|
|
|
dir := testutil.NewTemporaryDirectory("test_flock", t)
|
|
|
|
defer dir.Close()
|
|
|
|
|
|
|
|
fileName := filepath.Join(dir.Path(), "LOCK")
|
|
|
|
|
2021-09-02 15:43:54 +00:00
|
|
|
_, err := os.Stat(fileName)
|
|
|
|
require.Error(t, err, "File %q unexpectedly exists.", fileName)
|
2018-05-29 18:35:48 +00:00
|
|
|
|
|
|
|
lock, existed, err := Flock(fileName)
|
2024-02-01 14:18:01 +00:00
|
|
|
require.NoError(t, err, "Error locking file %q", fileName)
|
2021-09-02 15:43:54 +00:00
|
|
|
require.False(t, existed, "File %q reported as existing during locking.", fileName)
|
2018-05-29 18:35:48 +00:00
|
|
|
|
|
|
|
// File must now exist.
|
2021-09-02 15:43:54 +00:00
|
|
|
_, err = os.Stat(fileName)
|
2024-02-01 14:18:01 +00:00
|
|
|
require.NoError(t, err, "Could not stat file %q expected to exist", fileName)
|
2018-05-29 18:35:48 +00:00
|
|
|
|
|
|
|
// Try to lock again.
|
|
|
|
lockedAgain, existed, err := Flock(fileName)
|
2021-09-02 15:43:54 +00:00
|
|
|
require.Error(t, err, "File %q locked twice.", fileName)
|
|
|
|
require.Nil(t, lockedAgain, "Unsuccessful locking did not return nil.")
|
|
|
|
require.True(t, existed, "Existing file %q not recognized.", fileName)
|
2018-05-29 18:35:48 +00:00
|
|
|
|
2021-09-02 15:43:54 +00:00
|
|
|
err = lock.Release()
|
2024-02-01 14:18:01 +00:00
|
|
|
require.NoError(t, err, "Error releasing lock for file %q", fileName)
|
2018-05-29 18:35:48 +00:00
|
|
|
|
|
|
|
// File must still exist.
|
2021-09-02 15:43:54 +00:00
|
|
|
_, err = os.Stat(fileName)
|
2024-02-01 14:18:01 +00:00
|
|
|
require.NoError(t, err, "Could not stat file %q expected to exist", fileName)
|
2018-05-29 18:35:48 +00:00
|
|
|
|
|
|
|
// Lock existing file.
|
|
|
|
lock, existed, err = Flock(fileName)
|
2024-02-01 14:18:01 +00:00
|
|
|
require.NoError(t, err, "Error locking file %q", fileName)
|
2021-09-02 15:43:54 +00:00
|
|
|
require.True(t, existed, "Existing file %q not recognized.", fileName)
|
2018-05-29 18:35:48 +00:00
|
|
|
|
2021-09-02 15:43:54 +00:00
|
|
|
err = lock.Release()
|
2024-02-01 14:18:01 +00:00
|
|
|
require.NoError(t, err, "Error releasing lock for file %q", fileName)
|
2018-05-29 18:35:48 +00:00
|
|
|
}
|