File: conntrack_test.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 872 kB
  • sloc: makefile: 4
file content (185 lines) | stat: -rw-r--r-- 3,908 bytes parent folder | download | duplicates (2)
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
package conntrack

import (
	"context"
	"math"
	"strconv"
	"sync"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"

	_ "github.com/anacrolix/envpprof"
	"github.com/bradfitz/iter"
)

func entry(id int) Entry {
	return Entry{"", "", strconv.FormatInt(int64(id), 10)}
}

func TestWaitingForSameEntry(t *testing.T) {
	i := NewInstance()
	i.SetMaxEntries(1)
	i.Timeout = func(Entry) time.Duration {
		return 0
	}
	e1h1 := i.WaitDefault(context.Background(), entry(1))
	gotE2s := make(chan struct{})
	for range iter.N(2) {
		go func() {
			i.WaitDefault(context.Background(), entry(2))
			gotE2s <- struct{}{}
		}()
	}
	gotE1 := make(chan struct{})
	var e1h2 *EntryHandle
	go func() {
		e1h2 = i.WaitDefault(context.Background(), entry(1))
		gotE1 <- struct{}{}
	}()
	select {
	case <-gotE1:
	case <-gotE2s:
		t.FailNow()
	}
	go e1h1.Done()
	go e1h2.Done()
	<-gotE2s
	<-gotE2s
}

func TestInstanceSetNoMaxEntries(t *testing.T) {
	i := NewInstance()
	i.SetMaxEntries(0)
	var wg sync.WaitGroup
	wait := func(e Entry, p priority) {
		i.Wait(context.Background(), e, "", p)
		wg.Done()
	}
	for _, e := range []Entry{entry(0), entry(1)} {
		for _, p := range []priority{math.MinInt32, math.MaxInt32} {
			wg.Add(1)
			go wait(e, p)
		}
	}
	waitForNumWaiters := func(num int) {
		i.mu.Lock()
		for len(i.waiters) != num {
			i.numWaitersChanged.Wait()
		}
		i.mu.Unlock()
	}
	waitForNumWaiters(4)
	i.SetNoMaxEntries()
	waitForNumWaiters(0)
	wg.Wait()
}

func TestWaitReturnsNilContextCompleted(t *testing.T) {
	i := NewInstance()
	i.SetMaxEntries(0)
	ctx, cancel := context.WithCancel(context.Background())
	cancel()
	assert.Nil(t, i.WaitDefault(ctx, entry(0)))
	ctx, cancel = context.WithTimeout(context.Background(), 10*time.Millisecond)
	assert.Nil(t, i.WaitDefault(ctx, entry(1)))
	cancel()
}

func TestWaitContextCanceledButRoomForEntry(t *testing.T) {
	i := NewInstance()
	i.SetMaxEntries(1)
	ctx, cancel := context.WithCancel(context.Background())
	go cancel()
	eh := i.WaitDefault(ctx, entry(0))
	if eh == nil {
		assert.Error(t, ctx.Err())
	} else {
		eh.Done()
	}
}

func TestUnlimitedInstance(t *testing.T) {
	i := NewInstance()
	i.SetNoMaxEntries()
	i.Timeout = func(Entry) time.Duration { return 0 }
	eh := i.WaitDefault(context.Background(), entry(0))
	assert.NotNil(t, eh)
	i.mu.Lock()
	assert.Len(t, i.entries[eh.e], 1)
	i.mu.Unlock()
	eh.Done()
	i.mu.Lock()
	assert.Nil(t, i.entries[eh.e])
	i.mu.Unlock()
}

func TestUnlimitedInstanceContextCanceled(t *testing.T) {
	i := NewInstance()
	i.SetNoMaxEntries()
	i.Timeout = func(Entry) time.Duration { return 0 }
	ctx, cancel := context.WithCancel(context.Background())
	cancel()
	eh := i.WaitDefault(ctx, entry(0))
	assert.NotNil(t, eh)
	i.mu.Lock()
	assert.Len(t, i.entries[eh.e], 1)
	i.mu.Unlock()
	eh.Done()
	i.mu.Lock()
	assert.Nil(t, i.entries[eh.e])
	i.mu.Unlock()
}

func TestContextCancelledWhileWaiting(t *testing.T) {
	i := NewInstance()
	i.SetMaxEntries(0)
	ctx, cancel := context.WithCancel(context.Background())
	i.mu.Lock()
	assert.Len(t, i.waiters, 0)
	i.mu.Unlock()
	waitReturned := make(chan struct{})
	go func() {
		eh := i.WaitDefault(ctx, entry(0))
		assert.Nil(t, eh)
		close(waitReturned)
	}()
	for {
		i.mu.Lock()
		if len(i.waiters) == 1 {
			i.mu.Unlock()
			break
		}
		i.mu.Unlock()
		time.Sleep(time.Millisecond)
	}
	cancel()
	<-waitReturned
	assert.Len(t, i.entries, 0)
	assert.Len(t, i.waiters, 0)
}

func TestRaceWakeAndContextCompletion(t *testing.T) {
	i := NewInstance()
	i.SetMaxEntries(1)
	eh0 := i.WaitDefault(context.Background(), entry(0))
	ctx, cancel := context.WithCancel(context.Background())
	waitReturned := make(chan struct{})
	go func() {
		eh1 := i.WaitDefault(ctx, entry(1))
		if eh1 != nil {
			eh1.Forget()
		}
		close(waitReturned)
	}()
	go cancel()
	go eh0.Forget()
	<-waitReturned
	cancel()
	eh0.Forget()
	i.mu.Lock()
	assert.Len(t, i.entries, 0)
	assert.Len(t, i.waiters, 0)
	i.mu.Unlock()
}