github.com/teng231/glock@v1.1.11/count_lock_test.go (about)

     1  package glock
     2  
     3  import (
     4  	"log"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  /*
    10  	Run test: go test -run TestDistributeLock
    11  	go test -bench BenchmarkCounterLock100t  -benchmem
    12  	go test -bench BenchmarkCounterLock10000t  -benchmem
    13  ?*/
    14  func TestCountDown(t *testing.T) {
    15  	cd, err := CreateCountLock("localhost:6379", "", "test3_", time.Minute)
    16  	if err != nil {
    17  		log.Print(err)
    18  		t.Fail()
    19  	}
    20  	cd.Start("test1", 100, time.Minute)
    21  	cur, _ := cd.DecrBy("test1", 1)
    22  	log.Print(cur)
    23  	if cur != 99 {
    24  		log.Print(cur)
    25  		t.Fail()
    26  	}
    27  }
    28  
    29  func TestIncreaseUp(t *testing.T) {
    30  	cd, err := CreateCountLock("localhost:6379", "", "test3_", time.Minute)
    31  	if err != nil {
    32  		log.Print(err)
    33  		t.Fail()
    34  	}
    35  	cd.Start("test3", 100, time.Minute)
    36  	cur, _ := cd.IncrBy("test3", 1)
    37  	log.Print(cur)
    38  	if cur != 101 {
    39  		log.Print(cur)
    40  		t.Fail()
    41  	}
    42  }
    43  
    44  func BenchmarkCounterLock100t(t *testing.B) {
    45  	cd, err := CreateCountLock("localhost:6379", "", "test3_", 4*time.Second)
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	cd.Start("locktest1", 0, 4*time.Minute)
    50  	for i := 0; i < 100; i++ {
    51  		if _, err := cd.IncrBy("test4", 1); err != nil {
    52  			log.Print(err)
    53  		}
    54  		if _, err := cd.DecrBy("test4", 1); err != nil {
    55  			log.Print(err)
    56  		}
    57  	}
    58  	cur, err := cd.Current("test4")
    59  	if err != nil {
    60  		log.Print(err)
    61  	}
    62  	if cur != 0 {
    63  		t.Fail()
    64  	}
    65  }
    66  
    67  func BenchmarkCounterLock10000t(t *testing.B) {
    68  	cd, err := CreateCountLock("localhost:6379", "", "test3_", 4*time.Second)
    69  	if err != nil {
    70  		panic(err)
    71  	}
    72  	cd.Start("locktest1", 0, 4*time.Minute)
    73  	for i := 0; i < 10000; i++ {
    74  		if _, err := cd.IncrBy("test4", 1); err != nil {
    75  			log.Print(err)
    76  		}
    77  		if _, err := cd.DecrBy("test4", 1); err != nil {
    78  			log.Print(err)
    79  		}
    80  	}
    81  	cur, err := cd.Current("test4")
    82  	if err != nil {
    83  		log.Print(err)
    84  	}
    85  	if cur != 0 {
    86  		t.Fail()
    87  	}
    88  }