github.com/GitbookIO/syncgroup@v0.0.0-20200915204659-4f0b2961ab10/mutexes_test.go (about)

     1  package syncgroup
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestMutexSimple(t *testing.T) {
     8  	group := NewMutexGroup()
     9  
    10  	// Check that we can lock multiple keys at once
    11  	group.Lock("a")
    12  	group.Lock("b")
    13  	group.Lock("c")
    14  	group.Unlock("a")
    15  	group.Unlock("b")
    16  	group.Unlock("c")
    17  }
    18  
    19  func TestMutexDeleted(t *testing.T) {
    20  	group := NewMutexGroup()
    21  	keys := []string{"a", "b", "c"}
    22  
    23  	// Lock
    24  	for _, key := range keys {
    25  		group.Lock(key)
    26  	}
    27  
    28  	// TODO: Ensure keys exist
    29  
    30  	// Unlock
    31  	for _, key := range keys {
    32  		group.Unlock(key)
    33  	}
    34  
    35  	// Ensure keys are deleted once unlocked
    36  	for _, key := range keys {
    37  		if group.Has(key) {
    38  			t.Errorf("MutexGroup: '%s' should be deleted since all of it's instances have been unlocked", key)
    39  		}
    40  	}
    41  }