-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathexample_test.go
71 lines (61 loc) · 1.24 KB
/
example_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
package fscache
import (
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"time"
)
func Example() {
// create the cache, keys expire after 1 hour.
c, err := New("./cache", 0755, time.Hour)
if err != nil {
log.Fatal(err.Error())
}
// wipe the cache when done
defer os.RemoveAll("./cache")
defer c.Clean()
// Get() and it's streams can be called concurrently but just for example:
for i := 0; i < 3; i++ {
r, w, err := c.Get("stream")
if err != nil {
log.Fatal(err.Error())
}
if w != nil { // a new stream, write to it.
go func() {
w.Write([]byte("hello world\n"))
w.Close()
}()
}
// the stream has started, read from it
io.Copy(os.Stdout, r)
r.Close()
}
// Output:
// hello world
// hello world
// hello world
}
func ExampleHandler() {
c, err := New("./server", 0700, 0)
if err != nil {
log.Fatal(err.Error())
}
defer os.RemoveAll("./server")
defer c.Clean()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello Client")
})
ts := httptest.NewServer(Handler(c, handler))
defer ts.Close()
resp, err := http.Get(ts.URL)
if err != nil {
log.Fatal(err.Error())
}
io.Copy(os.Stdout, resp.Body)
resp.Body.Close()
// Output:
// Hello Client
}