-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport_test.go
94 lines (85 loc) · 1.66 KB
/
port_test.go
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
// Copyright (c) 2017, Boise State University All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"net"
"testing"
)
func TestPortRange(t *testing.T) {
pr := newPortRange(8000, 8)
p, err := pr.Acquire()
if err != nil {
t.Error(err)
}
if !pr.ports[0] {
t.Errorf("didn't acquire port %d", p)
}
pr.Drop(p)
if pr.ports[0] {
t.Errorf("failed to drop %d", p)
}
}
func TestFullPortRange(t *testing.T) {
const n = 8
const sp = 8000
pr := newPortRange(sp, n)
for i := 0; i < n; i++ {
_, err := pr.Acquire()
if err != nil {
t.Error(err)
}
}
}
func TestPortOverflow(t *testing.T) {
pr := newPortRange(8000, 100)
for i := 0; i < 100; i++ {
_, err := pr.Acquire()
if err != nil {
t.Error(err)
}
}
_, err := pr.Acquire()
if err != errNotebookPoolFull {
t.Errorf("should have errored with %s, didn't", errNotebookPoolFull)
}
}
func TestZombiePort(t *testing.T) {
pr := newPortRange(8000, 10)
_, err := pr.Acquire()
if err != nil {
t.Error(err)
}
// steal port 8001
s, err := net.Listen("tcp", ":8001")
if err != nil {
t.Error(err)
}
p, err := pr.Acquire()
if err != nil {
t.Error(err)
}
s.Close()
if p != 8002 {
t.Errorf("bad port exp: %d, got: %d", 8002, p)
}
}
func TestOutOfRange(t *testing.T) {
pr := newPortRange(8000, 1)
err := pr.Drop(8002)
if err != errPortOutOfRange {
t.Errorf("unexpected error: %s", err)
}
}
func BenchmarkPortRange(b *testing.B) {
pr := newPortRange(8000, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 100; j++ {
pr.Acquire()
}
for p := 8000; p < 100; p++ {
pr.Drop(i)
}
}
}