github.com/andy2046/gopie@v0.7.0/pkg/spinlock/spinlock_test.go (about)

     1  package spinlock_test
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  	"time"
     7  
     8  	. "github.com/andy2046/gopie/pkg/spinlock"
     9  )
    10  
    11  func TestLocker(t *testing.T) {
    12  	threads, loops, count := 8, 1000000, 0
    13  	var wg sync.WaitGroup
    14  	wg.Add(threads)
    15  
    16  	l := New()
    17  	start := time.Now()
    18  	for i := 0; i < threads; i++ {
    19  		go func() {
    20  			for i := 0; i < loops; i++ {
    21  				l.Lock()
    22  				count++
    23  				if !l.IsLocked() {
    24  					t.Error("expected to be locked")
    25  				}
    26  				l.Unlock()
    27  			}
    28  			wg.Done()
    29  		}()
    30  	}
    31  	wg.Wait()
    32  
    33  	duration := time.Since(start)
    34  	t.Logf("duration: %4.2f Seconds\n", duration.Seconds())
    35  	if count != threads*loops {
    36  		t.Errorf("expected %d got %d", threads*loops, count)
    37  	}
    38  }
    39  
    40  func TestNoCopy(t *testing.T) {
    41  	// go vet fails
    42  	var l1 Locker
    43  	l2 := l1
    44  	var l3 = l1
    45  	l2 = l1
    46  	_, _ = l2, l3
    47  	t.Log("go vet fails here")
    48  }