Repository navigation
Expand file tree
/
Copy pathruntime_controlplane_test.go
More file actions
112 lines (92 loc) · 3.44 KB
/
Copy pathruntime_controlplane_test.go
File metadata and controls
112 lines (92 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// TestControlPlaneLockSerializes is the baseline: the lock still does the job a
// sync.Mutex did, so making it context-aware did not cost mutual exclusion.
func TestControlPlaneLockSerializes(t *testing.T) {
var lock controlPlaneLock
release, err := lock.acquire(context.Background())
require.NoError(t, err)
second := make(chan struct{})
go func() {
innerRelease, innerErr := lock.acquire(context.Background())
require.NoError(t, innerErr)
innerRelease()
close(second)
}()
select {
case <-second:
t.Fatal("a second holder entered the control plane while it was held")
case <-time.After(250 * time.Millisecond):
}
release()
select {
case <-second:
case <-time.After(5 * time.Second):
t.Fatal("releasing the control plane did not admit the waiter")
}
}
// TestControlPlaneLockRefusesCancelledCaller is the property sync.Mutex could
// not offer and the reason for the change. Lock() cannot be cancelled, so a
// caller whose RPC deadline had already passed would still queue behind an
// in-flight replay and then run DDL on behalf of a request that returned long
// ago. Here the caller must be turned away instead.
func TestControlPlaneLockRefusesCancelledCaller(t *testing.T) {
var lock controlPlaneLock
release, err := lock.acquire(context.Background())
require.NoError(t, err)
defer release()
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err = lock.acquire(ctx)
require.ErrorIs(t, err, context.Canceled,
"a cancelled caller must be refused, not queued behind the holder")
}
// TestControlPlaneLockRefusesCancelledCallerWhenFree pins the precheck. With the
// lock FREE and ctx already done, both select cases are ready and Go chooses
// between them at random — so without an explicit ctx check this would admit an
// expired caller a fraction of the time. Repeated so a random-choice regression
// cannot pass by luck.
func TestControlPlaneLockRefusesCancelledCallerWhenFree(t *testing.T) {
var lock controlPlaneLock
ctx, cancel := context.WithCancel(context.Background())
cancel()
for range 200 {
_, err := lock.acquire(ctx)
require.ErrorIs(t, err, context.Canceled,
"an expired caller must never enter a free control plane")
}
// Refusal must not have consumed a slot: the lock is still usable.
release, err := lock.acquire(context.Background())
require.NoError(t, err)
release()
}
// TestControlPlaneLockRespectsDeadline covers the waiting case: a caller that
// times out while queued gives up rather than executing late.
func TestControlPlaneLockRespectsDeadline(t *testing.T) {
var lock controlPlaneLock
release, err := lock.acquire(context.Background())
require.NoError(t, err)
defer release()
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
start := time.Now()
_, err = lock.acquire(ctx)
require.ErrorIs(t, err, context.DeadlineExceeded)
require.Less(t, time.Since(start), 5*time.Second, "acquire must return at its deadline, not hang")
}
// TestControlPlaneLockZeroValueIsUsable pins the lazy init: Runtime embeds this
// by value and NewRuntime does not initialize it, so the zero value has to work.
func TestControlPlaneLockZeroValueIsUsable(t *testing.T) {
var lock controlPlaneLock
release, err := lock.acquire(context.Background())
require.NoError(t, err)
release()
again, err := lock.acquire(context.Background())
require.NoError(t, err)
again()
}