-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecoverer_test.go
86 lines (82 loc) · 1.94 KB
/
recoverer_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
// recoverer_test.go
package minigrush
import (
"encoding/json"
"net/http"
"net/url"
"reflect"
"testing"
"time"
"github.com/crbrox/store/dummy"
)
func TestA(t *testing.T) {
url1, _ := url.Parse("http://golang.org/pkg/net/http/#NewRequest")
url2, _ := url.Parse("https://www.google.es")
p1 := &Petition{
Id: "id1",
Body: []byte("body1"),
Method: "GET",
URL: url1,
Proto: "HTTP/1.1",
Header: make(http.Header),
Trailer: make(http.Header),
RemoteAddr: "RemoteAddr1",
RequestURI: "RequestURI1",
TargetHost: "targetHost1",
TargetScheme: "http",
Created: time.Now()}
p2 := &Petition{
Id: "id2",
Body: []byte("body2"),
Method: "POST",
URL: url2,
Proto: "HTTP/1.1",
Header: make(http.Header),
Trailer: make(http.Header),
RemoteAddr: "RemoteAddr2",
RequestURI: "RequestURI2",
TargetHost: "targetHost2",
TargetScheme: "https",
Created: time.Now()}
var petitions = []*Petition{p1, p2}
petStore := dummy.Store{}
petCh := make(chan *Petition, 1000)
for _, p := range petitions {
text, err := json.Marshal(p)
if err != nil {
t.Error(err)
}
petStore.Put(p.Id, text)
}
recoverer := &Recoverer{
SendTo: petCh,
PetitionStore: petStore,
}
err := recoverer.Recover()
if err != nil {
t.Error(err)
}
if len(petCh) != len(petitions) {
t.Fatalf("number of stored petitions should be equal to enqueued ones len(petCh) %d len(petitions) %d ",
len(petCh), len(petitions))
}
for i := 0; i < len(petitions); i++ {
select {
case ep := <-petCh:
var sp *Petition
switch ep.Id {
case "id1":
sp = p1
case "id2":
sp = p2
default:
t.Fatalf("unknown enqueued petition id %q", ep.Id)
}
if !reflect.DeepEqual(ep, sp) {
t.Fatal("enqued petition is not equal to stored petition")
}
default:
t.Fatal("should be more enqueued petitions")
}
}
}