summaryrefslogtreecommitdiff
path: root/src/pkg/net/fd_mutex_test.go
blob: 8383084b7a284cbad7429bf16799c7c1f7dd7ef4 (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
// 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 net

import (
	"math/rand"
	"runtime"
	"testing"
	"time"
)

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

	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 fdMutex
	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)
	var mu fdMutex
	mu.RWLock(true)
	for i := 0; i < 4; i++ {
		go func() {
			if mu.RWLock(true) {
				t.Fatal("broken")
			}
			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 fdMutex
	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 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)
	var mu fdMutex
	var readState [2]uint64
	var writeState [2]uint64
	for p := 0; p < P; p++ {
		go func() {
			r := rand.New(rand.NewSource(rand.Int63()))
			for i := 0; i < N; i++ {
				switch r.Intn(3) {
				case 0:
					if !mu.Incref() {
						t.Fatal("broken")
					}
					if mu.Decref() {
						t.Fatal("broken")
					}
				case 1:
					if !mu.RWLock(true) {
						t.Fatal("broken")
					}
					// Ensure that it provides mutual exclusion for readers.
					if readState[0] != readState[1] {
						t.Fatal("broken")
					}
					readState[0]++
					readState[1]++
					if mu.RWUnlock(true) {
						t.Fatal("broken")
					}
				case 2:
					if !mu.RWLock(false) {
						t.Fatal("broken")
					}
					// Ensure that it provides mutual exclusion for writers.
					if writeState[0] != writeState[1] {
						t.Fatal("broken")
					}
					writeState[0]++
					writeState[1]++
					if mu.RWUnlock(false) {
						t.Fatal("broken")
					}
				}
			}
			done <- true
		}()
	}
	for p := 0; p < P; p++ {
		<-done
	}
	if !mu.IncrefAndClose() {
		t.Fatal("broken")
	}
	if !mu.Decref() {
		t.Fatal("broken")
	}
}