|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package kmutex |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "math/rand" |
| 22 | + "runtime" |
| 23 | + "strconv" |
| 24 | + "sync" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/containerd/containerd/pkg/seed" |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | +) |
| 31 | + |
| 32 | +func init() { |
| 33 | + seed.WithTimeAndRand() |
| 34 | +} |
| 35 | + |
| 36 | +func TestBasic(t *testing.T) { |
| 37 | + t.Parallel() |
| 38 | + |
| 39 | + km := newKeyMutex() |
| 40 | + ctx := context.Background() |
| 41 | + |
| 42 | + km.Lock(ctx, "c1") |
| 43 | + km.Lock(ctx, "c2") |
| 44 | + |
| 45 | + assert.Equal(t, len(km.locks), 2) |
| 46 | + assert.Equal(t, km.locks["c1"].ref, 1) |
| 47 | + assert.Equal(t, km.locks["c2"].ref, 1) |
| 48 | + |
| 49 | + checkWaitFn := func(key string, num int) { |
| 50 | + retries := 100 |
| 51 | + waitLock := false |
| 52 | + |
| 53 | + for i := 0; i < retries; i++ { |
| 54 | + // prevent from data-race |
| 55 | + km.mu.Lock() |
| 56 | + ref := km.locks[key].ref |
| 57 | + km.mu.Unlock() |
| 58 | + |
| 59 | + if ref == num { |
| 60 | + waitLock = true |
| 61 | + break |
| 62 | + } |
| 63 | + time.Sleep(time.Duration(rand.Int63n(100)) * time.Millisecond) |
| 64 | + } |
| 65 | + assert.Equal(t, waitLock, true) |
| 66 | + } |
| 67 | + |
| 68 | + // should acquire successfully after release |
| 69 | + { |
| 70 | + waitCh := make(chan struct{}) |
| 71 | + go func() { |
| 72 | + defer close(waitCh) |
| 73 | + |
| 74 | + km.Lock(ctx, "c1") |
| 75 | + }() |
| 76 | + checkWaitFn("c1", 2) |
| 77 | + |
| 78 | + km.Unlock("c1") |
| 79 | + |
| 80 | + <-waitCh |
| 81 | + assert.Equal(t, km.locks["c1"].ref, 1) |
| 82 | + } |
| 83 | + |
| 84 | + // failed to acquire if context cancel |
| 85 | + { |
| 86 | + var errCh = make(chan error, 1) |
| 87 | + |
| 88 | + ctx, cancel := context.WithCancel(context.Background()) |
| 89 | + go func() { |
| 90 | + errCh <- km.Lock(ctx, "c1") |
| 91 | + }() |
| 92 | + |
| 93 | + checkWaitFn("c1", 2) |
| 94 | + |
| 95 | + cancel() |
| 96 | + assert.Equal(t, <-errCh, context.Canceled) |
| 97 | + assert.Equal(t, km.locks["c1"].ref, 1) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestReleasePanic(t *testing.T) { |
| 102 | + t.Parallel() |
| 103 | + |
| 104 | + km := newKeyMutex() |
| 105 | + |
| 106 | + defer func() { |
| 107 | + if recover() == nil { |
| 108 | + t.Fatal("release of unlocked key did not panic") |
| 109 | + } |
| 110 | + }() |
| 111 | + |
| 112 | + km.Unlock(t.Name()) |
| 113 | +} |
| 114 | + |
| 115 | +func TestMultileAcquireOnKeys(t *testing.T) { |
| 116 | + t.Parallel() |
| 117 | + |
| 118 | + km := newKeyMutex() |
| 119 | + nloops := 10000 |
| 120 | + nproc := runtime.GOMAXPROCS(0) |
| 121 | + ctx := context.Background() |
| 122 | + |
| 123 | + var wg sync.WaitGroup |
| 124 | + for i := 0; i < nproc; i++ { |
| 125 | + wg.Add(1) |
| 126 | + |
| 127 | + go func(key string) { |
| 128 | + defer wg.Done() |
| 129 | + |
| 130 | + for i := 0; i < nloops; i++ { |
| 131 | + km.Lock(ctx, key) |
| 132 | + |
| 133 | + time.Sleep(time.Duration(rand.Int63n(100)) * time.Nanosecond) |
| 134 | + |
| 135 | + km.Unlock(key) |
| 136 | + } |
| 137 | + }("key-" + strconv.Itoa(i)) |
| 138 | + } |
| 139 | + wg.Wait() |
| 140 | +} |
| 141 | + |
| 142 | +func TestMultiAcquireOnSameKey(t *testing.T) { |
| 143 | + t.Parallel() |
| 144 | + |
| 145 | + km := newKeyMutex() |
| 146 | + key := "c1" |
| 147 | + ctx := context.Background() |
| 148 | + |
| 149 | + assert.Nil(t, km.Lock(ctx, key)) |
| 150 | + |
| 151 | + nproc := runtime.GOMAXPROCS(0) |
| 152 | + nloops := 10000 |
| 153 | + |
| 154 | + var wg sync.WaitGroup |
| 155 | + for i := 0; i < nproc; i++ { |
| 156 | + wg.Add(1) |
| 157 | + |
| 158 | + go func() { |
| 159 | + defer wg.Done() |
| 160 | + |
| 161 | + for i := 0; i < nloops; i++ { |
| 162 | + km.Lock(ctx, key) |
| 163 | + |
| 164 | + time.Sleep(time.Duration(rand.Int63n(100)) * time.Nanosecond) |
| 165 | + |
| 166 | + km.Unlock(key) |
| 167 | + } |
| 168 | + }() |
| 169 | + } |
| 170 | + km.Unlock(key) |
| 171 | + wg.Wait() |
| 172 | + |
| 173 | + // c1 key has been released so the it should not have any klock. |
| 174 | + assert.Equal(t, len(km.locks), 0) |
| 175 | +} |
0 commit comments