github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/lock/lock_test.go (about) 1 package lock 2 3 import ( 4 "context" 5 "sync" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestRedisLocker(t *testing.T) { 13 l := NewRedisLocker("127.0.0.1:6379", "", 0) 14 testLocker(t, l) 15 } 16 17 func TestProcessLocker(t *testing.T) { 18 l := NewProcessLocker() 19 testLocker(t, l) 20 } 21 22 func testLocker(t *testing.T, l Locker) { 23 t.Run("success", func(t *testing.T) { 24 g := sync.WaitGroup{} 25 g.Add(2) 26 go func() { 27 err := l.Do(context.Background(), "hello", time.Second, func(ctx context.Context) error { 28 time.Sleep(time.Second * 3) 29 return nil 30 }) 31 require.Nil(t, err, "err must be nil") 32 g.Done() 33 }() 34 go func() { 35 time.Sleep(time.Second) 36 err := l.Do(context.Background(), "world", time.Second, func(ctx context.Context) error { 37 time.Sleep(time.Second * 3) 38 return nil 39 }) 40 require.Nil(t, err, "err must be nil") 41 g.Done() 42 }() 43 g.Wait() 44 }) 45 t.Run("success on previous timeout", func(t *testing.T) { 46 g := sync.WaitGroup{} 47 g.Add(2) 48 go func() { 49 err := l.Do(context.Background(), "hello", time.Second, func(ctx context.Context) error { 50 time.Sleep(time.Second * 2) 51 return nil 52 }) 53 require.Nil(t, err, "err must be nil") 54 g.Done() 55 }() 56 go func() { 57 time.Sleep(time.Second * 2) 58 err := l.Do(context.Background(), "hello", time.Second, func(ctx context.Context) error { 59 time.Sleep(time.Second * 2) 60 return nil 61 }) 62 require.Nil(t, err, "err must be nil") 63 g.Done() 64 }() 65 g.Wait() 66 }) 67 t.Run("failed on previous valid", func(t *testing.T) { 68 g := sync.WaitGroup{} 69 g.Add(2) 70 go func() { 71 err := l.Do(context.Background(), "hello", time.Second*2, func(ctx context.Context) error { 72 time.Sleep(time.Second * 2) 73 return nil 74 }) 75 require.Nil(t, err, "err must be nil") 76 g.Done() 77 }() 78 go func() { 79 time.Sleep(time.Second) 80 err := l.Do(context.Background(), "hello", time.Second*2, func(ctx context.Context) error { 81 time.Sleep(time.Second * 2) 82 return nil 83 }) 84 require.NotNil(t, err, "err must be not nil") 85 t.Logf("%+v\n", err) 86 g.Done() 87 }() 88 g.Wait() 89 }) 90 } 91 92 func TestLockerFunctional(t *testing.T) { 93 t.Run("redis", func(t *testing.T) { 94 l := NewRedisLocker("", "", 0) 95 testFunctional(t, l) 96 }) 97 t.Run("process", func(t *testing.T) { 98 l := NewProcessLocker() 99 testFunctional(t, l) 100 }) 101 } 102 103 func testFunctional(t *testing.T, l Locker) { 104 t.Run("timeout", func(t *testing.T) { 105 err := l.Do(context.Background(), "hello", time.Second, func(ctx context.Context) error { 106 t := time.NewTimer(time.Second * 2) 107 select { 108 case <-ctx.Done(): 109 return ctx.Err() 110 case <-t.C: 111 return nil 112 } 113 }) 114 require.NotNil(t, err, "err should not be nil") 115 }) 116 t.Run("success", func(t *testing.T) { 117 err := l.Do(context.Background(), "hello", time.Second*2, func(ctx context.Context) error { 118 t := time.NewTimer(time.Second) 119 select { 120 case <-ctx.Done(): 121 return ctx.Err() 122 case <-t.C: 123 return nil 124 } 125 }) 126 require.Nil(t, err, "err should be nil") 127 }) 128 }