github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/backend/sftp/stringlock_test.go (about)

     1  // +build !plan9
     2  
     3  package sftp
     4  
     5  import (
     6  	"fmt"
     7  	"sync"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestStringLock(t *testing.T) {
    15  	var wg sync.WaitGroup
    16  	counter := [3]int{}
    17  	lock := newStringLock()
    18  	const (
    19  		outer = 10
    20  		inner = 100
    21  		total = outer * inner
    22  	)
    23  	for k := 0; k < outer; k++ {
    24  		for j := range counter {
    25  			wg.Add(1)
    26  			go func(j int) {
    27  				defer wg.Done()
    28  				ID := fmt.Sprintf("%d", j)
    29  				for i := 0; i < inner; i++ {
    30  					lock.Lock(ID)
    31  					n := counter[j]
    32  					time.Sleep(1 * time.Millisecond)
    33  					counter[j] = n + 1
    34  					lock.Unlock(ID)
    35  				}
    36  
    37  			}(j)
    38  		}
    39  	}
    40  	wg.Wait()
    41  	assert.Equal(t, [3]int{total, total, total}, counter)
    42  }