inmemory_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package lockman
  15. import (
  16. "context"
  17. "fmt"
  18. "math/rand"
  19. "os"
  20. "sync"
  21. "testing"
  22. "time"
  23. "github.com/petermattis/goid"
  24. "yunion.io/x/log"
  25. "yunion.io/x/pkg/utils"
  26. )
  27. func TestInMemoryLockManager(t *testing.T) {
  28. lockman := NewInMemoryLockManager()
  29. shared := newSharedObject()
  30. cfg := &testLockManagerConfig{
  31. players: 3,
  32. cycles: 3,
  33. lockman: lockman,
  34. shared: shared,
  35. }
  36. testLockManager(t, cfg)
  37. }
  38. func TestRunManu(t *testing.T) {
  39. rand.Seed(100)
  40. lockman := NewInMemoryLockManager()
  41. counter := 0
  42. MAX := 512
  43. for i := 0; i < MAX; i++ {
  44. go func() {
  45. ctx := context.Background()
  46. now := time.Now()
  47. ms := now.UnixMilli()
  48. ctx = context.WithValue(ctx, "time", ms)
  49. lockman.LockKey(ctx, "test")
  50. defer lockman.UnlockKey(ctx, "test")
  51. counter++
  52. time.Sleep(1 + time.Duration(rand.Intn(50))*time.Millisecond)
  53. t.Logf("counter: %d", counter)
  54. }()
  55. }
  56. for counter < MAX {
  57. time.Sleep(1)
  58. }
  59. t.Logf("complete")
  60. }
  61. type app struct {
  62. ctx context.Context
  63. key string
  64. lockman ILockManager
  65. }
  66. func (app *app) run() {
  67. app.lockman.LockKey(app.ctx, app.key)
  68. defer app.lockman.UnlockKey(app.ctx, app.key)
  69. fmt.Printf("run for goid: %d key %s\n", goid.Get(), app.key)
  70. }
  71. type emptyKey struct{}
  72. func TestRunManu3(t *testing.T) {
  73. for i := 0; i < 100; i++ {
  74. TestRunManu2(t)
  75. }
  76. }
  77. func TestRunManu2(t *testing.T) {
  78. rand.Seed(100)
  79. lockman := NewInMemoryLockManager()
  80. debug_log = true
  81. // 使用 WaitGroup 来等待所有 goroutine 完成
  82. var wg sync.WaitGroup
  83. MAX_GOROUTINE := 4096
  84. MAX_KEY := 4
  85. complete := make(chan struct{})
  86. go func() {
  87. for {
  88. select {
  89. case <-time.After(time.Second):
  90. t.Logf("timeout")
  91. utils.DumpAllGoroutineStack(os.Stdout)
  92. case <-complete:
  93. return
  94. }
  95. }
  96. }()
  97. bgCtx := context.Background()
  98. // 为每个 goroutine 创建独立的 context
  99. for i := 0; i < MAX_GOROUTINE; i++ {
  100. wg.Add(1)
  101. go func(id int) {
  102. defer wg.Done()
  103. // 为每个 goroutine 创建新的 context
  104. ctx := context.WithValue(bgCtx, emptyKey{}, id)
  105. log.Infof("ctx for id %d: %p", id, ctx)
  106. app := &app{
  107. ctx: ctx,
  108. key: fmt.Sprintf("test-%d", id%MAX_KEY),
  109. lockman: lockman,
  110. }
  111. app.run()
  112. }(i)
  113. }
  114. // 等待所有 goroutine 完成
  115. wg.Wait()
  116. close(complete)
  117. t.Logf("complete")
  118. }