-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhttp_test.go
135 lines (116 loc) · 2.65 KB
/
http_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
package buckets_test
import (
"encoding/binary"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"github.com/joyrexus/buckets"
)
// Set this to see how the counts are actually updated.
const verbose = false
// Counter updates a the hits bucket for every URL path requested.
type counter struct {
hits *buckets.Bucket
}
// Our handler communicates the new count from a successful database
// transaction.
func (c counter) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
key := []byte(req.URL.String())
// Decode handles key not found for us.
value, _ := c.hits.Get(key)
count := decode(value) + 1
if err := c.hits.Put(key, encode(count)); err != nil {
http.Error(rw, err.Error(), 500)
return
}
if verbose {
log.Printf("server: %s: %d", req.URL.String(), count)
}
// Reply with the new count .
rw.Header().Set("Content-Type", "application/octet-stream")
fmt.Fprintf(rw, "%d\n", count)
}
func client(id int, base string, paths []string) error {
// Process paths in random order.
rng := rand.New(rand.NewSource(int64(id)))
permutation := rng.Perm(len(paths))
for i := range paths {
path := paths[permutation[i]]
resp, err := http.Get(base + path)
if err != nil {
return err
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if verbose {
log.Printf("client: %s: %s", path, buf)
}
}
return nil
}
func ExampleBucket() {
// Open the database.
bx, _ := buckets.Open(tempfile())
defer os.Remove(bx.Path())
defer bx.Close()
// Create a hits bucket
hits, _ := bx.New([]byte("hits"))
// Start our web server
count := counter{hits}
srv := httptest.NewServer(count)
defer srv.Close()
// Get every path multiple times.
paths := []string{
"/foo",
"/bar",
"/baz",
"/quux",
"/thud",
"/xyzzy",
}
for id := 0; id < 10; id++ {
if err := client(id, srv.URL, paths); err != nil {
fmt.Printf("client error: %v", err)
}
}
// Check the final result
do := func(k, v []byte) error {
fmt.Printf("hits to %s: %d\n", k, decode(v))
return nil
}
hits.Map(do)
// outputs ...
// hits to /bar: 10
// hits to /baz: 10
// hits to /foo: 10
// hits to /quux: 10
// hits to /thud: 10
// hits to /xyzzy: 10
// Output:
// hits to /bar: 10
// hits to /baz: 10
// hits to /foo: 10
// hits to /quux: 10
// hits to /thud: 10
// hits to /xyzzy: 10
}
// encode marshals a counter.
func encode(n uint64) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, n)
return buf
}
// decode unmarshals a counter. Nil buffers are decoded as 0.
func decode(buf []byte) uint64 {
if buf == nil {
return 0
}
return binary.BigEndian.Uint64(buf)
}