Debian Codegraph @

render_test.go

raw symbols SCIP
1 package render
2
3 import (
4 "context"
5 "net/http"
6 "net/http/httptest"
7 "reflect"
8 "sync"
9 "testing"
10 )
11
12 var ctx = context.Background()
13
14 func TestLockConfig(t *testing.T) {
15 mutex := reflect.TypeOf(&sync.RWMutex{}).Kind()
16 empty := reflect.TypeOf(&emptyLock{}).Kind()
17
18 r1 := New(Options{
19 IsDevelopment: true,
20 UseMutexLock: false,
21 })
22 expect(t, reflect.TypeOf(r1.lock).Kind(), mutex)
23
24 r2 := New(Options{
25 IsDevelopment: true,
26 UseMutexLock: true,
27 })
28 expect(t, reflect.TypeOf(r2.lock).Kind(), mutex)
29
30 r3 := New(Options{
31 IsDevelopment: false,
32 UseMutexLock: true,
33 })
34 expect(t, reflect.TypeOf(r3.lock).Kind(), mutex)
35
36 r4 := New(Options{
37 IsDevelopment: false,
38 UseMutexLock: false,
39 })
40 expect(t, reflect.TypeOf(r4.lock).Kind(), empty)
41 }
42
43 /* Benchmarks */
44 func BenchmarkNormalJSON(b *testing.B) {
45 render := New()
46
47 h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
48 _ = render.JSON(w, 200, Greeting{"hello", "world"})
49 })
50
51 res := httptest.NewRecorder()
52 req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
53
54 for i := 0; i < b.N; i++ {
55 h.ServeHTTP(res, req)
56 }
57 }
58
59 func BenchmarkStreamingJSON(b *testing.B) {
60 render := New(Options{
61 StreamingJSON: true,
62 })
63
64 h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
65 _ = render.JSON(w, 200, Greeting{"hello", "world"})
66 })
67
68 res := httptest.NewRecorder()
69 req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
70
71 for i := 0; i < b.N; i++ {
72 h.ServeHTTP(res, req)
73 }
74 }
75
76 func BenchmarkHTML(b *testing.B) {
77 render := New(Options{
78 Directory: "testdata/basic",
79 })
80
81 h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
82 _ = render.HTML(w, http.StatusOK, "hello", "gophers")
83 })
84 req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
85
86 b.ResetTimer()
87 b.RunParallel(func(pb *testing.PB) {
88 for pb.Next() {
89 h.ServeHTTP(httptest.NewRecorder(), req)
90 }
91 })
92 }
93
94 /* Test Helper */
95 func expect(t *testing.T, a interface{}, b interface{}) {
96 if a != b {
97 t.Errorf("Expected ||%#v|| (type %v) - Got ||%#v|| (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
98 }
99 }
100
101 func expectNil(t *testing.T, a interface{}) {
102 if a != nil {
103 t.Errorf("Expected ||nil|| - Got ||%#v|| (type %v)", a, reflect.TypeOf(a))
104 }
105 }
106
107 func expectNotNil(t *testing.T, a interface{}) {
108 if a == nil {
109 t.Errorf("Expected ||not nil|| - Got ||nil|| (type %v)", reflect.TypeOf(a))
110 }
111 }