summaryrefslogtreecommitdiffstats
path: root/src/internal/poll/fd_mutex_test.go
blob: 62f953192d751eeae80fc819443d5b8b8dc60d61 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package poll_test

import (
	. "internal/poll"
	"math/rand"
	"runtime"
	"strings"
	"testing"
	"time"
)

func TestMutexLock(t *testing.T) {
	var mu XFDMutex

	if !mu.Incref() {
		t.Fatal("broken")
	}
	if mu.Decref() {
		t.Fatal("broken")
	}

	if !mu.RWLock(true) {
		t.Fatal("broken")
	}
	if mu.RWUnlock(true) {
		t.Fatal("broken")
	}

	if !mu.RWLock(false) {
		t.Fatal("broken")
	}
	if mu.RWUnlock(false) {
		t.Fatal("broken")
	}
}

func TestMutexClose(t *testing.T) {
	var mu XFDMutex
	if !mu.IncrefAndClose() {
		t.Fatal("broken")
	}

	if mu.Incref() {
		t.Fatal("broken")
	}
	if mu.RWLock(true) {
		t.Fatal("broken")
	}
	if mu.RWLock(false) {
		t.Fatal("broken")
	}
	if mu.IncrefAndClose() {
		t.Fatal("broken")
	}
}

func TestMutexCloseUnblock(t *testing.T) {
	c := make(chan bool, 4)
	var mu XFDMutex
	mu.RWLock(true)
	for i := 0; i < 4; i++ {
		go func() {
			if mu.RWLock(true) {
				t.Error("broken")
				return
			}
			c <- true
		}()
	}
	// Concurrent goroutines must not be able to read lock the mutex.
	time.Sleep(time.Millisecond)
	select {
	case <-c:
		t.Fatal("broken")
	default:
	}
	mu.IncrefAndClose() // Must unblock the readers.
	for i := 0; i < 4; i++ {
		select {
		case <-c:
		case <-time.After(10 * time.Second):
			t.Fatal("broken")
		}
	}
	if mu.Decref() {
		t.Fatal("broken")
	}
	if !mu.RWUnlock(true) {
		t.Fatal("broken")
	}
}

func TestMutexPanic(t *testing.T) {
	ensurePanics := func(f func()) {
		defer func() {
			if recover() == nil {
				t.Fatal("does not panic")
			}
		}()
		f()
	}

	var mu XFDMutex
	ensurePanics(func() { mu.Decref() })
	ensurePanics(func() { mu.RWUnlock(true) })
	ensurePanics(func() { mu.RWUnlock(false) })

	ensurePanics(func() { mu.Incref(); mu.Decref(); mu.Decref() })
	ensurePanics(func() { mu.RWLock(true); mu.RWUnlock(true); mu.RWUnlock(true) })
	ensurePanics(func() { mu.RWLock(false); mu.RWUnlock(false); mu.RWUnlock(false) })

	// ensure that it's still not broken
	mu.Incref()
	mu.Decref()
	mu.RWLock(true)
	mu.RWUnlock(true)
	mu.RWLock(false)
	mu.RWUnlock(false)
}

func TestMutexOverflowPanic(t *testing.T) {
	defer func() {
		r := recover()
		if r == nil {
			t.Fatal("did not panic")
		}
		msg, ok := r.(string)
		if !ok {
			t.Fatalf("unexpected panic type %T", r)
		}
		if !strings.Contains(msg, "too many") || strings.Contains(msg, "inconsistent") {
			t.Fatalf("wrong panic message %q", msg)
		}
	}()

	var mu1 XFDMutex
	for i := 0; i < 1<<21; i++ {
		mu1.Incref()
	}
}

func TestMutexStress(t *testing.T) {
	P := 8
	N := int(1e6)
	if testing.Short() {
		P = 4
		N = 1e4
	}
	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(P))
	done := make(chan bool, P)
	var mu XFDMutex
	var readState [2]uint64
	var writeState [2]uint64
	for p := 0; p < P; p++ {
		go func() {
			defer func() {
				done <- !t.Failed()
			}()
			r := rand.New(rand.NewSource(rand.Int63()))
			for i := 0; i < N; i++ {
				switch r.Intn(3) {
				case 0:
					if !mu.Incref() {
						t.Error("broken")
						return
					}
					if mu.Decref() {
						t.Error("broken")
						return
					}
				case 1:
					if !mu.RWLock(true) {
						t.Error("broken")
						return
					}
					// Ensure that it provides mutual exclusion for readers.
					if readState[0] != readState[1] {
						t.Error("broken")
						return
					}
					readState[0]++
					readState[1]++
					if mu.RWUnlock(true) {
						t.Error("broken")
						return
					}
				case 2:
					if !mu.RWLock(false) {
						t.Error("broken")
						return
					}
					// Ensure that it provides mutual exclusion for writers.
					if writeState[0] != writeState[1] {
						t.Error("broken")
						return
					}
					writeState[0]++
					writeState[1]++
					if mu.RWUnlock(false) {
						t.Error("broken")
						return
					}
				}
			}
		}()
	}
	for p := 0; p < P; p++ {
		if !<-done {
			t.FailNow()
		}
	}
	if !mu.IncrefAndClose() {
		t.Fatal("broken")
	}
	if !mu.Decref() {
		t.Fatal("broken")
	}
}