github.com/apptainer/singularity@v3.1.1+incompatible/pkg/util/fs/lock/lock_test.go (about)

     1  // Copyright (c) 2019, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package lock
     7  
     8  import (
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/sylabs/singularity/internal/pkg/test"
    13  )
    14  
    15  func TestLock(t *testing.T) {
    16  	test.DropPrivilege(t)
    17  	defer test.ResetPrivilege(t)
    18  
    19  	if _, err := Exclusive(""); err == nil {
    20  		t.Errorf("unexpected success with empty path")
    21  	}
    22  
    23  	ch := make(chan bool, 1)
    24  
    25  	fd, err := Exclusive("/dev")
    26  	if err != nil {
    27  		t.Error(err)
    28  	}
    29  
    30  	go func() {
    31  		Exclusive("/dev")
    32  		ch <- true
    33  	}()
    34  
    35  	select {
    36  	case <-time.After(1 * time.Second):
    37  		Release(fd)
    38  		if err := Release(fd); err == nil {
    39  			t.Errorf("unexpected success during Release second call")
    40  		}
    41  	case <-ch:
    42  		t.Errorf("lock acquired")
    43  	}
    44  }