github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/pkg/lock/file_test.go (about)

     1  // Copyright 2014 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package lock
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os"
    20  	"testing"
    21  )
    22  
    23  func TestNewLock(t *testing.T) {
    24  	f, err := ioutil.TempFile("", "")
    25  	if err != nil {
    26  		t.Fatalf("error creating tmpfile: %v", err)
    27  	}
    28  	defer os.Remove(f.Name())
    29  	f.Close()
    30  
    31  	l, err := NewLock(f.Name(), RegFile)
    32  	if err != nil {
    33  		t.Fatalf("error creating NewFileLock: %v", err)
    34  	}
    35  	l.Close()
    36  
    37  	d, err := ioutil.TempDir("", "")
    38  	if err != nil {
    39  		t.Fatalf("error creating tmpdir: %v", err)
    40  	}
    41  	defer os.Remove(d)
    42  
    43  	l, err = NewLock(d, Dir)
    44  	if err != nil {
    45  		t.Fatalf("error creating NewLock: %v", err)
    46  	}
    47  
    48  	err = l.Close()
    49  	if err != nil {
    50  		t.Fatalf("error unlocking lock: %v", err)
    51  	}
    52  
    53  	if err = os.Remove(d); err != nil {
    54  		t.Fatalf("error removing tmpdir: %v", err)
    55  	}
    56  
    57  	l, err = NewLock(d, Dir)
    58  	if err == nil {
    59  		t.Fatalf("expected error creating lock on nonexistent path")
    60  	}
    61  }
    62  
    63  func TestExclusiveLock(t *testing.T) {
    64  	dir, err := ioutil.TempDir("", "")
    65  	if err != nil {
    66  		t.Fatalf("error creating tmpdir: %v", err)
    67  	}
    68  	defer os.Remove(dir)
    69  
    70  	// Set up the initial exclusive lock
    71  	l, err := ExclusiveLock(dir, Dir)
    72  	if err != nil {
    73  		t.Fatalf("error creating lock: %v", err)
    74  	}
    75  
    76  	// reacquire the exclusive lock using the receiver interface
    77  	err = l.TryExclusiveLock()
    78  	if err != nil {
    79  		t.Fatalf("error reacquiring exclusive lock: %v", err)
    80  	}
    81  
    82  	// Now try another exclusive lock, should fail
    83  	_, err = TryExclusiveLock(dir, Dir)
    84  	if err == nil {
    85  		t.Fatalf("expected err trying exclusive lock")
    86  	}
    87  
    88  	// Unlock the original lock
    89  	err = l.Close()
    90  	if err != nil {
    91  		t.Fatalf("error closing lock: %v", err)
    92  	}
    93  
    94  	// Now another exclusive lock should succeed
    95  	_, err = TryExclusiveLock(dir, Dir)
    96  	if err != nil {
    97  		t.Fatalf("error creating lock: %v", err)
    98  	}
    99  }
   100  
   101  func TestSharedLock(t *testing.T) {
   102  	dir, err := ioutil.TempDir("", "")
   103  	if err != nil {
   104  		t.Fatalf("error creating tmpdir: %v", err)
   105  	}
   106  	defer os.Remove(dir)
   107  
   108  	// Set up the initial shared lock
   109  	l1, err := SharedLock(dir, Dir)
   110  	if err != nil {
   111  		t.Fatalf("error creating new shared lock: %v", err)
   112  	}
   113  
   114  	err = l1.TrySharedLock()
   115  	if err != nil {
   116  		t.Fatalf("error reacquiring shared lock: %v", err)
   117  	}
   118  
   119  	// Subsequent shared locks should succeed
   120  	l2, err := TrySharedLock(dir, Dir)
   121  	if err != nil {
   122  		t.Fatalf("error creating shared lock: %v", err)
   123  	}
   124  	l3, err := TrySharedLock(dir, Dir)
   125  	if err != nil {
   126  		t.Fatalf("error creating shared lock: %v", err)
   127  	}
   128  
   129  	// But an exclusive lock should fail
   130  	_, err = TryExclusiveLock(dir, Dir)
   131  	if err == nil {
   132  		t.Fatal("expected exclusive lock to fail")
   133  	}
   134  
   135  	// Close the locks
   136  	err = l1.Close()
   137  	if err != nil {
   138  		t.Fatalf("error closing lock: %v", err)
   139  	}
   140  	err = l2.Close()
   141  	if err != nil {
   142  		t.Fatalf("error closing lock: %v", err)
   143  	}
   144  
   145  	// Only unlock one of them
   146  	err = l3.Unlock()
   147  	if err != nil {
   148  		t.Fatalf("error unlocking lock: %v", err)
   149  	}
   150  
   151  	// Now try an exclusive lock, should succeed
   152  	_, err = TryExclusiveLock(dir, Dir)
   153  	if err != nil {
   154  		t.Fatalf("error creating lock: %v", err)
   155  	}
   156  }