gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/host/storageobligationslock_test.go (about)

     1  package host
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"gitlab.com/SiaPrime/SiaPrime/build"
     8  	"gitlab.com/SiaPrime/SiaPrime/types"
     9  )
    10  
    11  // TestObligationLocks checks that the storage obligation locking functions
    12  // properly blocks and errors out for various use cases.
    13  func TestObligationLocks(t *testing.T) {
    14  	if testing.Short() || !build.VLONG {
    15  		t.SkipNow()
    16  	}
    17  	t.Parallel()
    18  	ht, err := blankHostTester("TestObligationLocks")
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	defer ht.Close()
    23  
    24  	// Simple lock and unlock.
    25  	ob1 := types.FileContractID{1}
    26  	ht.host.managedLockStorageObligation(ob1)
    27  	ht.host.managedUnlockStorageObligation(ob1)
    28  
    29  	// Simple lock and unlock, with trylock.
    30  	err = ht.host.managedTryLockStorageObligation(ob1, obligationLockTimeout)
    31  	if err != nil {
    32  		t.Fatal("unable to get lock despite not having a lock in place")
    33  	}
    34  	ht.host.managedUnlockStorageObligation(ob1)
    35  
    36  	// Threaded lock and unlock.
    37  	blockSuccessful := false
    38  	ht.host.managedLockStorageObligation(ob1)
    39  	go func() {
    40  		time.Sleep(obligationLockTimeout * 2)
    41  		blockSuccessful = true
    42  		ht.host.managedUnlockStorageObligation(ob1)
    43  	}()
    44  	ht.host.managedLockStorageObligation(ob1)
    45  	if !blockSuccessful {
    46  		t.Error("two threads were able to simultaneously grab an obligation lock")
    47  	}
    48  	ht.host.managedUnlockStorageObligation(ob1)
    49  
    50  	// Attempted lock and unlock - failed.
    51  	ht.host.managedLockStorageObligation(ob1)
    52  	go func() {
    53  		time.Sleep(obligationLockTimeout * 2)
    54  		ht.host.managedUnlockStorageObligation(ob1)
    55  	}()
    56  	err = ht.host.managedTryLockStorageObligation(ob1, obligationLockTimeout)
    57  	if err != errObligationLocked {
    58  		t.Fatal("storage obligation was able to get a lock, despite already being locked")
    59  	}
    60  
    61  	// Attempted lock and unlock - succeeded.
    62  	ht.host.managedLockStorageObligation(ob1)
    63  	go func() {
    64  		time.Sleep(obligationLockTimeout / 2)
    65  		ht.host.managedUnlockStorageObligation(ob1)
    66  	}()
    67  	err = ht.host.managedTryLockStorageObligation(ob1, obligationLockTimeout)
    68  	if err != nil {
    69  		t.Fatal("storage obligation unable to get lock, depsite having enough time")
    70  	}
    71  	ht.host.managedUnlockStorageObligation(ob1)
    72  
    73  	// Multiple locks and unlocks happening together.
    74  	ob2 := types.FileContractID{2}
    75  	ob3 := types.FileContractID{3}
    76  	ht.host.managedLockStorageObligation(ob1)
    77  	ht.host.managedLockStorageObligation(ob2)
    78  	ht.host.managedLockStorageObligation(ob3)
    79  	ht.host.managedUnlockStorageObligation(ob3)
    80  	ht.host.managedUnlockStorageObligation(ob2)
    81  	err = ht.host.managedTryLockStorageObligation(ob2, obligationLockTimeout)
    82  	if err != nil {
    83  		t.Fatal("unable to get lock despite not having a lock in place")
    84  	}
    85  	err = ht.host.managedTryLockStorageObligation(ob3, obligationLockTimeout)
    86  	if err != nil {
    87  		t.Fatal("unable to get lock despite not having a lock in place")
    88  	}
    89  	err = ht.host.managedTryLockStorageObligation(ob1, obligationLockTimeout)
    90  	if err != errObligationLocked {
    91  		t.Fatal("storage obligation was able to get a lock, despite already being locked")
    92  	}
    93  	ht.host.managedUnlockStorageObligation(ob1)
    94  }