forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restful_server_list_test.go
162 lines (132 loc) · 3.69 KB
/
restful_server_list_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
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
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package das
import (
"context"
"fmt"
"net"
"net/http"
"testing"
"time"
)
func TestRestfulServerList(t *testing.T) {
initTest(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
urlsIn := []string{"https://supersecret.nowhere.com:9871", "https://www.google.com"}
listContents := urlsIn[0] + " \t" + urlsIn[1]
port, server := newListHttpServerForTest(t, &stringHandler{listContents})
listUrl := fmt.Sprintf("https://localhost:%d", port)
urls, err := RestfulServerURLsFromList(ctx, listUrl)
Require(t, err)
if !stringListIsPermutation(urlsIn, urls) {
t.Fatal()
}
err = server.Shutdown(ctx)
Require(t, err)
}
func TestRestfulServerListDaemon(t *testing.T) {
initTest(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
urlsIn := []string{"https://supersecret.nowhere.com:9871", "https://www.google.com"}
listContents := urlsIn[0] + " \t" + urlsIn[1]
port, server := newListHttpServerForTest(t, &stringHandler{listContents})
listUrl := fmt.Sprintf("https://localhost:%d", port)
listChan := StartRestfulServerListFetchDaemon(ctx, listUrl, 200*time.Millisecond)
for i := 0; i < 4; i++ {
list := <-listChan
if !stringListIsPermutation(list, urlsIn) {
t.Fatal(i)
}
}
err := server.Shutdown(ctx)
Require(t, err)
}
func TestRestfulServerListDaemonWithErrors(t *testing.T) {
initTest(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
urlsIn := []string{"https://supersecret.nowhere.com:9871", "https://www.google.com"}
listContents := urlsIn[0] + " \t" + urlsIn[1]
port, server := newListHttpServerForTest(
t,
Handlers(
&connectionClosingHandler{},
&connectionClosingHandler{},
&stringHandler{listContents},
&erroringHandler{},
&erroringHandler{},
&stringHandler{listContents},
&erroringHandler{},
&connectionClosingHandler{},
&stringHandler{listContents},
),
)
listUrl := fmt.Sprintf("https://localhost:%d", port)
listChan := StartRestfulServerListFetchDaemon(ctx, listUrl, 200*time.Millisecond)
for i := 0; i < 3; i++ {
list := <-listChan
if !stringListIsPermutation(list, urlsIn) {
t.Fatal(i, "not a match")
}
}
err := server.Shutdown(ctx)
Require(t, err)
}
func stringListIsPermutation(lis1, lis2 []string) bool {
if len(lis1) != len(lis2) {
return false
}
lookup := make(map[string]bool)
for _, s := range lis1 {
lookup[s] = true
}
for _, s := range lis2 {
if !lookup[s] {
return false
}
}
return true
}
func newListHttpServerForTest(t *testing.T, handler http.Handler) (int, *http.Server) {
server := &http.Server{
Handler: handler,
ReadHeaderTimeout: 5 * time.Second,
}
listener, err := net.Listen("tcp", "localhost:0")
Require(t, err)
go func() {
_ = server.Serve(listener)
}()
tcpAddr, _ := listener.Addr().(*net.TCPAddr)
return tcpAddr.Port, server
}
type stringHandler struct {
contents string
}
func (h *stringHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
_, _ = w.Write([]byte(h.contents))
}
type erroringHandler struct {
}
func (h *erroringHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(404)
}
type connectionClosingHandler struct {
}
func (h *connectionClosingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
panic("close connection")
}
type multiHandler struct {
current int
handlers []http.Handler
}
func Handlers(hs ...http.Handler) *multiHandler {
return &multiHandler{0, hs}
}
func (h *multiHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
i := h.current % len(h.handlers)
h.current++
h.handlers[i].ServeHTTP(w, req)
}